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

Add requestLogger middleware #70

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Config = {
adaptiveRejection: boolean
filterDeadNodesFromArchiver: boolean
verbose: boolean
enableRequestLogger: boolean
firstLineLogs: boolean
verboseRequestWithRetry: boolean
verboseAALG: boolean
Expand Down Expand Up @@ -165,6 +166,7 @@ export const CONFIG: Config = {
adaptiveRejection: true,
filterDeadNodesFromArchiver: false,
verbose: false,
enableRequestLogger: true,
verboseRequestWithRetry: false,
verboseAALG: false,
firstLineLogs: true, // default is true and turn off for prod for perf
Expand Down
86 changes: 86 additions & 0 deletions src/middlewares/requestLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Request, Response, NextFunction } from 'express'
import { CONFIG as config } from '../config'

const requestLogger = (req: Request, res: Response, next: NextFunction): void => {
if (config.enableRequestLogger) {
const reqTime = Date.now()
const senderIp = req.ip
const userAgent = req.headers['user-agent'] || 'Unknown'

const responseChunks: Buffer[] = []

const originalWrite = res.write.bind(res)
res.write = function (
chunk: any,
encodingOrCallback?: BufferEncoding | ((error: Error | null | undefined) => void),
callback?: (error: Error | null | undefined) => void
) {
responseChunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))

if (typeof encodingOrCallback === 'function') {
return originalWrite(chunk, encodingOrCallback)
} else {
return originalWrite(chunk, encodingOrCallback as BufferEncoding, callback)
}
}

const originalEnd = res.end.bind(res)
res.end = function (chunk?: any, ...args: any[]) {
const responseBody = Buffer.concat(responseChunks).toString('utf8')
res.locals.responseBody = responseBody
return originalEnd(chunk, ...args)
}

const originalJson = res.json.bind(res)
res.json = function (body) {
res.locals.responseBody = body
return originalJson(body)
}

const originalSend = res.send.bind(res)
res.send = function (body) {
res.locals.responseBody = body
return originalSend(body)
}

res.on('finish', () => {
const resTime = Date.now()

console.log(
`Request URL: ${req.originalUrl} ||` +
` Response Status Code: ${res.statusCode} ||` +
` Sender IP: ${senderIp} ||` +
` Request Timestamp: ${new Date(reqTime).toISOString()} ||` +
` Response Timestamp: ${new Date(resTime).toISOString()} ||` +
` Request Method: ${req.method} ||` +
` Response Time: ${resTime - reqTime}ms ||` +
` User Agent: ${userAgent}`
Comment on lines +50 to +57

Check warning

Code scanning / CodeQL

Log injection Medium

Log entry depends on a
user-provided value
.
Log entry depends on a
user-provided value
.

Copilot Autofix AI 4 months ago

To fix the log injection issue, we need to sanitize the userAgent value before logging it. Specifically, we should remove any newline characters from the userAgent string to prevent log injection attacks. This can be done using the String.prototype.replace method to remove any newline characters (\n and \r).

Suggested changeset 1
src/middlewares/requestLogger.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/middlewares/requestLogger.ts b/src/middlewares/requestLogger.ts
--- a/src/middlewares/requestLogger.ts
+++ b/src/middlewares/requestLogger.ts
@@ -7,3 +7,4 @@
     const senderIp = req.ip
-    const userAgent = req.headers['user-agent'] || 'Unknown'
+    let userAgent = req.headers['user-agent'] || 'Unknown'
+    userAgent = userAgent.replace(/\n|\r/g, "")
 
EOF
@@ -7,3 +7,4 @@
const senderIp = req.ip
const userAgent = req.headers['user-agent'] || 'Unknown'
let userAgent = req.headers['user-agent'] || 'Unknown'
userAgent = userAgent.replace(/\n|\r/g, "")

Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
)

const responseBody = res.locals.responseBody
if (res.statusCode !== 200) {
console.log(
`Request Failed with ${res.statusCode} ||` +
`Request Body: ${JSON.stringify(req.body)} ||` +
` Response Body: ${res.locals.responseBody}`
)
} else if (responseBody) {
try {
const parsedBody = JSON.parse(responseBody)
if (parsedBody && 'error' in parsedBody) {
console.log(
`RPC Request Failed with Error ||` +
` Request Body: ${JSON.stringify(req.body)} ||` +
` Response Body: ${res.locals.responseBody}`
)
}
} catch (e) {
// Silently fail if parsing fails, no logging here
}
}
})
}
next()
}

export default requestLogger
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { nestedCountersInstance } from './utils/nestedCounters'
import { methodWhitelist } from './middlewares/methodWhitelist'
import { isDebugModeMiddlewareLow, rateLimitedDebugAuth } from './middlewares/debugMiddleware'
import { isIPv4 } from 'net';
import requestLogger from './middlewares/requestLogger'

setDefaultResultOrder('ipv4first')

Expand Down Expand Up @@ -90,6 +91,7 @@ app.set('trust proxy', false)
app.use(cors({ methods: ['POST'] }))
app.use(express.json())
app.use(cookieParser())
app.use(requestLogger)
app.use(function (req, res, next) {
res.setHeader('X-Content-Type-Options', 'nosniff')
res.setHeader(
Expand Down
Loading