QWED

Type to search pages, engines and advisories.

    Looking for API details?Search the docs

    Engines

    Is AI-generated code safe to run? What a check can tell you first

    A model that writes code is often right, and sometimes writes something that does more than it was asked. This guide covers what such code can reach, the difference between checking code before it runs and running it in a sandbox, what QWED's Code engine admits, and what to use for everything it does not.

    QWED project · Published · Updated

    The short answer

    Not by default. Code a model writes can read files, reach the network, start processes or use names that mean something only where it runs, and it reads as confidently when it does. Check it before it runs, and run it in a sandbox even then. QWED's Code engine, open source under Apache-2.0, admits a narrow kind of code: Python that only computes, using listed standard-library modules and names. It denies everything else, and never admits JavaScript, TypeScript, Java or Go. For general application code, use static analysis such as Semgrep, CodeQL or Bandit, a sandbox and review.

    What can go wrong with AI-generated code?

    • It reaches files or the network. A script asked to total a column also opens a file, or sends what it read somewhere.
    • It starts processes. A call to subprocess or os.system runs whatever command the string holds.
    • It runs code from text. eval, exec and less obvious routes turn a string into code that no reader saw.
    • It pulls in a package. A third-party import brings in code the model did not write and you did not review.
    • It reads a name it never set. In a notebook or a REPL, df or np is whatever the session holds under that name.

    Before it runs, or while it runs?

    There are two different things you can do with code you did not write, and they answer different questions.

    Before it runs

    What can this code reach?

    A static check reads the code without running it and decides from its shape. It can refuse code before anything happens. It cannot see values that exist only at run time.

    In QWED: the Code engine.

    While it runs

    What can this code damage?

    A sandbox runs the code with limits: no network, a read-only filesystem, an unprivileged user, a time limit. It contains what the code does. It does not show that the code was safe or correct.

    In QWED: the Symbolic engine runs CrossHair inside a Docker sandbox and returns UNVERIFIABLE or BLOCKED, never a proof. The Stats engine runs code in a sandbox and returns UNVERIFIABLE. SecureCodeExecutor runs code; it does not verify it.

    Running code is not verifying it. Use both: check first, and run what passes in a sandbox.

    What does QWED's Code engine admit?

    The Code engine is part of QWED's open-source core. For Python it parses the code into a syntax tree and admits it only when it is safe by its shape. Lists of what is dangerous kept missing something (the track record below shows how often), so the engine works from a list of what is safe instead:

    • Modules. Only computation-only standard-library modules, such as math, decimal, fractions, statistics, itertools, json, re and datetime, and in each only the listed names: os.sep is admitted, os.system is not. No third-party module is admitted.
    • Builtins. Only builtins that compute. open, eval, getattr, vars, globals and the others that reach files, code or namespaces are refused.
    • Attributes. Special attributes (__x__) are refused, and so is reading an attribute by a name made at run time.
    • Names. A name the code reads before it binds it is refused. If your runtime binds names before the code runs, declare them: CodeVerifier(bound_names=["df"]).

    Other languages get less. JavaScript, TypeScript, Java and Go are only searched for listed patterns, and a call reached through a variable or an alias matches none of them. A match shows the code unsafe; no match shows nothing, so such code comes back UNVERIFIABLE and is never admitted. SQL sent to /verify/code is decided by the SQL engine without a schema, so writes are refused but tables and columns are not checked. Checking LLM-generated SQL

    What does the check return?

    CodeDecisionWhy
    Python: add up prices with decimalVERIFIED · ADMITEvery module and name it uses is on the list.
    Python: subprocess.run(["ls", "-la"])VERIFIED · DENYmodule_not_admitted, process_execution
    Python: open("notes.txt").read()VERIFIED · DENYbuiltin_not_admitted, file_or_network_access
    Python: import numpyVERIFIED · DENYmodule_not_admitted: no third-party module is on the list.
    Python: print(df.head())VERIFIED · DENYunbound_name. With bound_names=["df"], VERIFIED · ADMIT.
    JavaScript: console.log(1 + 1);UNVERIFIABLE · DENYcode_verifier.language_unverifiable: never admitted.
    Python that does not parse, or RustBLOCKED · DENYcode_verifier.parse_error, code_verifier.unsupported_language

    VERIFIED · DENY means the engine found what the code uses outside its list, and the result names it. UNVERIFIABLE · DENY means it could not tell. BLOCKED · DENY means it could not check the code at all. Only VERIFIED · ADMIT may run.

    Try it

    In Python, with the core package (pip install qwed), with the output of a run on qwed 13.0.0:

    python
    from qwed_new.core.code_verifier import CodeVerifierfrom qwed_new.core.diagnostics import admission_decisionadmitted = '''from decimal import Decimalprices = ["19.99", "5.01", "0.10"]total = sum(Decimal(p) for p in prices)print(total)'''refused = '''import subprocesssubprocess.run(["ls", "-la"])'''for code in (admitted, refused):    result = CodeVerifier().verify_code(code, language="python")    print(result.status.value, admission_decision(result).value)    print(sorted({i["type"] for i in result.developer_fields["issues"]}))# VERIFIED ADMIT# []# VERIFIED BLOCKED# ['module_not_admitted', 'process_execution']

    BLOCKED in the second value is the admission decision; in a Verification Context record the same decision is written DENY. Over the self-hosted API, send the code and its language:

    request
    POST /verify/code{  "code": "import math\nprint(math.sqrt(2))",  "language": "python"}

    What to use for the rest

    Most application code reads files, calls services and uses packages, so the Code engine will not admit it. That is the right answer for a check that only admits what it can show safe, and it means other tools have to do the rest:

    • Static analysis for code in any language: Semgrep matches patterns that look like source code, CodeQL runs queries over code for code scanning, and Bandit looks for common security issues in Python. They report findings; they do not admit code.
    • A check on every pull request for code that is committed. QWED Security, a GitHub App, reads the files a pull request changes with deterministic engines and no model in the verdict: Python, JavaScript and TypeScript, Go and Rust patterns, taint from untrusted input to dangerous calls, credentials shaped like known providers' keys and high-entropy secrets, shell commands, Dockerfiles, CI workflows and dependencies. It blocks on what the pull request introduces and on what a release would ship, keeps older findings visible without blocking, and reports as GitHub checks, a pull request comment and code scanning results. It reads the code; it does not run it.
    • A sandbox for anything you run: a container with no network and a read-only filesystem, or a stronger boundary such as gVisor or a Firecracker microVM, with limits on time and memory.
    • Review by a person for code that will stay, as for code from anyone else.

    What does it not check?

    • Whether the code is right. Admitted code can still compute the wrong thing. The engine checks what the code can reach, not what it returns. For a figure, check the figure. How to check AI calculations
    • Time and memory. A loop that never ends is made only of admitted parts. Run admitted code with limits.
    • What your runtime binds. bound_names and trusted_modules are your declarations about your runtime; the engine takes them as given.
    • Code in other languages. It is never admitted, and a pattern search that finds nothing proves nothing.

    Results are only as sound as QWED's own lists and parser. Everything QWED does not verify · What each engine may claim

    Track record

    Each of these was a false accept in QWED's code checks: code admitted that should have been refused. Each is fixed, and tests cover it.

    • GHSA-7xrf-75qr-2r5w, fixed in qwed 11.0.0. /verify/code admitted JavaScript and Java that run commands, SQL that drops tables, and Python that uses names it never imports. Other languages are now never admitted, SQL is decided by SQLVerifier, and a name read before it is bound is refused.
    • GHSA-9r7q-mx75-hcj7, fixed in qwed 9.1.0. CodeVerifier 8.0.0 still admitted file, network, pickle and process code that its lists of dangerous calls did not name. Python is now admitted only by a list of what is safe.
    • GHSA-jrxf-9jh4-86w7, fixed in qwed 9.0.0. String-to-code execution through timeit, cProfile, pdb and code, and file access through tarfile, zipfile, gzip, codecs, linecache, builtins.open and os.listdir, were admitted.
    • GHSA-p75q-7rg5-ff46, fixed in qwed 9.0.0. Code that typing evaluates from strings, such as string annotations run by singledispatch.register or typing.get_type_hints, was admitted.
    • GHSA-qrp4-p44j-m484, fixed in qwed 9.0.0. Shell commands reached through the module loader, __loader__.load_module('posix').system, were admitted.
    • GHSA-pr6c-cqg8-6w5r, fixed in qwed 9.0.0. Code hidden by a source encoding declaration, such as utf-7 or unicode_escape, was admitted.
    • GHSA-4rj3-8wqj-cqwc, fixed in qwed 8.0.0. The SDK’s verify_code() used a local list instead of CodeVerifier, so it admitted code the server refused.
    • GHSA-4phf-6h72-9xfg, fixed in qwed 7.3.0. Command execution, file access and exfiltration code came back VERIFIED · ADMIT.
    • GHSA-6rxr-gq43-694g, fixed in qwed 7.2.3. Code that runs shell commands came back VERIFIED and was admitted.

    The full track record

    Sources and further reading

    Written and reviewed by the QWED maintainers under the project's governance model. Found an error? Contact us.