diff --git a/api/src/routes/public/status.ts b/api/src/routes/public/status.ts index 3774e0a359f..0dbca24ad5b 100644 --- a/api/src/routes/public/status.ts +++ b/api/src/routes/public/status.ts @@ -1,5 +1,4 @@ import { type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox'; -import { isEmpty } from 'lodash'; /** * Plugin for the health check endpoint. @@ -15,25 +14,7 @@ export const statusRoute: FastifyPluginCallbackTypebox = ( done ) => { fastify.get('/status/ping', async (req, _reply) => { - const url = req.url || 'URL not found'; - const reqId = req.id || 'REQ_ID not found'; - const headers = isEmpty(req.headers) ? 'HEADERS not found' : req.headers; - const ip = - req.headers['x-forwarded-for'] || - req.headers['x-real-ip'] || - req.ip || - 'IP not found'; - const query = isEmpty(req.query) ? 'QUERY not found' : req.query; - - fastify.log - .child({ - URL: url, - REQ_ID: reqId, - HEADERS: headers, - IP: ip, - QUERY: query - }) - .debug('returning a ping'); + fastify.log.child({ req }).debug('returning a ping'); return { msg: 'pong' }; }); diff --git a/api/src/server.ts b/api/src/server.ts index 6d3b3ecf87f..ad53fbda20a 100644 --- a/api/src/server.ts +++ b/api/src/server.ts @@ -3,8 +3,12 @@ // is not included in the build (it's a dev dependency). // eslint-disable-next-line @typescript-eslint/triple-slash-reference /// + import { randomBytes } from 'crypto'; + import { FastifyRequest } from 'fastify'; +import { isEmpty } from 'lodash'; + import { build } from './app'; import { FREECODECAMP_NODE_ENV, @@ -13,16 +17,30 @@ import { PORT } from './utils/env'; -const requestSerializer = (request: FastifyRequest) => ({ - method: request.method, - url: request.url, - ip: request.headers['x-forwarded-for'] || request.ip, - hostname: request.hostname, - remoteAddress: Array.isArray(request.headers['x-forwarded-for']) - ? request.headers['x-forwarded-for'][0] - : request.headers['x-forwarded-for'] || request.ip, - remotePort: request.socket.remotePort -}); +const requestSerializer = (req: FastifyRequest) => { + const method = req.method || 'METHOD not found'; + const url = req.url || 'URL not found'; + const headers = req.headers || 'HEADERS not found'; + const xForwardedFor = Array.isArray(req.headers['x-forwarded-for']) + ? req.headers['x-forwarded-for'][0] + : req.headers['x-forwarded-for']; + const ip = + xForwardedFor || req.headers['x-real-ip'] || req.ip || 'IP not found'; + const query = isEmpty(req.query) ? 'QUERY not found' : req.query; + const hostname = req.hostname || 'HOSTNAME not found'; + const remotePort = req.socket.remotePort || 'REMOTE_PORT not found'; + + return { + REQ_ID: req.id, + METHOD: method, + URL: url, + IP: ip, + HOSTNAME: hostname, + REMOTE_PORT: remotePort, + QUERY: query, + HEADERS: headers + }; +}; const envToLogger = { development: { @@ -36,14 +54,21 @@ const envToLogger = { }, level: FCC_API_LOG_LEVEL || 'info', serializers: { - req: requestSerializer + req: (req: FastifyRequest) => { + return { + method: req.method, + url: req.url + }; + } } + // No need to redact in development }, production: { level: FCC_API_LOG_LEVEL || 'info', serializers: { req: requestSerializer - } + }, + redact: ['req.HEADERS.cookie'] } };