Python: Add html.escape as HTML sanitizer

This commit is contained in:
Rasmus Wriedt Larsen
2024-01-22 17:31:55 +01:00
parent 6533269387
commit cbed6e861d
2 changed files with 25 additions and 0 deletions

View File

@@ -4842,6 +4842,23 @@ module StdlibPrivate {
override predicate isShellInterpreted(DataFlow::Node arg) { arg = this.getCommand() }
}
}
// ---------------------------------------------------------------------------
// html
// ---------------------------------------------------------------------------
/**
* A call to 'html.escape'.
* See https://docs.python.org/3/library/html.html#html.escape
*/
private class HtmlEscapeCall extends Escaping::Range, API::CallNode {
HtmlEscapeCall() { this = API::moduleImport("html").getMember("escape").getACall() }
override DataFlow::Node getAnInput() { result = this.getParameter(0, "s").asSink() }
override DataFlow::Node getOutput() { result = this }
override string getKind() { result = Escaping::getHtmlKind() }
}
}
// ---------------------------------------------------------------------------

View File

@@ -0,0 +1,8 @@
import html
s = "tainted"
html.escape(s) # $ escapeInput=s escapeKind=html escapeOutput=html.escape(..)
html.escape(s, True) # $ escapeInput=s escapeKind=html escapeOutput=html.escape(..)
html.escape(s, False) # $ escapeInput=s escapeKind=html escapeOutput=html.escape(..)
html.escape(s, quote=False) # $ escapeInput=s escapeKind=html escapeOutput=html.escape(..)