-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse the Forwarded header, for secure cookies WIN!
- Loading branch information
1 parent
c851851
commit 43f1d53
Showing
11 changed files
with
168 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import express from 'express'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
import { handleGetDocuments } from '../controller/documentController'; | ||
import { handleHealthCheck } from '../controller/healthController'; | ||
import { handleGetSystemRoles } from '../controller/systemRoleController'; | ||
import { importMetaUrl } from '../importMetaUtils'; | ||
|
||
export default express.Router() | ||
.use( | ||
'/documents', | ||
express.static( | ||
fileURLToPath(new URL('../resources/documents', importMetaUrl())) | ||
) | ||
) | ||
.get('/documents', handleGetDocuments) | ||
.get('/health', handleHealthCheck) | ||
.get('/systemRoles', handleGetSystemRoles); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Express, Request } from 'express'; | ||
|
||
/** | ||
* Unfortunately, "trust proxy" is by default broken when Express is behind an | ||
* AWS HTTP API Gateway: | ||
* - https://github.com/expressjs/express/issues/5459 | ||
* - https://repost.aws/en/questions/QUtBHMaz7IQ6aM4RCBMnJvgw/why-does-apigw-http-api-use-forwarded-header-while-other-services-still-use-x-forwarded-headers | ||
* | ||
* Therefore we use Express API overrides to modify our Request IP and Protocol properties: | ||
* - https://expressjs.com/en/guide/overriding-express-api.html | ||
*/ | ||
export function usingForwardedHeader(app: Express): Express { | ||
Object.defineProperties(app.request, { | ||
'ip': { | ||
configurable: true, | ||
enumerable: true, | ||
get() { | ||
const proxies = parseForwardedHeader(this as Request); | ||
return proxies?.['for']?.[0] ?? this.socket.remoteAddress; | ||
}, | ||
}, | ||
'protocol': { | ||
configurable: true, | ||
enumerable: true, | ||
get() { | ||
const proxies = parseForwardedHeader(this as Request); | ||
return proxies?.['proto']?.[0] ?? this.socket.encrypted ? 'https' : 'http'; | ||
}, | ||
}, | ||
}); | ||
return app; | ||
} | ||
|
||
function parseForwardedHeader(request: Request) { | ||
return request.header('Forwarded') | ||
?.split(",") | ||
.flatMap((proxy) => proxy.split(';')) | ||
.reduce( | ||
(result, proxyProps) => { | ||
const [key, value] = proxyProps.split('='); | ||
if (key && value) { | ||
result[key] = (result[key] || []).concat(value); | ||
} | ||
return result; | ||
}, | ||
{} as Record<string, string[]> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"availability-zones:account=600982866784:region=eu-west-1": [ | ||
"eu-west-1a", | ||
"eu-west-1b", | ||
"eu-west-1c" | ||
] | ||
"availability-zones:account=600982866784:region=eu-west-1": [ | ||
"eu-west-1a", | ||
"eu-west-1b", | ||
"eu-west-1c" | ||
] | ||
} |
Oops, something went wrong.