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 5 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
30 changes: 29 additions & 1 deletion src/libs/Middleware/Logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ 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(logData?: Record<string, unknown> | null): Record<string, unknown> | undefined | null {
Copy link
Contributor

@suneox suneox Aug 30, 2024

Choose a reason for hiding this comment

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

Nice to have support to keep the data type of object after serialization

Suggested change
function serializeLoggingData(logData?: Record<string, unknown> | null): Record<string, unknown> | undefined | null {
function serializeLoggingData<T extends Record<string, unknown>>(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;
}
}

Copy link
Contributor

@suneox suneox Aug 30, 2024

Choose a reason for hiding this comment

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

Hi @dominictb i've updated suggestion

Copy link
Contributor

Choose a reason for hiding this comment

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

Are we going to implement this suggestion?

Copy link
Contributor

Choose a reason for hiding this comment

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

@dominictb have you got any feedback on this suggestion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking into this now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@suneox if we fail to serialize the data, shall we return dummy value like [Not serializable] or something? If we return the original data I'm afraid we could get into the original freeze issue again.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok I got it, so if fail to serialize data we can return null instead of original data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, I'll update and let you know.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

return logData && (JSON.parse(JSON.stringify(logData, getCircularReplacer())) as Record<string, unknown>);
}

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 +61,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 +151,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