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

fix(KUI-1378): add noindex robots header for secondary hosts (app.*) #388

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 132 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"jest-environment-jsdom": "29.7.0",
"lint-staged": "^15.2.2",
"mini-css-extract-plugin": "^2.8.1",
"node-mocks-http": "^1.14.1",
"nodemon": "^3.1.0",
"null-loader": "^4.0.1",
"path": "^0.12.7",
Expand Down
6 changes: 6 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ server.use(
})
)

/* ********************************
* ******* No index middleware ****
* ********************************
*/
server.use(require('./utils/noIndexMiddleware.js'))

/* **********************************
* ******* APPLICATION ROUTES *******
* **********************************
Expand Down
28 changes: 28 additions & 0 deletions server/utils/noIndexMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { server: serverConfig } = require('../configuration')

/**
* Middleware that adds "noindex" robots header if "server host" and the request
* host ("forwarded host") doesn't match. This is done to prevent Google from
* indexing app.kth.se/* when we want the user to use www.kth.se/*
*
* The "server host" (SERVER_HOST_URL) is the primary host we expected the
* app to be hosted on (ie https://www.kth.se).
*
* The orginal host of the request is replaced with an Azure host
* (*.azurewebsites.net) before reaching our app so we look at the "forwarded
* host" ("x-forwarded-host" header) .
*
* Default, if x-forwarded-host is missing, is to do nothing.
*/
module.exports = function (req, res, next) {
const forwardedHost = req.header('x-forwarded-host')
if (forwardedHost) {
const serverHostUrl = new URL(serverConfig.hostUrl)
const serverHost = serverHostUrl.host
if (serverHost !== forwardedHost) {
console.log('Hello world')
res.set('x-robots-tag', 'noindex')
}
}
next()
}
53 changes: 53 additions & 0 deletions server/utils/noIndexMiddleware.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const httpMocks = require('node-mocks-http')

const noIndexMiddleware = require('./noIndexMiddleware')

const mockedServerHost = 'mockedserverhost.example.com'
jest.mock('../configuration', () => ({
server: { hostUrl: 'https://mockedserverhost.example.com' },
}))

describe('noIndexMiddleware', () => {
it('should not set "X-Robots-Tag" header if "X-Forwarded-Host" is missing', () => {
const next = jest.fn()
const { req, res } = httpMocks.createMocks({
headers: {},
})

noIndexMiddleware(req, res, next)

expect(res.getHeader('X-Robots-Tag')).toBeUndefined()
// expect(next).toHaveBeenCalledOnce()
expect(next).toHaveBeenCalledTimes(1)
axelbjo marked this conversation as resolved.
Show resolved Hide resolved
})

it('should not set "X-Robots-Tag" header if "X-Forwarded-Host" match SERVER_HOST_URL', () => {
const next = jest.fn()
const { req, res } = httpMocks.createMocks({
headers: {
'X-Forwarded-Host': mockedServerHost,
},
})

noIndexMiddleware(req, res, next)

expect(res.getHeader('X-Robots-Tag')).toBeUndefined()
// expect(next).toHaveBeenCalledOnce()
expect(next).toHaveBeenCalledTimes(1)
})

it('should set "X-Robots-Tag" header if "X-Forwarded-Host" does not match SERVER_HOST_URL', () => {
const next = jest.fn()
const { req, res } = httpMocks.createMocks({
headers: {
'X-Forwarded-Host': 'anotherhost.example.com',
},
})

noIndexMiddleware(req, res, next)

expect(res.getHeader('X-Robots-Tag')).toBe('noindex')
// expect(next).toHaveBeenCalledOnce()
expect(next).toHaveBeenCalledTimes(1)
})
})
Loading