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: serialize request.data in logging middleware #47778

Merged
merged 7 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
26 changes: 25 additions & 1 deletion src/libs/Middleware/Logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ import type Request from '@src/types/onyx/Request';
import type Response from '@src/types/onyx/Response';
import type Middleware from './types';

function getCircularReplacer() {
const ancestors: unknown[] = [];
return function (this: unknown, key: string, value: unknown): unknown {
if (typeof value !== 'object' || value === null) {
return value;
}
// `this` is the object that value is contained in, i.e the direct parent
// eslint-disable-next-line no-invalid-this
while (ancestors.length > 0 && ancestors.at(-1) !== this) {
ancestors.pop();
}
if (ancestors.includes(value)) {
return '[Circular]';
}
ancestors.push(value);
return value;
};
}

function logRequestDetails(message: string, request: Request, response?: Response | void) {
// Don't log about log or else we'd cause an infinite loop
if (request.command === 'Log') {
Expand Down Expand Up @@ -38,7 +57,10 @@ function logRequestDetails(message: string, request: Request, response?: Respons
* requests because they contain sensitive information.
*/
if (request.command !== 'AuthenticatePusher') {
extraData.request = request;
extraData.request = {
...request,
data: JSON.parse(JSON.stringify(request.data, getCircularReplacer())) as Record<string, unknown>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: Nice to have, we can move this logic into the function serializeLoggingValue.

};
extraData.response = response;
}

Expand Down Expand Up @@ -125,3 +147,5 @@ const Logging: Middleware = (response, request) => {
};

export default Logging;

export {getCircularReplacer};
31 changes: 31 additions & 0 deletions tests/unit/LoggingMiddlewareTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {getCircularReplacer} from '@libs/Middleware/Logging';

describe('LoggingMiddleware', () => {
describe('getCircularReplacer', () => {
it('should return "[Circular]" for circular references', () => {
const circularReplacer = getCircularReplacer();
const obj: Record<string, unknown> = {};
obj.obj = obj;
const result = JSON.stringify(obj, circularReplacer);
expect(result).toBe('{"obj":"[Circular]"}');
});

it('should return the original value for non-circular references', () => {
const circularReplacer = getCircularReplacer();
const obj: Record<string, unknown> = {};
obj.foo = 'bar';
const result = JSON.stringify(obj, circularReplacer);
expect(result).toBe('{"foo":"bar"}');
});

it('should not stringify function in the object', () => {
const circularReplacer = getCircularReplacer();
const obj: Record<string, unknown> = {
foo: () => 'bar',
baz: 'baz',
};
const result = JSON.stringify(obj, circularReplacer);
expect(result).toBe('{"baz":"baz"}');
});
});
});
Loading