diff --git a/core/nano-server/src/nano-server.ts b/core/nano-server/src/nano-server.ts index cbd84c919..e0f24d802 100644 --- a/core/nano-server/src/nano-server.ts +++ b/core/nano-server/src/nano-server.ts @@ -594,4 +594,30 @@ export class AlwatrConnection { return parsedParams as T; } + + getRemoteAddress(): string { + return ( + this.incomingMessage.headers['x-forwarded-for'] + ?.split(',') + .pop() + ?.trim() || + this.incomingMessage.socket.remoteAddress || + 'unknown' + ); + } + + requireClientId(): string { + const clientId = this.incomingMessage.headers['client-id']; + + if (!clientId) { + // eslint-disable-next-line no-throw-literal + throw { + ok: false, + statusCode: 401, + errorCode: 'client_denied', + }; + } + + return clientId; + } } diff --git a/core/nano-server/src/type.ts b/core/nano-server/src/type.ts index 0d28ff54b..2b5923fc0 100644 --- a/core/nano-server/src/type.ts +++ b/core/nano-server/src/type.ts @@ -4,6 +4,8 @@ declare module 'http' { * Alwatr Client UUID */ 'client-id'?: string; + + 'x-forwarded-for'?: string; } } diff --git a/package.json b/package.json index f8919d84f..f9a9c3320 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "format": "run-s format:prettier format:eslint", "format:eslint": "yarn lint:ts --fix", "format:prettier": "prettier . --ignore-path .gitignore --write", - "clean": "git clean -d -x -f --exclude=node_modules --exclude=*.env", + "clean": "git clean -d -x -f --exclude=node_modules --exclude=*.env --exclude=_data", "serve": "wds", "watch": "run-p watch:* serve", "watch:ts": "yarn build:ts --watch --preserveWatchOutput", diff --git a/services/form-registration/src/route/put.ts b/services/form-registration/src/route/put.ts index 57324f0db..0bfd6d7b1 100644 --- a/services/form-registration/src/route/put.ts +++ b/services/form-registration/src/route/put.ts @@ -12,16 +12,8 @@ nanoServer.route('PUT', '/form/', async ( connection.requireToken(config.nanoServer.accessToken); const params = connection.requireQueryParams<{formId: string}>({formId: 'string'}); - const remoteAddress = connection.incomingMessage.socket.remoteAddress ?? 'unknown'; - const clientId = connection.incomingMessage.headers['client-id']; - - if (!clientId) { - return { - ok: false, - statusCode: 401, - errorCode: 'client_id_header_required', - }; - } + const clientId = connection.requireClientId(); + const remoteAddress = connection.getRemoteAddress(); if (config.formList.indexOf(params.formId) === -1) { return { diff --git a/uniquely/com-api/src/route/put-order.ts b/uniquely/com-api/src/route/put-order.ts index c9b4cdfed..19f8bb815 100644 --- a/uniquely/com-api/src/route/put-order.ts +++ b/uniquely/com-api/src/route/put-order.ts @@ -13,16 +13,8 @@ nanoServer.route('PUT', '/order/', async (connection) => { connection.requireToken((token: string) => { return tokenGenerator.verify(params.userId, token) === 'valid'; }); - const remoteAddress = connection.incomingMessage.socket.remoteAddress ?? 'unknown'; - const clientId = connection.incomingMessage.headers['client-id']; - - if (!clientId) { - return { - ok: false, - statusCode: 401, - errorCode: 'client_id_header_required', - }; - } + const remoteAddress = connection.getRemoteAddress(); + const clientId = connection.requireClientId(); const order = await connection.requireJsonBody();