-
Notifications
You must be signed in to change notification settings - Fork 5
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 header forwarding #28
base: main
Are you sure you want to change the base?
Changes from all commits
5eb499d
0e96a9c
7dec5a0
3e562a0
14d7575
054f045
e16238a
60b6b08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/node_modules | ||
.DS_Store |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,14 +99,17 @@ export default defineConfig({ | |
widgetsMapping: './src/widgets', | ||
templatesMapping: './src/templates', | ||
viewTransitionWorkaround: false, | ||
forwardHeaders: [ | ||
includeResponseHeaders: [ | ||
'content-security-policy', | ||
'strict-transport-security', | ||
'x-frame-options', | ||
'referrer-policy', | ||
'cache-control', | ||
'host' | ||
'cache-control' | ||
], | ||
excludeRequestHeaders: [ | ||
// For single-site setups or hosting on multiple servers, block the host header | ||
'host' | ||
] | ||
proxyRoutes: [ | ||
// Custom URLs that should be proxied to Apostrophe. | ||
// Note that all of `/api/v1` is already proxied, so | ||
|
@@ -153,17 +156,28 @@ improve performance for editors. Ordinary website visitors are | |
not impacted in any case. We are seeking an alternative solution to | ||
eliminate this option. | ||
|
||
### `forwardHeaders` | ||
### `includeResponseHeaders` | ||
|
||
An array of HTTP headers that you want to include from Apostrophe to the final response sent to the browser - useful if you want to use an Apostrophe module like `@apostrophecms/security-headers` and want to keep those headers as configured in Apostrophe. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also good for respecting apostrophe's caching headers. |
||
|
||
An array of HTTP headers that you want to forward from Apostrophe to the final response sent to the browser - useful if you want to use an Apostrophe module like `@apostrophecms/security-headers` and want to keep those headers as configured in Apostrophe. | ||
At the present time, Astro is not compatible with the `nonce` property of `content-security-policy` `script-src` value. So this is automatically removed with that integration. The rest of the CSP header remains unchanged. | ||
|
||
### `excludeRequestHeaders` | ||
|
||
An array of HTTP headers that you want to prevent from being forwarded from the browser to Apostrophe. This is particularly useful in single-site setups where you want to block the `host` header to allow Astro and Apostrophe to run on different domains or ports. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just say different hostnames. Different ports aren't really a problem. |
||
|
||
By default, all headers are forwarded except those specified in this array. | ||
|
||
### `forwardHeaders` (deprecated) | ||
|
||
This option has been replaced by `includeResponseHeaders` which provides clearer naming for its purpose. If both options are provided, `includeResponseHeaders` takes precedence. `forwardHeaders` will be removed in a future version. | ||
|
||
### Mapping Apostrophe templates to Astro components | ||
|
||
Since the front end of our project is entirely Astro, we'll need to create Astro components corresponding to each | ||
template that Apostrophe would normally render with Nunjucks. | ||
Create your template mapping in `src/templates/index.js` file. | ||
|
||
Create your template mapping in `src/templates/index.js` file. | ||
As shown above, this file path must then be added to your `astro.config.mjs` file, | ||
in the `templatesMapping` option of the `apostrophe` integration. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,14 @@ export default async function aposResponse(req) { | |
const aposUrl = new URL(url.pathname, aposHost); | ||
aposUrl.search = url.search; | ||
|
||
const requestHeaders = {} | ||
for (const header of req.headers) { | ||
requestHeaders[header[0]] = header[1]; | ||
const requestHeaders = {}; | ||
for (const [name, value] of req.headers) { | ||
const headerLower = name.toLowerCase(); | ||
if (!config.excludeRequestHeaders?.map(h => h.toLowerCase()).includes(headerLower)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
requestHeaders[name] = value; | ||
} | ||
} | ||
|
||
const res = await request(aposUrl.href, { headers: requestHeaders, method: req.method, body: req.body }); | ||
const responseHeaders = new Headers(); | ||
Object.entries(res.headers).forEach(([key, value]) => { | ||
|
@@ -21,7 +25,7 @@ export default async function aposResponse(req) { | |
}); | ||
const { headers, statusCode, ...rest } = res; | ||
const body = [204, 304].includes(statusCode) ? null : res.body; | ||
const response = new Response(body, { ...rest , status: res.statusCode, headers: responseHeaders }); | ||
const response = new Response(body, { ...rest, status: res.statusCode, headers: responseHeaders }); | ||
return response; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right so it is critical to include this setting in the project level PRs.