QWED

Type to search pages, engines and advisories.

    Looking for API details?Search the docs

    Security·Advisories·GHSA-pr6c-cqg8-6w5r

    CodeVerifier admits code hidden by a source encoding declaration (utf-7, unicode_escape)

    highFixed in 9.0.0GHSA-pr6c-cqg8-6w5r · Published 6 November 2026

    Summary

    CodeVerifier.verify_code (/verify/code) reads Python source as the text it is given and never applies the source's coding declaration. Python does apply it when the code runs from a file, an import, or compile/exec of bytes. With # -*- coding: utf-7 -*- or # -*- coding: unicode_escape -*-, what CodeVerifier reads as a comment decodes to new statements, so code that imports os and calls os.system is returned VERIFIED · ADMIT.

    Details

    PEP 263 lets the first or second line of a Python file name its source encoding. The interpreter decodes the file with that codec before tokenising. Two standard codecs turn comment text into line breaks and arbitrary characters:

    • utf-7: +AAo- is a newline, and any character can be written as +<base64 of UTF-16>-;
    • unicode_escape: \x0a is a newline, and any character can be written as \xNN.

    CodeVerifier parses and scans the string as given. The encoded part sits after #, so to its AST and line scans it is one comment. Encoding each character on its own leaves no readable name for the substring checks (os.system, import), and the encoded runs are too short for the high-entropy secret heuristic.

    exec() of a str ignores the declaration, so a caller that only ever runs the verified code as a string is not affected. A caller that writes it to a .py file and runs or imports it, or passes it as bytes to compile/exec, runs the hidden statements.

    The GitHub Action's scan-code mode had the same gap. It is fixed in 9.0.0 under GHSA-hg73-j3qv-w4vh: scan-code now decodes with tokenize.detect_encoding and refuses any declaration other than UTF-8. This advisory covers CodeVerifier and /verify/code.

    PoC

    Harmless: the hidden line runs echo HIDDEN.

    import base64
    from qwed_new.core.code_verifier import CodeVerifier
    from qwed_new.core.diagnostics import admission_decision
    
    hidden = "\nimport os\nos.system('echo HIDDEN')\n"
    utf7 = "# -*- coding: utf-7 -*-\nx = 1  #" + "".join(
        "+" + base64.b64encode(c.encode("utf-16-be")).decode().rstrip("=") + "-" for c in hidden
    ) + "\n"
    escape = "# -*- coding: unicode_escape -*-\nx = 1  #" + "".join("\\x%02x" % ord(c) for c in hidden) + "\n"
    
    for source in (utf7, escape):
        result = CodeVerifier().verify_code(source, language="python")
        print(result.status.value, admission_decision(result).value)   # VERIFIED ADMIT, twice
        open("verified.py", "w", encoding="ascii").write(source)
        # python verified.py  ->  prints HIDDEN
    

    Reproduced on main @ b74ac00 (8.0.0). Both files print HIDDEN when run with CPython 3.11, 3.14.3 (the server image's Python) and 3.15.2.

    Impact

    A false VERIFIED · ADMIT from /verify/code and CodeVerifier.verify_code for code that runs a shell command, when the admitted code is saved to a file and run or imported, or compiled from bytes. Any check the verifier applies can be hidden the same way, not only process execution.

    Patches

    Fixed in 9.0.0 (3090e22). Upgrade to 9.0.0 or later.

    CodeVerifier finds the declared encoding with tokenize.detect_encoding, as the interpreter does, and refuses Python source whose codec is not UTF-8 (source_encoding, CRITICAL, so BLOCKED). A declaration naming an unknown codec is refused too. Encodings are compared by codec, so every spelling Python accepts for UTF-8 (utf-8, UTF8, utf_8, utf-8-sig) and source with no declaration are admitted as before.

    On 9.0.0 both PoC sources are VERIFIED, is_valid: false, BLOCKED (source_encoding), and a source with # -*- coding: utf-8 -*- is still admitted.

    Workarounds

    Upgrade to 9.0.0. On older versions, run admitted code only as a str passed to exec, or refuse any source whose first two lines contain a coding declaration before passing it to QWED.

    All advisories