Impossible to make dynamic API response security headers based on the current authenticated user #45709
Unanswered
ctrlaltdylan
asked this question in
Help
Replies: 1 comment 2 replies
-
|
Yeah, as you mentioned, the res object in the API routes does not offer a method to alter the headers. But maybe there is a way to deal with it, one workaround that you could try is to send the current store information as a part of the API response data, and then use JavaScript to set the headers dynamically on the client side. Here's how you could implement this workaround:
app.get('/api/store-info', (req, res) => {
res.json({
store: req.store
});
});
fetch('/api/store-info')
.then(response => response.json())
.then(data => {
const store = data.store;
const headerValue = `frame-ancestors https://${store}.myshopify.com`;
document.head.append(`<meta http-equiv="Content-Security-Policy" content="${headerValue}">`);
});This should allow you to dynamically set the headers based on the current store information, without the need for the request object to be available in the headers() function :) |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Background
Shopify renders apps within an iframe.
And they have started to become more strict on Content Security Policies, namely the
frame-ancestor.More information on this policy here: https://shopify.dev/apps/store/security/iframe-protection
Long story short, the CSP header is dependent on the currently logged in Shopify store. So if
ctrlaltdylan.myshopify.comis logged in, then the headers need to be as followed:My middleware solution injects the current logged in store into the
reqobject.Next.js offers API response header customization, however the
requestis not passed to thisheaders()function available in thenext.config.js.This means that isn't not possible to dynamically adjust headers based on the request and the authenticated shop.
Other workarounds?
No. Unfortunately the
resobject in the API routes do not offer a method to alter the headers either.No workarounds seem to be possible.
Beta Was this translation helpful? Give feedback.
All reactions