Skip to content

Commit

Permalink
fix(plugin-koa|express|restify): Ensure clientIp and referer are prop…
Browse files Browse the repository at this point in the history
…erly collected

Fixes #615.
  • Loading branch information
bengourley committed Sep 5, 2019
1 parent 86b60fa commit 874f52c
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/plugin-express/src/request-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ module.exports = req => {
request.query = extractObject(req, 'query')
request.body = extractObject(req, 'body')

request.clientIp = req.ip || (connection ? connection.remoteAddress : undefined)
request.referer = req.headers.referer || req.headers.referrer

if (connection) {
request.connection = {
remoteAddress: connection.remoteAddress || req.ip,
remoteAddress: connection.remoteAddress,
remotePort: connection.remotePort,
bytesRead: connection.bytesRead,
bytesWritten: connection.bytesWritten,
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-koa/src/request-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module.exports = ctx => {
headers: request.headers,
httpVersion: request.httpVersion,
query: ctx.request.query,
referer: request.headers.referer,
clientIp: ctx.ip,
connection: request.connection ? {
remoteAddress: request.connection.remoteAddress || request.ip,
remotePort: request.connection.remotePort,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-koa/test/koa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('plugin: koa', () => {
c.use(plugin)
const middleware = c.getPlugin('koa')
const mockCtx = {
req: { connection: { address: () => ({ port: 1234 }) } },
req: { connection: { address: () => ({ port: 1234 }) }, headers: {} },
request: { query: {} },
res: {},
response: { headerSent: false },
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin-restify/src/request-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ module.exports = req => {
request.query = extractObject(req, 'query')
request.body = extractObject(req, 'body')

request.clientIp = req.headers['x-forwarded-for'] || (connection ? connection.remoteAddress : undefined)
request.referer = req.headers.referer || req.headers.referrer

if (connection) {
request.connection = {
remoteAddress: connection.remoteAddress || req.ip,
remoteAddress: connection.remoteAddress,
remotePort: connection.remotePort,
bytesRead: connection.bytesRead,
bytesWritten: connection.bytesWritten,
Expand Down
1 change: 1 addition & 0 deletions test/node/features/express.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Scenario: a synchronous thrown error in a route
And the "file" of stack frame 0 equals "scenarios/app.js"
And the event "request.url" equals "http://express/sync"
And the event "request.httpMethod" equals "GET"
And the event "request.clientIp" is not null

Scenario: an asynchronous thrown error in a route
Then I open the URL "http://express/async"
Expand Down
1 change: 1 addition & 0 deletions test/node/features/koa.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Scenario: a synchronous thrown error in a route
And the "file" of stack frame 0 equals "scenarios/app.js"
And the event "request.url" equals "http://koa/err"
And the event "request.httpMethod" equals "GET"
And the event "request.clientIp" is not null

Scenario: an asynchronous thrown error in a route
Then I open the URL "http://koa/async-err"
Expand Down
3 changes: 2 additions & 1 deletion test/node/features/restify.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Scenario: a synchronous thrown error in a route
And the "file" of stack frame 0 equals "scenarios/app.js"
And the event "request.url" equals "http://restify/sync"
And the event "request.httpMethod" equals "GET"
And the event "request.clientIp" is not null

Scenario: an asynchronous thrown error in a route
Then I open the URL "http://restify/async"
Expand Down Expand Up @@ -72,4 +73,4 @@ Scenario: an explicit internal server error
And the exception "errorClass" equals "InternalServerError"
And the exception "message" equals "oh noes!"
And the exception "type" equals "nodejs"
And the "file" of stack frame 0 equals "scenarios/app.js"
And the "file" of stack frame 0 equals "scenarios/app.js"

0 comments on commit 874f52c

Please sign in to comment.