|
| 1 | +# Copyright (c) 2025 David Salvisberg |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +r""" |
| 5 | +============================================ |
| 6 | +B704: Potential XSS on markupsafe.Markup use |
| 7 | +============================================ |
| 8 | +
|
| 9 | +``markupsafe.Markup`` does not perform any escaping, so passing dynamic |
| 10 | +content, like f-strings, variables or interpolated strings will potentially |
| 11 | +lead to XSS vulnerabilities, especially if that data was submitted by users. |
| 12 | +
|
| 13 | +Instead you should interpolate the resulting ``markupsafe.Markup`` object, |
| 14 | +which will perform escaping, or use ``markupsafe.escape``. |
| 15 | +
|
| 16 | +
|
| 17 | +**Config Options:** |
| 18 | +
|
| 19 | +This plugin allows you to specify additional callable that should be treated |
| 20 | +like ``markupsafe.Markup``. By default we recognize ``flask.Markup`` as |
| 21 | +an alias, but there are other subclasses or similar classes in the wild |
| 22 | +that you may wish to treat the same. |
| 23 | +
|
| 24 | +Additionally there is a whitelist for callable names, whose result may |
| 25 | +be safely passed into ``markupsafe.Markup``. This is useful for escape |
| 26 | +functions like e.g. ``bleach.clean`` which don't themselves return |
| 27 | +``markupsafe.Markup``, so they need to be wrapped. Take care when using |
| 28 | +this setting, since incorrect use may introduce false negatives. |
| 29 | +
|
| 30 | +These two options can be set in a shared configuration section |
| 31 | +`markupsafe_xss`. |
| 32 | +
|
| 33 | +
|
| 34 | +.. code-block:: yaml |
| 35 | +
|
| 36 | + markupsafe_xss: |
| 37 | + # Recognize additional aliases |
| 38 | + extend_markup_names: |
| 39 | + - webhelpers.html.literal |
| 40 | + - my_package.Markup |
| 41 | +
|
| 42 | + # Allow the output of these functions to pass into Markup |
| 43 | + allowed_calls: |
| 44 | + - bleach.clean |
| 45 | + - my_package.sanitize |
| 46 | +
|
| 47 | +
|
| 48 | +:Example: |
| 49 | +
|
| 50 | +.. code-block:: none |
| 51 | +
|
| 52 | + >> Issue: [B704:markupsafe_markup_xss] Potential XSS with |
| 53 | + ``markupsafe.Markup`` detected. Do not use ``Markup`` |
| 54 | + on untrusted data. |
| 55 | + Severity: Medium Confidence: High |
| 56 | + CWE: CWE-79 (https://cwe.mitre.org/data/definitions/79.html) |
| 57 | + Location: ./examples/markupsafe_markup_xss.py:5:0 |
| 58 | + 4 content = "<script>alert('Hello, world!')</script>" |
| 59 | + 5 Markup(f"unsafe {content}") |
| 60 | + 6 flask.Markup("unsafe {}".format(content)) |
| 61 | +
|
| 62 | +.. seealso:: |
| 63 | +
|
| 64 | + - https://pypi.org/project/MarkupSafe/ |
| 65 | + - https://markupsafe.palletsprojects.com/en/stable/escaping/#markupsafe.Markup |
| 66 | + - https://cwe.mitre.org/data/definitions/79.html |
| 67 | +
|
| 68 | +.. versionadded:: 1.8.3 |
| 69 | +
|
| 70 | +""" |
| 71 | +import ast |
| 72 | + |
| 73 | +import bandit |
| 74 | +from bandit.core import issue |
| 75 | +from bandit.core import test_properties as test |
| 76 | +from bandit.core.utils import get_call_name |
| 77 | + |
| 78 | + |
| 79 | +def gen_config(name): |
| 80 | + if name == "markupsafe_xss": |
| 81 | + return { |
| 82 | + "extend_markup_names": [], |
| 83 | + "allowed_calls": [], |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +@test.takes_config("markupsafe_xss") |
| 88 | +@test.checks("Call") |
| 89 | +@test.test_id("B704") |
| 90 | +def markupsafe_markup_xss(context, config): |
| 91 | + |
| 92 | + qualname = context.call_function_name_qual |
| 93 | + if qualname not in ("markupsafe.Markup", "flask.Markup"): |
| 94 | + if qualname not in config.get("extend_markup_names", []): |
| 95 | + # not a Markup call |
| 96 | + return None |
| 97 | + |
| 98 | + args = context.node.args |
| 99 | + if not args or isinstance(args[0], ast.Constant): |
| 100 | + # both no arguments and a constant are fine |
| 101 | + return None |
| 102 | + |
| 103 | + allowed_calls = config.get("allowed_calls", []) |
| 104 | + if ( |
| 105 | + allowed_calls |
| 106 | + and isinstance(args[0], ast.Call) |
| 107 | + and get_call_name(args[0], context.import_aliases) in allowed_calls |
| 108 | + ): |
| 109 | + # the argument contains a whitelisted call |
| 110 | + return None |
| 111 | + |
| 112 | + return bandit.Issue( |
| 113 | + severity=bandit.MEDIUM, |
| 114 | + confidence=bandit.HIGH, |
| 115 | + cwe=issue.Cwe.XSS, |
| 116 | + text=f"Potential XSS with ``{qualname}`` detected. Do " |
| 117 | + f"not use ``{context.call_function_name}`` on untrusted data.", |
| 118 | + ) |
0 commit comments