Skip to content

Commit e1c4b8c

Browse files
committed
Python: Add helper predicates to Escaping kinds
The other approach felt a bit too much like specifying magic strings that you had to get right. (crossing your fingers that no-one writes `HTML` instead of `html`)
1 parent bcef8d1 commit e1c4b8c

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

python/ql/src/semmle/python/Concepts.qll

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,21 @@ module Escaping {
334334
abstract DataFlow::Node getOutput();
335335

336336
/**
337-
* Gets the context that this function escapes for, such as `html`, or `url`.
337+
* Gets the context that this function escapes for.
338+
*
339+
* While kinds are represented as strings, this should not be relied upon. Use the
340+
* predicates in the `Escaping` module, such as `getHtmlKind`.
338341
*/
339342
abstract string getKind();
340343
}
344+
345+
/** Gets the escape-kind for escaping a string so it can safely be included in HTML. */
346+
string getHtmlKind() { result = "html" }
347+
// TODO: If adding an XML kind, update the modeling of the `MarkupSafe` PyPI package.
348+
//
349+
// Technically it claims to escape for both HTML and XML, but for now we don't have
350+
// anything that relies on XML escaping, so I'm going to defer deciding whether they
351+
// should be the same kind, or whether they deserve to be treated differently.
341352
}
342353

343354
/**
@@ -346,7 +357,7 @@ module Escaping {
346357
* `<p>{}</p>`.
347358
*/
348359
class HtmlEscaping extends Escaping {
349-
HtmlEscaping() { range.getKind() = "html" }
360+
HtmlEscaping() { range.getKind() = Escaping::getHtmlKind() }
350361
}
351362

352363
/** Provides classes for modeling HTTP-related APIs. */

0 commit comments

Comments
 (0)