Skip to content

Commit

Permalink
fix: useragent and ip detection in password express endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Nov 23, 2023
1 parent 9270f87 commit 3bd3fb8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 3 additions & 1 deletion packages/password/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@
"dependencies": {
"@accounts/two-factor": "^0.32.4",
"bcryptjs": "2.4.3",
"express-validator": "^7.0.1"
"express-validator": "^7.0.1",
"request-ip": "3.3.0"
},
"devDependencies": {
"@accounts/server": "^0.33.1",
"@accounts/types": "^0.33.1",
"@types/bcryptjs": "2.4.6",
"@types/express": "^4.17.21",
"@types/lodash.set": "4.3.9",
"@types/request-ip": "0.0.41",
"graphql": "16.8.1",
"graphql-modules": "3.0.0-alpha-20231106133212-0b04b56e",
"lodash.set": "4.3.2",
Expand Down
16 changes: 12 additions & 4 deletions packages/password/src/endpoints/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type Injector } from 'graphql-modules';
import type { Request, Response, NextFunction } from 'express';
import AccountsPassword from '../accounts-password';
import { body, matchedData, param, validationResult } from 'express-validator';
import { getClientIp } from 'request-ip';

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

const getUserAgent = (req: Request) => {
let userAgent: string = (req.headers['user-agent'] as string) || '';
if (req.headers['x-ucbrowser-ua']) {
// special case of UC Browser
userAgent = req.headers['x-ucbrowser-ua'] as string;
}
return userAgent;
};

function getHtml(title: string, body: string) {
return `
<!DOCTYPE html>
Expand All @@ -30,11 +40,9 @@ function getHtml(title: string, body: string) {
}

export const infosMiddleware = (req: Request, _res: Response, next: NextFunction) => {
const userAgent = 'userAgent';
const ip = 'ip';
req.infos = {
userAgent,
ip,
userAgent: getUserAgent(req),
ip: getClientIp(req) ?? req.ip,
};
next();
};
Expand Down
6 changes: 2 additions & 4 deletions packages/rest-express/src/express-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ const accountsExpress = (
* Middleware to populate the user agent and ip.
*/
router.use((req, _, next) => {
const userAgent = getUserAgent(req);
const ip = getClientIp(req)!;
req.infos = {
userAgent,
ip,
userAgent: getUserAgent(req),
ip: getClientIp(req) ?? req.ip,
};

next();
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,14 @@ __metadata:
"@types/bcryptjs": "npm:2.4.6"
"@types/express": "npm:^4.17.21"
"@types/lodash.set": "npm:4.3.9"
"@types/request-ip": "npm:0.0.41"
bcryptjs: "npm:2.4.3"
express-validator: "npm:^7.0.1"
graphql: "npm:16.8.1"
graphql-modules: "npm:3.0.0-alpha-20231106133212-0b04b56e"
lodash.set: "npm:4.3.2"
reflect-metadata: "npm:0.1.13"
request-ip: "npm:3.3.0"
peerDependencies:
"@accounts/server": ^0.33.0
graphql: ^14.0.0 || ^15.0.0 || ^16.0.0
Expand Down

0 comments on commit 3bd3fb8

Please sign in to comment.