Skip to content

fix: Redact nested private attributes when attribute values are stdClass objects - #306

Open
zigzagdev wants to merge 2 commits into
launchdarkly:mainfrom
zigzagdev:fix/redact-private-attributes-in-object-values
Open

zigzagdev wants to merge 2 commits into
launchdarkly:mainfrom
zigzagdev:fix/redact-private-attributes-in-object-values

Conversation

@zigzagdev

@zigzagdev zigzagdev commented Nov 21, 2026 •

Copy link
Copy Markdown

fix: Redact nested private attributes in associative arrays that have numeric keys

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

Related issues

None.

Describe the solution you've provided

Private attribute redaction only handled associative arrays, so nested private attributes inside stdClass values were sent unredacted.
So, this PR affects contexts created with LDContext::fromJson(), which decodes JSON objects as stdClass.

This change applies the same redaction to stdClass values and keeps them as JSON objects after redaction, including when their property names are numeric.

Describe alternatives you've considered

Converting stdClass values to associative arrays in LDContext::fromJson(). That would change the attribute values that callers get back from the context, so I kept the fix in the serializer, where redaction happens.

Added some tests in tests/Impl/Events/EventSerializerTest.php:

  • testObjectPropertyRedactionInContextParsedFromJsonString: a private attribute nested in a context from LDContext::fromJson() is redacted.
  • testObjectPropertyRedactionWithStdClassValues: nested and numeric-named properties of stdClass values are redacted, and the result stays a JSON object.
  • testRedactingEveryPropertyOfStdClassValueLeavesEmptyObject: redacting every property leaves {}.

The new tests fail without the fix (run against main's EventSerializer.php):

    Tests: 3, Assertions: 3, Failures: 3.
    Failed asserting that '[{...,"context":{"key":"user-key","kind":"user","b":{"prop1":true}}}]' contains ""b":{}".

With the fix:

    OK (3 tests, 5 assertions)

Additional context

Full suite on the CI PHP matrix following

PHP Dependencies Result
8.2 latest / lowest OK (Tests: 562, Assertions: 1504, Skipped: 21)
8.3 latest / lowest OK (Tests: 562, Assertions: 1504, Skipped: 21)
8.4 latest / lowest OK (Tests: 562, Assertions: 1504, Skipped: 21)

Note

Overview
Event serialization now redacts nested private attributes inside stdClass values (typical for contexts from LDContext::fromJson()), not only associative arrays. redactJsonValue treats stdClass and non-list arrays like JSON objects, coerces keys to strings for path matching, and returns (object)$ret so redacted structures still encode as {} objects—including numeric property names and fully redacted objects.

serializeEvents passes an explicit MAX_JSON_DEPTH (512) to json_encode, and redaction stops at the same depth to avoid infinite recursion on cycles and to align with encode failure behavior (empty payload when encode fails).

Adds broad EventSerializerTest coverage for JSON-parsed contexts, multi-kind contexts, lists vs objects, escaped path components, non-mutating redaction, cyclic/deep values, and numeric keys.

Reviewed by Cursor Bugbot for commit 0bfc9d9. Bugbot is set up for automated code reviews on this repo. Configure here.

A context that LDContext::fromJson() decodes from a string stores JSON
objects as stdClass. The event serializer only applied nested private
attribute redaction to associative arrays, so properties such as
/address/street were sent in analytics events. Redaction now applies to
stdClass values too, and a redacted object stays a JSON object.
@zigzagdev
zigzagdev requested a review from a team as a code owner November 21, 2026 05:45
@keelerm84

Copy link
Copy Markdown
Member

@zigzagdev just wanted to acknowledge that we have seen this PR. I haven't had a chance to dig into it yet, but will try to get to it this week if I'm able.

Build on the stdClass redaction fix:

- Treat an associative array with any numeric key as a JSON object, the
  same way json_encode does (array_is_list). Before, the first numeric key
  returned the array unredacted. If a key was already redacted, the event
  also listed it in _meta.redactedAttributes.
- Return redacted objects as objects, so an array that is a list after
  redaction still serializes as a JSON object.
- Stop redaction at the json_encode depth limit, so a value with a cycle
  makes json_encode fail instead of exhausting memory.
@keelerm84

Copy link
Copy Markdown
Member

Thanks for this fix, @zigzagdev. I used the maintainer-edit access to push one more commit to this branch (0bfc9d9). It builds on your change and leaves your commit and tests as they were.

The commit changes three things in redactJsonValue:

  • Associative arrays with numeric keys. An array such as ['123' => 'a', 'secret' => 'z'] still hit the early return $value and was sent without redaction. When the numeric key came after a redacted key, the event also listed that key in _meta.redactedAttributes. The serializer now uses the same rule as json_encode. An array is a list only if array_is_list() is true for it, and any other array is handled the same way as a stdClass.
  • Object shape after redaction. Redacted objects come back as objects in all cases. An array like ['secret' => 's', 0 => 'a'] would otherwise become [0 => 'a'] after redaction and serialize as a JSON list.
  • Depth limit. Walking stdClass values means a value that refers to itself ($o->self = $o) recursed until PHP ran out of memory. Before, json_encode failed on the cycle and the batch was dropped. Redaction now stops at the json_encode depth limit of 512, so that case fails the same way as before.

New tests cover each case, plus multi-kind contexts, escaped path components, lists, and parity between fromJson() with a JSON string and with an array. The full unit suite and the v2 contract tests pass.

A separate fix for how attribute names that start with / appear in redactedAttributes is in #309. It is not part of this PR.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants