Skip to content

Commit 3bd3fb8

Browse files
committed
fix: useragent and ip detection in password express endpoint
1 parent 9270f87 commit 3bd3fb8

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

packages/password/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@
2929
"dependencies": {
3030
"@accounts/two-factor": "^0.32.4",
3131
"bcryptjs": "2.4.3",
32-
"express-validator": "^7.0.1"
32+
"express-validator": "^7.0.1",
33+
"request-ip": "3.3.0"
3334
},
3435
"devDependencies": {
3536
"@accounts/server": "^0.33.1",
3637
"@accounts/types": "^0.33.1",
3738
"@types/bcryptjs": "2.4.6",
3839
"@types/express": "^4.17.21",
3940
"@types/lodash.set": "4.3.9",
41+
"@types/request-ip": "0.0.41",
4042
"graphql": "16.8.1",
4143
"graphql-modules": "3.0.0-alpha-20231106133212-0b04b56e",
4244
"lodash.set": "4.3.2",

packages/password/src/endpoints/express.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type Injector } from 'graphql-modules';
22
import type { Request, Response, NextFunction } from 'express';
33
import AccountsPassword from '../accounts-password';
44
import { body, matchedData, param, validationResult } from 'express-validator';
5+
import { getClientIp } from 'request-ip';
56

67
function matchOrThrow<T extends Record<string, any> = Record<string, any>>(
78
...args: Parameters<typeof matchedData>
@@ -12,6 +13,15 @@ function matchOrThrow<T extends Record<string, any> = Record<string, any>>(
1213
return matchedData(...args) as T;
1314
}
1415

16+
const getUserAgent = (req: Request) => {
17+
let userAgent: string = (req.headers['user-agent'] as string) || '';
18+
if (req.headers['x-ucbrowser-ua']) {
19+
// special case of UC Browser
20+
userAgent = req.headers['x-ucbrowser-ua'] as string;
21+
}
22+
return userAgent;
23+
};
24+
1525
function getHtml(title: string, body: string) {
1626
return `
1727
<!DOCTYPE html>
@@ -30,11 +40,9 @@ function getHtml(title: string, body: string) {
3040
}
3141

3242
export const infosMiddleware = (req: Request, _res: Response, next: NextFunction) => {
33-
const userAgent = 'userAgent';
34-
const ip = 'ip';
3543
req.infos = {
36-
userAgent,
37-
ip,
44+
userAgent: getUserAgent(req),
45+
ip: getClientIp(req) ?? req.ip,
3846
};
3947
next();
4048
};

packages/rest-express/src/express-middleware.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ const accountsExpress = (
4141
* Middleware to populate the user agent and ip.
4242
*/
4343
router.use((req, _, next) => {
44-
const userAgent = getUserAgent(req);
45-
const ip = getClientIp(req)!;
4644
req.infos = {
47-
userAgent,
48-
ip,
45+
userAgent: getUserAgent(req),
46+
ip: getClientIp(req) ?? req.ip,
4947
};
5048

5149
next();

yarn.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,14 @@ __metadata:
412412
"@types/bcryptjs": "npm:2.4.6"
413413
"@types/express": "npm:^4.17.21"
414414
"@types/lodash.set": "npm:4.3.9"
415+
"@types/request-ip": "npm:0.0.41"
415416
bcryptjs: "npm:2.4.3"
416417
express-validator: "npm:^7.0.1"
417418
graphql: "npm:16.8.1"
418419
graphql-modules: "npm:3.0.0-alpha-20231106133212-0b04b56e"
419420
lodash.set: "npm:4.3.2"
420421
reflect-metadata: "npm:0.1.13"
422+
request-ip: "npm:3.3.0"
421423
peerDependencies:
422424
"@accounts/server": ^0.33.0
423425
graphql: ^14.0.0 || ^15.0.0 || ^16.0.0

0 commit comments

Comments
 (0)