Skip to content

Commit

Permalink
Merge pull request #140 from internetee/139-sanitize-url
Browse files Browse the repository at this point in the history
Implemented URL sanitization for logger
  • Loading branch information
vohmar authored Jan 26, 2024
2 parents c9e8236 + a648e67 commit c200fe4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 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
18 changes: 14 additions & 4 deletions server/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@ const logger = {
ignoreRoute,
meta: false,
msg: (req, res) => {
return `${req.method} ${req.protocol}://${req.get('host')}${req.originalUrl} (${
res.statusCode
}) ${Math.floor(res.responseTime / 1000)}, User-Agent: ${req.get(
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
if (typeof url === 'string') {
return url.replace(/[{}]/g, match => encodeURIComponent(match));
} else {
return '';
}
}

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

0 comments on commit c200fe4

Please sign in to comment.