Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redaction of objects with circular structure #19834

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-wasps-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@directus/api": patch
---

Fixed redaction of objects with circular structure
18 changes: 18 additions & 0 deletions api/src/utils/redact-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ describe('getReplacer tests', () => {
}
});

test('Correctly parses object with circular structure when used with JSON.stringify()', () => {
const obj: Record<string, any> = {
a: 'foo',
};

obj['b'] = obj;
obj['c'] = { obj };

const expectedResult = {
a: 'foo',
c: {},
};

const result = JSON.parse(JSON.stringify(obj, getReplacer(getRedactedString)));

expect(result).toStrictEqual(expectedResult);
});

test('Correctly parses error object when used with JSON.stringify()', () => {
const errorMessage = 'Error Message';
const errorCause = 'Error Cause';
Expand Down
11 changes: 11 additions & 0 deletions api/src/utils/redact-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,18 @@ export function getReplacer(replacement: Replacement, values?: Values) {
? Object.entries(values).filter(([_k, v]) => typeof v === 'string' && v.length > 0)
: [];

const seen = new WeakSet();

return (_key: string, value: unknown) => {
// Skip circular values
paescuj marked this conversation as resolved.
Show resolved Hide resolved
if (typeof value === 'object' && value !== null) {
paescuj marked this conversation as resolved.
Show resolved Hide resolved
if (seen.has(value)) {
return;
}

seen.add(value);
}

if (value instanceof Error) {
return {
name: value.name,
Expand Down