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 all 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
35 changes: 34 additions & 1 deletion src/libs/Middleware/Logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ 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 serializeLoggingData<T extends Record<string, unknown> | undefined>(logData: T): T | null {
try {
return JSON.parse(JSON.stringify(logData, getCircularReplacer())) as T;
} catch (error) {
Log.hmmm('Failed to serialize log data', {error});
return null;
}
}

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 +66,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: serializeLoggingData(request.data),
};
extraData.response = response;
}

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

export default Logging;

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

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

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

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