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

feat: log http POST requests body in http-access log #3214

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 21 additions & 1 deletion packages/cli/src/daemon/log-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Request, Response } from 'express'
import morgan from 'morgan'

const ACCESS_LOG_FMT =
'ip=:remote-addr ts=:date[iso] method=:method original_url=:original-url base_url=:base-url path=:path:params http_version=:http-version req_header:req[header] status=:status content_length=:res[content-length] content_type=":res[content-type]" ref=:referrer user_agent=":user-agent" elapsed_ms=:total-time[3] error_message=":error-message" error_code=:error-code'
'ip=:remote-addr ts=:date[iso] method=:method original_url=:original-url base_url=:base-url path=:path:params :body http_version=:http-version req_header:req[header] status=:status content_length=:res[content-length] content_type=":res[content-type]" ref=:referrer user_agent=":user-agent" elapsed_ms=:total-time[3] error_message=":error-message" error_code=:error-code'

export function logRequests(loggerProvider: LoggerProvider): any[] {
morgan.token<Request, Response>('error-message', (req, res: Response) => {
Expand Down Expand Up @@ -33,6 +33,26 @@ export function logRequests(loggerProvider: LoggerProvider): any[] {
}
return ' params=-'
})
morgan.token<Request, Response>('body', (req) => {
if (req.body) {
const keys = Object.keys(req.body)
if (keys.length > 0) {
const body = keys.reduce((prev, curr) => {
const value = req.body[curr]
const valKeys = Object.keys(value)
if (valKeys.length > 0) {
// value is an object
return prev + ` body.${curr}=${JSON.stringify(value)}`
Copy link
Contributor

Choose a reason for hiding this comment

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

is this going to be extremely long? agree we want it tho

Copy link
Contributor Author

@stbrody stbrody Apr 23, 2024

Choose a reason for hiding this comment

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

does seem like a risk, yes. This is printing the full body of the commit being sent to js-ceramic, so is users are creating large json documents in their app, this will print the full document, which could be up to 256kb large

Copy link
Contributor

Choose a reason for hiding this comment

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

um if we do this for every single request on gitcoin i definitely think its a risk of crashing them by filling up the logs. i would like to sample them

} else {
// value is a scalar
return prev + ` body.${curr}=${value}`
}
}, '')
return body
}
}
return ' body=-'
})

const logger = loggerProvider.makeServiceLogger('http-access')

Expand Down