-
Notifications
You must be signed in to change notification settings - Fork 43
SONARXML-277 Add S5734 Require nosniff header in web.config #376
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0f41c5b
Add S5734 Require nosniff header in web.config
tomasz-tylenda-sonarsource 8587bad
Update sonar-xml-plugin/src/test/resources/checks/MimeNosniffCheck/we…
tomasz-tylenda-sonarsource 8bd3dbf
1. message 2. FNs
tomasz-tylenda-sonarsource 9ecbaa8
style
tomasz-tylenda-sonarsource File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| { | ||
| "project:asp.net-web-application/web.config": [ | ||
| 1 | ||
| ] | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
66 changes: 66 additions & 0 deletions
66
...-xml-plugin/src/main/java/org/sonar/plugins/xml/checks/security/web/MimeNosniffCheck.java
This file contains hidden or 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,66 @@ | ||
| /* | ||
| * SonarQube XML Plugin | ||
| * Copyright (C) 2010-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.plugins.xml.checks.security.web; | ||
|
|
||
| import org.sonar.check.Rule; | ||
| import org.sonarsource.analyzer.commons.xml.XPathBuilder; | ||
| import org.sonarsource.analyzer.commons.xml.XmlFile; | ||
| import org.w3c.dom.Document; | ||
| import org.w3c.dom.Element; | ||
| import org.w3c.dom.NodeList; | ||
|
|
||
| import javax.xml.xpath.XPathExpression; | ||
| import java.util.Collections; | ||
|
|
||
| /** | ||
| * Ensure that the X-Content-Type-Options header is set to "nosniff" to prevent MIME type sniffing. | ||
| * The check applies to .NET web.config files. | ||
| */ | ||
| @Rule(key = "S5734") | ||
| public class MimeNosniffCheck extends BaseWebCheck { | ||
| private final XPathExpression httpCookiesExpression = XPathBuilder | ||
| .forExpression( | ||
| "/configuration" | ||
| + "/system.webServer" | ||
| + "/httpProtocol" | ||
| + "/customHeaders" | ||
| + "/add[@name=\"X-Content-Type-Options\" and @value=\"nosniff\"]") | ||
| .build(); | ||
|
|
||
| /** Attach the issue to the closest existing node. */ | ||
| private final XPathExpression reportNodeExpression = XPathBuilder | ||
| .forExpression(getDeepestExistingNode("configuration", "system.webServer", "httpProtocol", "customHeaders")) | ||
| .build(); | ||
|
|
||
| @Override | ||
| protected void scanWebConfig(XmlFile file) { | ||
| Document document = file.getDocument(); | ||
| NodeList expectedNodes = evaluate(httpCookiesExpression, document); | ||
|
|
||
| // null is returned on internal errors, and we don't want to raise a false positive in that case. | ||
| if (expectedNodes != null && expectedNodes.getLength() == 0) { | ||
| evaluateAsList(reportNodeExpression, document) | ||
| .stream() | ||
| .findFirst() | ||
| .ifPresent(target -> | ||
| reportIssue( | ||
| XmlFile.nameLocation((Element) target), | ||
| "Set the \"X-Content-Type-Options\" HTTP header to \"nosniff\" to mitigate MIME Confusion Attacks.", | ||
| Collections.emptyList())); | ||
| } | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
sonar-xml-plugin/src/main/resources/org/sonar/l10n/xml/rules/xml/S5734.html
This file contains hidden or 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,66 @@ | ||
| <p><a href="https://blog.mozilla.org/security/2016/08/26/mitigating-mime-confusion-attacks-in-firefox/">MIME confusion</a> attacks occur when an | ||
| attacker successfully tricks a web-browser to interpret a resource as a different type than the one expected. To correctly interpret a resource | ||
| (script, image, stylesheet …) web browsers look for the <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type">Content-Type | ||
| header</a> defined in the HTTP response received from the server, but often this header is not set or is set with an incorrect value. To avoid | ||
| content-type mismatch and to provide the best user experience, web browsers try to deduce the right content-type, generally by inspecting the content | ||
| of the resources (the first bytes). This "guess mechanism" is called <a href="https://en.wikipedia.org/wiki/Content_sniffing">MIME type | ||
| sniffing</a>.</p> | ||
| <p>Attackers can take advantage of this feature when a website ("example.com" here) allows to upload arbitrary files. In that case, an attacker can | ||
| upload a malicious image <em>fakeimage.png</em> (containing malicious JavaScript code or <a | ||
| href="https://docs.microsoft.com/fr-fr/archive/blogs/ieinternals/script-polyglots">a polyglot content</a> file) such as:</p> | ||
| <pre> | ||
| <script>alert(document.cookie)</script> | ||
| </pre> | ||
| <p>When the victim will visit the website showing the uploaded image, the malicious script embedded into the image will be executed by web browsers | ||
| performing MIME type sniffing.</p> | ||
| <h2>Ask Yourself Whether</h2> | ||
| <ul> | ||
| <li> <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type">Content-Type</a> header is not systematically set for all | ||
| resources. </li> | ||
| <li> Content of resources can be controlled by users. </li> | ||
| </ul> | ||
| <p>There is a risk if you answered yes to any of those questions.</p> | ||
| <h2>Recommended Secure Coding Practices</h2> | ||
| <p>Implement <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options">X-Content-Type-Options</a> header with | ||
| <em>nosniff</em> value (the only existing value for this header) which is supported by all modern browsers and will prevent browsers from performing | ||
| MIME type sniffing, so that in case of Content-Type header mismatch, the resource is not interpreted. For example within a <script> object | ||
| context, JavaScript MIME types are expected (like <em>application/javascript</em>) in the Content-Type header.</p> | ||
| <h2>Sensitive Code Example</h2> | ||
| <p>The following ASP.NET website configuration is sensitive, because it does not set the <code>X-Content-Type-Options</code> HTTP response header to | ||
| <code>nosniff</code>:</p> | ||
| <pre> | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <configuration> <!-- Sensitive --> | ||
| <system.webServer> | ||
| <!-- ... --> | ||
| </system.webServer> | ||
| </configuration> | ||
| </pre> | ||
| <h2>Compliant Solution</h2> | ||
| <p>To mitigate this finding, set <code>nosniff</code> globally for all pages in the application in the <code>web.config</code> file. While it is also | ||
| possible to configure the header individually in the application code, or per <code><location></code> element, setting it globally is less | ||
| error-prone and therefore recommended.</p> | ||
| <pre> | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <configuration> | ||
| <system.webServer> | ||
| <httpProtocol> | ||
| <customHeaders> | ||
| <add name="X-Content-Type-Options" value="nosniff"/> | ||
| </customHeaders> | ||
| </httpProtocol> | ||
| <!-- ... --> | ||
| </system.webServer> | ||
| </configuration> | ||
| </pre> | ||
| <h2>See</h2> | ||
| <ul> | ||
| <li> OWASP - <a href="https://owasp.org/Top10/A05_2021-Security_Misconfiguration/">Top 10 2021 Category A5 - Security Misconfiguration</a> </li> | ||
| <li> OWASP - <a href="https://owasp.org/www-project-top-ten/2017/A6_2017-Security_Misconfiguration">Top 10 2017 Category A6 - Security | ||
| Misconfiguration</a> </li> | ||
| <li> <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options">developer.mozilla.org</a> - X-Content-Type-Options | ||
| </li> | ||
| <li> <a href="https://blog.mozilla.org/security/2016/08/26/mitigating-mime-confusion-attacks-in-firefox/">blog.mozilla.org</a> - Mitigating MIME | ||
| Confusion Attacks in Firefox </li> | ||
| </ul> | ||
|
|
28 changes: 28 additions & 0 deletions
28
sonar-xml-plugin/src/main/resources/org/sonar/l10n/xml/rules/xml/S5734.json
This file contains hidden or 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,28 @@ | ||
| { | ||
| "title": "Allowing browsers to sniff MIME types is security-sensitive", | ||
| "type": "SECURITY_HOTSPOT", | ||
| "code": { | ||
| "impacts": { | ||
| "SECURITY": "LOW" | ||
| }, | ||
| "attribute": "COMPLETE" | ||
| }, | ||
| "status": "ready", | ||
| "remediation": { | ||
| "func": "Constant\/Issue", | ||
| "constantCost": "5min" | ||
| }, | ||
| "tags": [], | ||
| "defaultSeverity": "Minor", | ||
| "ruleSpecification": "RSPEC-5734", | ||
| "sqKey": "S5734", | ||
| "scope": "Main", | ||
| "securityStandards": { | ||
| "OWASP": [ | ||
| "A6" | ||
| ], | ||
| "OWASP Top 10 2021": [ | ||
| "A5" | ||
| ] | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| "S5322", | ||
| "S5332", | ||
| "S5604", | ||
| "S5734", | ||
| "S6358", | ||
| "S6359", | ||
| "S6361", | ||
|
|
||
36 changes: 36 additions & 0 deletions
36
...-xml-plugin/src/test/java/org/sonar/plugins/xml/checks/security/web/BaseWebCheckTest.java
This file contains hidden or 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,36 @@ | ||
| /* | ||
| * SonarQube XML Plugin | ||
| * Copyright (C) 2010-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.plugins.xml.checks.security.web; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class BaseWebCheckTest { | ||
| @Test | ||
| void testGetDeepestExistingNode() { | ||
| BaseWebCheck baseWebCheck = new BaseWebCheck(); | ||
|
|
||
| assertThat(baseWebCheck.getDeepestExistingNode("a")) | ||
| .isEqualTo("/a"); | ||
|
|
||
| assertThat(baseWebCheck.getDeepestExistingNode("a", "b")) | ||
| .isEqualTo("/a/b|/a[not(b)]"); | ||
|
|
||
| assertThat(baseWebCheck.getDeepestExistingNode("a", "b", "c")) | ||
| .isEqualTo("/a/b/c|/a/b[not(c)]|/a[not(b)]"); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
...-plugin/src/test/java/org/sonar/plugins/xml/checks/security/web/MimeNosniffCheckTest.java
This file contains hidden or 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,53 @@ | ||
| /* | ||
| * SonarQube XML Plugin | ||
| * Copyright (C) 2010-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.plugins.xml.checks.security.web; | ||
|
|
||
| import java.nio.file.Paths; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
| import org.sonarsource.analyzer.commons.xml.checks.SonarXmlCheckVerifier; | ||
|
|
||
| class MimeNosniffCheckTest { | ||
|
|
||
| @Test | ||
| void compliant() { | ||
| String path = Paths.get("webconfig-compliant", "web.config").toString(); | ||
| SonarXmlCheckVerifier.verifyNoIssue(path, new MimeNosniffCheck()); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { | ||
| "webconfig-no-custom-headers", | ||
| "webconfig-missing-nosniff", | ||
| "webconfig-other-value" | ||
| }) | ||
| void noncompliant(String dirName) { | ||
| String path = Paths.get(dirName, "web.config").toString(); | ||
| SonarXmlCheckVerifier.verifyIssues(path, new MimeNosniffCheck()); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { | ||
| "webconfig-add-remove", | ||
| "webconfig-add-clear", | ||
| }) | ||
| void false_negatives(String dirName) { | ||
| String path = Paths.get(dirName, "web.config").toString(); | ||
| SonarXmlCheckVerifier.verifyNoIssue(path, new MimeNosniffCheck()); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
sonar-xml-plugin/src/test/resources/checks/MimeNosniffCheck/webconfig-add-clear/web.config
This file contains hidden or 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,11 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <configuration> | ||
| <system.webServer> | ||
| <httpProtocol> | ||
| <customHeaders> | ||
| <add name="X-Content-Type-Options" value="nosniff"/> <!-- FN, please Sonar, but don't turn it on --> | ||
| <clear /> | ||
| </customHeaders> | ||
| </httpProtocol> | ||
| </system.webServer> | ||
| </configuration> |
11 changes: 11 additions & 0 deletions
11
sonar-xml-plugin/src/test/resources/checks/MimeNosniffCheck/webconfig-add-remove/web.config
This file contains hidden or 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,11 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <configuration> | ||
| <system.webServer> | ||
| <httpProtocol> | ||
| <customHeaders> | ||
| <add name="X-Content-Type-Options" value="nosniff"/> <!-- FN, please Sonar, but don't turn it on --> | ||
| <remove name="X-Content-Type-Options" /> | ||
| </customHeaders> | ||
| </httpProtocol> | ||
| </system.webServer> | ||
| </configuration> |
11 changes: 11 additions & 0 deletions
11
sonar-xml-plugin/src/test/resources/checks/MimeNosniffCheck/webconfig-compliant/web.config
This file contains hidden or 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,11 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <configuration> | ||
| <system.webServer> | ||
| <httpProtocol> | ||
| <customHeaders> | ||
| <clear /> | ||
| <add name="X-Content-Type-Options" value="nosniff"/> | ||
tomasz-tylenda-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </customHeaders> | ||
| </httpProtocol> | ||
| </system.webServer> | ||
| </configuration> | ||
12 changes: 12 additions & 0 deletions
12
...ml-plugin/src/test/resources/checks/MimeNosniffCheck/webconfig-missing-nosniff/web.config
This file contains hidden or 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,12 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <configuration> | ||
| <system.webServer> | ||
| <httpProtocol> | ||
| <customHeaders> <!-- Noncompliant {{Set the "X-Content-Type-Options" HTTP header to "nosniff" to mitigate MIME Confusion Attacks.}} --> | ||
| <!-- ^^^^^^^^^^^^^ --> | ||
| <remove name="X-Powered-By" /> | ||
| <add name="X-Frame-Options" value="SAMEORIGIN" /> | ||
| </customHeaders> | ||
| </httpProtocol> | ||
| </system.webServer> | ||
| </configuration> |
4 changes: 4 additions & 0 deletions
4
...-plugin/src/test/resources/checks/MimeNosniffCheck/webconfig-no-custom-headers/web.config
This file contains hidden or 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,4 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <configuration> <!-- Noncompliant {{Set the "X-Content-Type-Options" HTTP header to "nosniff" to mitigate MIME Confusion Attacks.}} --> | ||
| <!-- ^[sc=2;ec=14] --> | ||
| </configuration> |
11 changes: 11 additions & 0 deletions
11
sonar-xml-plugin/src/test/resources/checks/MimeNosniffCheck/webconfig-other-value/web.config
This file contains hidden or 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,11 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <configuration> | ||
| <system.webServer> | ||
| <httpProtocol> | ||
| <customHeaders> <!-- Noncompliant {{Set the "X-Content-Type-Options" HTTP header to "nosniff" to mitigate MIME Confusion Attacks.}} --> | ||
| <!-- ^^^^^^^^^^^^^ --> | ||
| <add name="X-Content-Type-Options" value="yes"/> | ||
| </customHeaders> | ||
| </httpProtocol> | ||
| </system.webServer> | ||
| </configuration> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.