'How to set headers for a React app created using 'create-react-app'
I created a sample react app using "create-react-app". I need to set the request headers with X-Frame-options and Content-Security-Policy. How do I do this? I just tried to update my index.html with the below code, but I'm not sure whether this it's right or not. Can anyone help with this?
<meta http-equiv="Content-Security-Policy" content="frame-ancestors 'none'">
<meta http-equiv="X-Frame-Options" content="SAMEORIGIN">
Solution 1:[1]
Simply modifying index.html as you have is not sufficient, at least for X-Frame-Options. Per OWASP:
Common Defense Mistakes
Meta-tags that attempt to apply the X-Frame-Options directive DO NOT WORK. For example, ) will not work. You must apply the X-FRAME-OPTIONS directive as HTTP Response Header...
MDC has a similar warning.
I spent some time looking, but didn't find a way to set X-Frame-Options in React itself. There are ways to do it at the server level or in other languages (e.g. for Tomact, or in Java, or with webpack, or configure it with Spring Security), which may or may not be helpful to you.
React doesn't seem to support Content-Security-Policy either... at least not as of 2013, and I searched but did not find any more recent change in position.
Solution 2:[2]
You have to override some webpack config that comes with create-react-app and add "X-Frame-Options": "SAMEORIGIN".
But in CRA, you can't do that directly with a webpack.config.js. You gotta use a package that rewires the way CRA start / build your app. Here are the rewiring packages you can use:
- react-app-rewired (CRA v1)
- customize-cra or rescript (CRA v2)
- craco (CRA v3)
To know which CRA version you used, you can roughly follow the react-scripts version in your package.json.
craco
I succeeded with this craco config!!! In craco.config.js:
module.exports = {
webpack: {
headers: {
'X-Frame-Options': 'Deny'
}
}
}
(I tested by scanning in Burpsuite; the Frameable Response issue was gone.)
Reference: How to set alias path via webpack in CRA (create-react-app) and craco?
react-app-rewired
I THINK you could try this, BUT I haven't tested it. In config-overrides.js:
module.exports = {
devServer: function(configFunction) {
return function(proxy, allowedHost) {
const config = configFunction(proxy, allowedHost)
config.headers = {
'X-Frame-Options': 'Deny'
}
return config
}
}
}
Reference: Create React App adding CORS header
*I may have loosely gotten the versions wrongly, please correct if so.
Solution 3:[3]
If you are talking specifically for the dev server, you can configure the underlying express instance using the 'manually configure proxy' process.
You can set headers there:
// src/setupProxy.js
module.exports = function(app) {
app.use((req, res, next) => {
res.set({
'foo': 'bar'
});
next();
});
};
Solution 4:[4]
For react-scripts start update your src/setupProxy.js:
module.exports = function (app) {
app.use(function (req, res, next) {
res.setHeader("X-Frame-Options", "DENY");
next();
});
};
If you in the Heroku and use buildpacks update static.json:
{
"root": "build/",
"https_only": true,
"clean_urls": true,
"routes": {
"/**": "index.html"
},
"headers": {
"/**": {
"X-Frame-Options": "DENY"
}
}
}
Solution 5:[5]
x-frame-options and content-security-policy are response headers and not request headers. So you need to put it on server and not in react app (client)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | |
| Solution 3 | dwjohnston |
| Solution 4 | gavenkoa |
| Solution 5 | pantech |
