AI-Generated Code Security: Insecure Patterns to Check
Short answer: for AI-generated code security, remember that an assistant can write code that works and is still unsafe. Paste it into the AI Code Risk Linter and it lists lines matching well-known insecure patterns, each with the reason, a safer alternative and, where the mapping is certain, a CWE number. It is a pattern-based quick check, not a security audit. It can be wrong, and it never runs your code.
Patterns it looks for
JavaScript and TypeScript: eval and new Function; innerHTML, outerHTML, insertAdjacentHTML and document.write with a non-constant value; dangerouslySetInnerHTML; exec built from a template string; Math.random near words like token, secret or password; MD5 or SHA-1 near a password; rejectUnauthorized: false; CORS * together with credentials; JWT ignoreExpiration and the none algorithm; SQL built with template strings or concatenation; redirects using request values; target="_blank" without rel; tokens in localStorage.
Python: eval and exec; loading pickle or marshal; yaml.load without a safe loader; subprocess with shell=True; verify=False; the random module near secrets; assert used for security checks; DEBUG = True; SQL built with f-strings; tempfile.mktemp.
SQL: UPDATE or DELETE without WHERE, DROP, TRUNCATE, GRANT ALL.
Shell: a download piped into a shell (see checking a shell command before running it), chmod 777, curl -k.
Several languages: passwords, secrets, tokens and API keys written into the code. Obvious placeholders such as changeme are ignored.
Two patterns worth understanding
innerHTMLfrom a non-constant value (CWE-79). If the value contains user input, an attacker's markup runs in your page. The safe habit istextContent, or escaping, as described in HTML entities.- SQL built by concatenation (CWE-89). Use parameterised queries instead of joining user values into the query string. A formatted query is easier to review; see SQL formatting.
How it reads your code
Text inside comments and string literals is not treated as code, so // eval(x) is not flagged. A value counts as constant when it is only string literals, so el.innerHTML = '<b>ok</b>' is not flagged. JavaScript is read with the site's JavaScript lexer, and if that fails (TypeScript types and JSX can cause this), a simpler scanner is used and a note says so.
What it cannot catch
- Data flow. It does not know where a variable came from, so
exec(cmd)is not flagged even ifcmdholds user input. - Missing protection: missing authentication or authorisation, weak access rules, race conditions, business-logic flaws, vulnerable dependencies.
- Other languages and unusual formatting, such as aliased imports, generated code and minified code.
A clean result is not proof of safety. For anything that matters, use a real SAST tool such as Semgrep or CodeQL, code review and tests.
How to check AI-generated code security with the AI Code Risk Linter
- Open the AI Code Risk Linter.
- Paste the code, up to 1,000,000 characters.
- Filter by severity and switch off rules that do not apply.
- Read each finding and the safer alternative, then fix or justify it.
- Copy a Markdown report, or export SARIF 2.1.0 to load the findings into tools that read it.
The built-in example is a small JavaScript login handler with 6 findings in 11 lines: 3 high and 3 medium severity.
Limits to know
- At most 200 matches per rule and 2,000 findings in total; the list shows the first 300.
- Only four languages are covered, and only the listed patterns.
- Everything runs in your browser.
Frequently asked questions
Can a linter prove AI code is secure? No. It finds known risky patterns only.
What is CWE-79? Cross-site scripting. CWE-89 is SQL injection.
Does it run my code? Never.
What is SARIF? A standard format for static analysis results.
What should I use besides this? Review, tests and a real SAST tool.