Skip to content

Commit 3a885ea

Browse files
committed
Insecure Helmet middle configuration - frameguard or CSP to 'false'
1 parent 8e251ee commit 3a885ea

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
2+
<qhelp>
3+
<overview>
4+
<p>
5+
<a href="https://helmetjs.github.io/">Helmet</a> is a collection of middleware functions for securing Express apps. It sets various HTTP headers to guard against common web vulnerabilities.
6+
7+
This query detects Helmet misconfigurations that can lead to security vulnerabilities, specifically:
8+
9+
<ul>
10+
<li>Disabling frame protection</li>
11+
<li>Disabling Content Security Policy</li>
12+
</ul>
13+
14+
Content Security Policy (CSP) helps spot and prevent injection attacks such as Cross-Site Scripting (XSS).
15+
16+
Removing frame protections exposes an application to attacks such as clickjacking, where an attacker can trick a user into clicking on a button or link on a targeted page when they intended to click on the page carrying out the attack.
17+
</p>
18+
</overview>
19+
<recommendation>
20+
<p>
21+
To help mitigate these vulnerabilities, ensure that the following Helmet functions are not disabled, and are configured appropriately to your application:
22+
<ul>
23+
<li><code>frameguard</code></li>
24+
<li><code>contentSecurityPolicy</code></li>
25+
</ul>
26+
</p>
27+
</recommendation>
28+
<example>
29+
<p>
30+
The following code snippet demonstrates Helmet configured in an insecure manner:
31+
<code class="language-javascript">
32+
const helmet = require('helmet');
33+
app.use(helmet({
34+
frameguard: false,
35+
contentSecurityPolicy: false
36+
}));
37+
</code>
38+
</p>
39+
<p>
40+
In this example, the defaults are used, which enables frame protection and a default Content Security Policy.
41+
42+
<code class="language-javascript">
43+
app.use(helmet());
44+
</code>
45+
46+
You can also enable a custom Content Security Policy by passing an object to the <code>contentSecurityPolicy</code> key. For example, taken from the <a href="https://helmetjs.github.io/#content-security-policy">Helmet docs:
47+
48+
<code class="language-javascript">
49+
app.use(
50+
helmet({
51+
contentSecurityPolicy: {
52+
directives: {
53+
"script-src": ["'self'", "example.com"],
54+
"style-src": null,
55+
},
56+
},
57+
})
58+
);
59+
<code>
60+
<p>
61+
62+
</p>
63+
</example>
64+
<references>
65+
<ul>
66+
<li>
67+
<a href="https://helmetjs.github.io/">helmet.js website</a>
68+
</li>
69+
</ul>
70+
</references>
71+
</qhelp>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @name Insecure configuration of Helmet security middleware
3+
* @description The Helmet middleware is used to set security-related HTTP headers in Express applications. This query finds instances where the middleware is configured with important security features disabled.
4+
* @kind problem
5+
* @problem.severity error
6+
* @security-severity 5.0
7+
* @precision high
8+
* @id javascript/insecure-helmet-configuration
9+
* @tags security
10+
* cwe-693
11+
* cwe-1021
12+
*/
13+
14+
import semmle.javascript.frameworks.ExpressModules
15+
16+
class HelmetProperty extends Property {
17+
HelmetProperty() {
18+
exists(ExpressLibraries::HelmetRouteHandler helmet |
19+
helmet.(DataFlow::CallNode).getAnArgument().asExpr().(ObjectExpr).getAProperty() = this
20+
)
21+
}
22+
23+
predicate isFalse() { this.getInit().(BooleanLiteral).getBoolValue() = false }
24+
25+
predicate isImportantSecuritySetting() {
26+
this.getName() in ["frameguard", "contentSecurityPolicy"]
27+
// read from data extensions to allow enforcing other settings
28+
// TODO
29+
}
30+
}
31+
32+
from HelmetProperty helmetSetting
33+
where
34+
helmetSetting.isFalse() and
35+
helmetSetting.isImportantSecuritySetting()
36+
select helmetSetting, "Helmet route handler, called with $@ set to 'false'", helmetSetting, helmetSetting.getName()

0 commit comments

Comments
 (0)