Skip to content

Commit 730848c

Browse files
authored
Merge pull request github#12648 from michaelnebel/csharp/cs-web-debug-binary
C#: Improve cs/web/debug-binary to repect the RemoveAttributes transformation.
2 parents a034f89 + 4a64479 commit 730848c

File tree

10 files changed

+73
-3
lines changed

10 files changed

+73
-3
lines changed

csharp/ql/lib/semmle/code/asp/WebConfig.qll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ class WebConfigXml extends XmlFile {
1111
WebConfigXml() { this.getName().matches("%Web.config") }
1212
}
1313

14+
/**
15+
* A `Web.config` transformation file.
16+
*/
17+
class WebConfigReleaseTransformXml extends XmlFile {
18+
WebConfigReleaseTransformXml() { this.getName().matches("%Web.Release.config") }
19+
}
20+
1421
/** DEPRECATED: Alias for WebConfigXml */
1522
deprecated class WebConfigXML = WebConfigXml;
1623

@@ -19,6 +26,11 @@ class ConfigurationXmlElement extends XmlElement {
1926
ConfigurationXmlElement() { this.getName().toLowerCase() = "configuration" }
2027
}
2128

29+
/** A `<compilation>` tag in an ASP.NET configuration file. */
30+
class CompilationXmlElement extends XmlElement {
31+
CompilationXmlElement() { this.getName().toLowerCase() = "compilation" }
32+
}
33+
2234
/** DEPRECATED: Alias for ConfigurationXmlElement */
2335
deprecated class ConfigurationXMLElement = ConfigurationXmlElement;
2436

@@ -149,3 +161,15 @@ class HttpCookiesElement extends XmlElement {
149161
/** DEPRECATED: Alias for isRequireSsl */
150162
deprecated predicate isRequireSSL() { this.isRequireSsl() }
151163
}
164+
165+
/** A `Transform` attribute in a Web.config transformation file. */
166+
class TransformXmlAttribute extends XmlAttribute {
167+
TransformXmlAttribute() { this.getName().toLowerCase() = "transform" }
168+
169+
/**
170+
* Gets the list of attribute removals in `Transform=RemoveAttributes(list)`.
171+
*/
172+
string getRemoveAttributes() {
173+
result = this.getValue().regexpCapture("RemoveAttributes\\((.*)\\)", 1).splitAt(",")
174+
}
175+
}

csharp/ql/src/Security Features/CWE-011/ASPNetDebug.ql

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ import semmle.code.asp.WebConfig
1919

2020
from SystemWebXmlElement web, XmlAttribute debugAttribute
2121
where
22-
debugAttribute = web.getAChild("compilation").getAttribute("debug") and
23-
not debugAttribute.getValue().toLowerCase() = "false"
22+
exists(CompilationXmlElement compilation | compilation.getParent() = web |
23+
debugAttribute = compilation.getAttribute("debug") and
24+
not debugAttribute.getValue().toLowerCase() = "false"
25+
) and
26+
not exists(
27+
TransformXmlAttribute attribute, CompilationXmlElement compilation,
28+
WebConfigReleaseTransformXml file
29+
|
30+
compilation = attribute.getElement() and
31+
file = compilation.getFile() and
32+
attribute.getRemoveAttributes() = "debug" and
33+
file.getParentContainer() = web.getFile().getParentContainer()
34+
)
2435
select debugAttribute, "The 'debug' flag is set for an ASP.NET configuration file."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The query `cs/web/debug-binary` now disregards the `debug` attribute in case there is a transformation that removes it.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
| bad/Web.config:4:5:7:7 | debug=true | The 'debug' flag is set for an ASP.NET configuration file. |
1+
| bad1/Web.config:4:5:7:7 | debug=true | The 'debug' flag is set for an ASP.NET configuration file. |
2+
| bad2/Web.config:4:5:7:7 | debug=true | The 'debug' flag is set for an ASP.NET configuration file. |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
3+
<system.web>
4+
<compilation xdt:Transform="RemoveAttributes(debug)" />
5+
</system.web>
6+
</configuration>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<system.web>
4+
<compilation
5+
defaultLanguage="c#"
6+
debug="true"
7+
/>
8+
</system.web>
9+
</configuration>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
3+
<system.web>
4+
<compilation xdt:Transform="RemoveAttributes(debug)" />
5+
</system.web>
6+
</configuration>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<system.web>
4+
<compilation
5+
defaultLanguage="c#"
6+
debug="true"
7+
/>
8+
</system.web>
9+
</configuration>

0 commit comments

Comments
 (0)