Skip to content

Better logging of requests / reasons for 400 errors to help LPU#30

Merged
jmgasper merged 1 commit intomasterfrom
develop
Feb 9, 2026
Merged

Better logging of requests / reasons for 400 errors to help LPU#30
jmgasper merged 1 commit intomasterfrom
develop

Conversation

@jmgasper
Copy link
Contributor

@jmgasper jmgasper commented Feb 9, 2026

No description provided.

@jmgasper jmgasper merged commit ec18b71 into master Feb 9, 2026
6 checks passed

if (this.isCreateUserRequest(request.method, request.originalUrl)) {
this.logger.warn(
`400 Request Body: ${JSON.stringify(sanitizeForLogging(request.body))}`,
Copy link

Choose a reason for hiding this comment

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

[⚠️ correctness]
Consider handling potential JSON serialization errors when logging the request body. If request.body contains circular references or non-serializable values, JSON.stringify will throw an error.

if (typeof responseMessage === 'string') {
return responseMessage;
}
return JSON.stringify(sanitizeForLogging(responseMessage));
Copy link

Choose a reason for hiding this comment

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

[❗❗ security]
Ensure that sanitizeForLogging is robust against all types of input, especially since it is used to sanitize potentially sensitive data before logging. If responseMessage contains complex structures, ensure that the sanitizer handles them appropriately.


if (this.shouldLogCreateUserBody(method, originalUrl)) {
this.logger.debug(
`---> Request Body: ${JSON.stringify(sanitizeForLogging(req.body))}`,
Copy link

Choose a reason for hiding this comment

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

[⚠️ correctness]
Consider handling potential errors from JSON.stringify when logging the request body. If req.body contains circular references, JSON.stringify will throw an error, which could disrupt the logging process.

return false;
}

const pathOnly = originalUrl.split('?')[0];
Copy link

Choose a reason for hiding this comment

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

[⚠️ correctness]
The use of originalUrl.split('?')[0] to extract the path might not handle edge cases where the URL contains encoded characters or unusual structures. Consider using a more robust URL parsing method to ensure accuracy.

const REDACTED = '[REDACTED]';

function normalizeKey(key: string): string {
return key.toLowerCase().replace(/[^a-z0-9]/g, '');
Copy link

Choose a reason for hiding this comment

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

[⚠️ correctness]
The normalizeKey function replaces all non-alphanumeric characters with an empty string. Consider whether this might lead to unintended key collisions, especially if keys differ only by special characters.

);
}

function sanitizeValue(value: unknown, seen: WeakSet<object>): unknown {
Copy link

Choose a reason for hiding this comment

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

[⚠️ correctness]
The sanitizeValue function uses a WeakSet to track seen objects and prevent circular references. Ensure that all potential object types that might be logged are compatible with WeakSet, as it only accepts objects as keys.

return value;
}

if (Buffer.isBuffer(value)) {
Copy link

Choose a reason for hiding this comment

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

[⚠️ correctness]
The check for Buffer.isBuffer(value) assumes that the environment supports Node.js Buffers. If this code is intended to run in environments where Buffers are not available, consider adding a check for Buffer existence.

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.

1 participant