Skip to content

Commit

Permalink
Implemented URL sanitization for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergei Tsoganov authored and Sergei Tsoganov committed Jan 25, 2024
1 parent c9e8236 commit 83d7336
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion server/routes/apiRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,13 @@ export default {
if (e && e.response && e.response.status) {
return res.status(e.response.status).json({});
}
return res.status(408).json({});
// return res.status(408).json({});
if (!res.headersSent) {
return res.status(408).json({});
} else {
// Log or handle the situation where a response was already sent
console.error('Response already sent.');
}
}
},

Expand Down
10 changes: 8 additions & 2 deletions server/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ const logger = {
ignoreRoute,
meta: false,
msg: (req, res) => {
return `${req.method} ${req.protocol}://${req.get('host')}${req.originalUrl} (${
return `${req.method} ${req.protocol}://${req.get('host')}${sanitizeUrl(req.originalUrl)} (${
res.statusCode
}) ${Math.floor(res.responseTime / 1000)}, User-Agent: ${req.get(
'User-Agent'
)}, Referrer: ${req.get('Referrer')}, IP: ${
)}, Referrer: ${sanitizeUrl(req.get('Referrer'))}, IP: ${
req.ip.indexOf(':') >= 0 ? req.ip.substring(req.ip.lastIndexOf(':') + 1) : req.i
}`;
},
};

function sanitizeUrl(url) {
// Implement URL sanitization logic here
// For example, removing or encoding certain characters
return url.replace(/[{}]/g, encodeURIComponent);
}

export const accessLog = {
...logger,
transports: [
Expand Down

0 comments on commit 83d7336

Please sign in to comment.