diff --git a/package.json b/package.json index 83482454..ad1be8a7 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "license": "MIT", "dependencies": { + "@supercharge/request-ip": "^1.1.2", "@svgr/webpack": "^5.5.0", "ace-builds": "^1.4.12", "axios": "^0.21.1", diff --git a/server/index.ts b/server/index.ts index 6733af87..8ef6d6ed 100644 --- a/server/index.ts +++ b/server/index.ts @@ -22,6 +22,7 @@ import { getAppVersion } from './utils/appVersion'; import SlackAgent from './lib/notifications/agents/slack'; import PushoverAgent from './lib/notifications/agents/pushover'; import WebhookAgent from './lib/notifications/agents/webhook'; +import { getClientIp } from '@supercharge/request-ip'; const API_SPEC_PATH = path.join(__dirname, '../overseerr-api.yml'); @@ -62,6 +63,21 @@ app server.use(cookieParser()); server.use(bodyParser.json()); server.use(bodyParser.urlencoded({ extended: true })); + server.use((req, res, next) => { + try { + const descriptor = Object.getOwnPropertyDescriptor(req, 'ip'); + if (descriptor?.writable === true) { + req.ip = getClientIp(req) ?? ''; + } + } catch (e) { + logger.error('Failed to attach the ip to the request', { + label: 'Middleware', + message: e.message, + }); + } finally { + next(); + } + }); // Setup sessions const sessionRespository = getRepository(Session); diff --git a/server/routes/auth.ts b/server/routes/auth.ts index 5f60d512..8c2a90fd 100644 --- a/server/routes/auth.ts +++ b/server/routes/auth.ts @@ -151,6 +151,7 @@ authRoutes.post('/local', async (req, res, next) => { logger.info('Failed login attempt from user with incorrect credentials', { label: 'Auth', account: { + ip: req.ip, email: body.email, password: '__REDACTED__', }, diff --git a/yarn.lock b/yarn.lock index bd516764..af34f09f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1806,6 +1806,11 @@ resolved "https://registry.yarnpkg.com/@sqltools/formatter/-/formatter-1.2.2.tgz#9390a8127c0dcba61ebd7fdcc748655e191bdd68" integrity sha512-/5O7Fq6Vnv8L6ucmPjaWbVG1XkP4FO+w5glqfkIsq3Xw4oyNAdJddbnYodNDAfjVUvo/rrSCTom4kAND7T1o5Q== +"@supercharge/request-ip@^1.1.2": + version "1.1.2" + resolved "https://registry.yarnpkg.com/@supercharge/request-ip/-/request-ip-1.1.2.tgz#be9083aa50d3c6fc200f3ed5919e0b9c13fc8842" + integrity sha512-mtryG/uiSIVT0ga8A/F9hNEuBSbDxW7/m9PEtxIHqdkE/vr766m17mtLPhrXA4q4T+Qw44+33mg3Rtkl7o+OvQ== + "@svgr/babel-plugin-add-jsx-attribute@^5.4.0": version "5.4.0" resolved "https://registry.yarnpkg.com/@svgr/babel-plugin-add-jsx-attribute/-/babel-plugin-add-jsx-attribute-5.4.0.tgz#81ef61947bb268eb9d50523446f9c638fb355906"