Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOFF-814 - Capture and add user's ip address to the email to caseworker #272

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
13 changes: 12 additions & 1 deletion apps/rotm/behaviours/caseworker-email.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict';

const hof = require('hof');
Expand All @@ -7,6 +8,15 @@ const moment = require('moment');
const config = require('../../../config');
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, json } = format;
const http = require('http');

let ipAddress = '';
http.get({host: 'api.ipify.org', port: 80, path: '/'}, function (resp) {
resp.on('data', function (ip) {
ipAddress = String.fromCharCode(...ip);
console.log('api.ipify.org ip address : ' + ip); console.log('ipaddress : ' + ipAddress);
});
});

const logger = createLogger({
format: combine(timestamp(), json()),
Expand All @@ -26,7 +36,7 @@ const parse = (model, translate) => {

logger.log({
level: 'info',
message: `Submission ID: ${model.submissionID}, Email Submitted: ${submissionDateTime}`
message: `Submission ID: ${model.submissionID}, Email Submitted: ${submissionDateTime}, IP Address: ${model.ip}`
});

return {
Expand All @@ -37,6 +47,7 @@ const parse = (model, translate) => {
table: [
{label: getLabel('uniqueId'), value: model.submissionID},
{label: getLabel('submitted'), value: submissionDateTime},
{label: getLabel('ipaddress'), value: model.ip},
...fields.map(f => ({
label: getLabel(f),
value: model[f]
Expand Down
13 changes: 12 additions & 1 deletion apps/rotm/behaviours/url-repeater.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict';

const uuid = require('uuid');
Expand All @@ -12,13 +13,23 @@ module.exports = superclass => class extends superclass {
req.form.values['another-url-4']
].filter(Boolean);
const submissionID = req.sessionModel.get('submissionID');
req.log('info', `Submission ID: ${submissionID}, Saving Urls: ${req.form.values.urls}`);
const ip = req.sessionModel.get('ip');

req.log('info', `Submission ID: ${submissionID}, Saving Urls: ${req.form.values.urls}, IP Address: ${ip}`);
return super.saveValues(req, res, next);
}

getValues(req, res, next) {
super.getValues(req, res, (err, values) => {
const submissionID = req.sessionModel.get('submissionID') || uuid.v4();
console.log('x-forwarded-for : ' + req.headers['x-forwarded-for']);
console.log('remoteAddress: ' + req.connection.remoteAddress);
console.log('x-Real-IP: ' + req.header('x-Real-IP'));
console.log('req.ip ' + req.ip);
const ip = (req.headers['x-forwarded-for'] || req.connection.remoteAddress || '').split(',')[0].trim();
// eslint-disable-next-line no-console
console.log('ipaddress : ' + ip);
req.sessionModel.set('ip', ip);
req.sessionModel.set('submissionID', submissionID);
const urls = req.sessionModel.get('urls') || [];
req.log('info', `Submission ID: ${submissionID}, Saved Urls: ${urls}`);
Expand Down
6 changes: 3 additions & 3 deletions apps/rotm/translations/src/en/email.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
"submitted": {
"label": "Date and time of submission"
},
"ipaddress": {
"label": "User ip address"
},
"url": {
"label": "Link"
},
"evidence-written": {
"label": "Additional details"
},
"url": {
"label": "Link"
},
"contact-details-name": {
"label": "Full name"
},
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ services:
- PROXY_SERVICE_HOST=app
- PROXY_SERVICE_PORT=8080
- NAXSI_USE_DEFAULT_RULES=FALSE
- ADD_NGINX_SERVER_CFG=add_header Cache-Control private;add_header X-Frame-Options "SAMEORIGIN" always;add_header X-Content-Type-Options "nosniff" always;add_header X-XSS-Protection "1; mode=block" always;location /public {add_header Cache-Control max-age=86400;add_header X-Frame-Options "SAMEORIGIN" always;add_header X-Content-Type-Options "nosniff" always;add_header X-XSS-Protection "1; mode=block" always;alias /public;}
- ADD_NGINX_SERVER_CFG=add_header Cache-Control private;add_header X-Frame-Options "SAMEORIGIN" always; location /public { proxy_pass http://127.0.0.1::8080; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; }; add_header X-Content-Type-Options "nosniff" always;add_header X-XSS-Protection "1; mode=block" always;location /public {add_header Cache-Control max-age=86400;add_header X-Frame-Options "SAMEORIGIN" always;add_header X-Content-Type-Options "nosniff" always;add_header X-XSS-Protection "1; mode=block" always;alias /public;}
- ERROR_REDIRECT_CODES=599
ports:
- "443:443"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"bugs": "https://github.com/UKHomeOffice/rotm/issues",
"scripts": {
"start": "node server.js",
"start:dev": "hof-build watch",
"dev": "hof-build watch --env",
"start:dev": "hof-build watch --env",
"test": "yarn run test:lint",
"test:lint": "eslint . --config ./node_modules/eslint-config-hof/default.js",
"test:unit": "mocha \"test/test.setup.js\" \"test/_unit/**/*.spec.js\"",
Expand All @@ -36,6 +35,7 @@
"jimp": "^0.16.1",
"jpeg-js": "^0.4.4",
"lodash": "^4.17.21",
"mocha": "^10.6.0",
"moment": "^2.29.4",
"proxyquire": "^2.1.3",
"tar": "^6.2.1",
Expand Down
12 changes: 12 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const config = require('./config');
const mockAPIs = require('./mock-apis');
const bodyParser = require('busboy-body-parser');

const express = require('express');
const exp = express();

exp.set('trust proxy', true);


if (process.env.REDIS_URL) {
config.redis = process.env.REDIS_URL;
}
Expand Down Expand Up @@ -35,6 +41,7 @@ settings = Object.assign({}, settings, {

const app = hof(settings);


// Terms & Cookies added to have visibility on accessibility statement
// in the footer. Once HOF has updated with that we can remove these
// including the getTerms: false, getCookies: false config and common directory
Expand All @@ -57,6 +64,11 @@ if (config.useMocks) {
}

app.use((req, res, next) => {
const ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress;
// eslint-disable-next-line no-console
console.log('ipaddress : ' + ip);

// Set HTML Language
res.locals.htmlLang = 'en';

Expand Down
Loading