Skip to content

Add PHP language support - #1

Open
helen wants to merge 18 commits into
mainfrom
add-php-support
Open

helen wants to merge 18 commits into
mainfrom
add-php-support

Conversation

@helen

@helen helen commented Apr 28, 2026 •

Copy link
Copy Markdown
Owner

Add PHP Language Support

This PR adds comprehensive PHP language support to the CodeQL repository, following the tree-sitter-based approach used by Ruby and Rust.

What's Included

QL Standard Library (php/ql/lib/)

  • AST layer: Public AST classes for expressions, statements, literals, variables, calls, parameters, declarations, namespaces, types, and operations — all layered on top of auto-generated TreeSitter node types
  • Control flow graph: Full CFG implementation using the shared codeql/controlflow library with support for if/while/do/for/foreach/switch/try statements, break/continue/return/throw completions, and stable node IDs
  • SSA: Static Single Assignment form using the shared codeql/ssa library
  • Dataflow: Complete shared-library dataflow integration including local flow, inter-procedural flow, content flow (array elements, object properties), taint tracking, and flow summaries
  • Security concepts: SqlExecution, HtmlConstruction, SystemCommandExecution, FileSystemAccess, CodeExecution, Deserialization, RequestForgery, RedirectSink

Framework Models (php/ql/lib/codeql/php/frameworks/)

  • Built-in functions: eval, exec, system, passthru, shell_exec, popen, proc_open, file_get_contents, fopen, unserialize, curl_*, header
  • PDO: PDO::query, PDO::exec, PDO::prepare
  • MySQLi: mysqli_query, mysqli::query, mysqli_real_query
  • HTTP sources: $_GET, $_POST, $_REQUEST, $_COOKIE, $_SERVER, $_FILES
  • Laravel: Request input sources, Eloquent raw queries, Blade response sinks, redirect sinks
  • Symfony: Request input sources, Doctrine raw queries, Twig response sinks, redirect sinks
  • WordPress: $wpdb query methods, wp_redirect, esc_sql/sanitize_text_field sanitizers

Security Queries (php/ql/src/queries/security/)

CWE Query Description
CWE-089 SQL Injection Taint from user input to SQL execution
CWE-079 Reflected XSS Taint from user input to HTML output
CWE-078 Command Injection Taint from user input to system commands
CWE-022 Path Injection Taint from user input to file system access
CWE-094 Code Injection Taint from user input to eval/code execution
CWE-502 Unsafe Deserialization Taint from user input to unserialize
CWE-918 SSRF Taint from user input to outbound HTTP requests
CWE-601 Open Redirect Taint from user input to redirect targets

All queries include sanitizer models (type casts, validation functions, framework-specific sanitizers).

Extractor (php/extractor/)

  • Rust-based extractor using tree-sitter-php for parsing
  • Autobuilder that discovers and processes .php/.phtml files
  • CodeQL trap file generator
  • Database schema (~38KB) and TreeSitter QL mapping (~26KB) auto-generated from tree-sitter-php grammar

Tests (php/ql/test/)

  • Library tests for AST, dataflow, and CFG
  • Query tests for all 8 security queries

Compilation Status

✅ All 8 security queries compile successfully with CodeQL CLI v2.25.2.

Architecture

The implementation follows the same patterns used by Ruby and Rust:

  1. Tree-sitter grammar → auto-generated php.dbscheme and TreeSitter.qll
  2. Public AST classes extend TreeSitter node types to provide a clean API
  3. Shared libraries (codeql/dataflow, codeql/controlflow, codeql/ssa, codeql/concepts) provide the core analysis infrastructure
  4. Framework models implement the security concepts for PHP-specific frameworks
  5. Security queries use taint tracking configurations to find vulnerabilities

Next Steps

  • Expand CFG to handle more PHP-specific constructs (match expressions, named arguments)
  • Add type inference for better method dispatch
  • Expand framework coverage (Drupal, CodeIgniter, CakePHP)
  • Add more security queries (LDAP injection, XML injection, log injection)
  • Run QL tests to validate query results
  • Performance tuning for large PHP codebases

helen and others added 16 commits April 28, 2026 16:38
Add comprehensive PHP support to CodeQL including:

- Tree-sitter-based dbscheme with full PHP AST representation
- QL standard library: AST (expressions, statements, declarations,
  literals, variables, calls, parameters, types, operations, namespaces)
- Internal TreeSitter.qll mapping tree-sitter nodes to QL classes
- Dataflow and taint tracking infrastructure
- Security concepts: SqlExecution, HtmlConstruction,
  SystemCommandExecution, FileSystemAccess, CodeExecution
- Framework models: PHP superglobals ($_GET, $_POST, etc.),
  PDO, MySQLi, and built-in functions (eval, exec, system, etc.)
- Security queries: SQL injection (CWE-089), XSS (CWE-079),
  path injection (CWE-022), code injection (CWE-094),
  command injection (CWE-078)
- Query suites for code scanning and security-extended
- Build system integration (BUILD.bazel, qlpack.yml files)
- Extractor configuration and tool scripts
- Tests for AST, dataflow, and all security queries
- CODEOWNERS entry for /php/

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Implement the PHP extractor binary following the Ruby extractor pattern:

- Cargo.toml: depends on tree-sitter-php 0.24.1, codeql-extractor shared lib
- main.rs: CLI with Extract/Generate/Autobuild subcommands
- extractor.rs: parallel file extraction using tree_sitter_php::LANGUAGE_PHP
  and tree_sitter_php::PHP_NODE_TYPES, with overlay support
- generator.rs: generates dbscheme and TreeSitter.qll from node types
- autobuilder.rs: wraps shared autobuilder for .php/.phtml files
- BUILD.bazel: Bazel build config for the extractor binary

Simplified compared to Ruby extractor:
- No ERB template handling (tree-sitter-php handles <?php ?> natively)
- No encoding directive scanning (PHP is overwhelmingly UTF-8)
- No character encoding conversion

Also fixes qltest scripts to remove composer.json from file indexing
(composer.json is JSON, not PHP, and would cause parse errors).

Next steps:
- Run misc/bazel/3rdparty/update_cargo_deps.sh to regenerate Cargo.lock
  and Bazel dependency metadata
- Verify compilation with cargo check -p codeql-extractor-php

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Generated by misc/bazel/3rdparty/update_cargo_deps.sh after adding
php/extractor to the workspace. Resolves tree-sitter-php to 0.24.2.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace hand-written php.dbscheme and TreeSitter.qll with auto-generated
versions from the PHP extractor's generator (tree-sitter-php 0.24.2).

Update all QL library files to match the generated API:
- Use @php_expression/@php_statement/@php_literal/@php_type__ instead
  of @php_underscore_* naming
- InstanceofExpr: modeled as BinaryExpr with operator "instanceof"
  (not a separate node type in tree-sitter-php)
- SilenceExpr: use ErrorSuppressionExpression (generated name)
- ThrowStmt: modeled as ExprStmt wrapping ThrowExpression (PHP 8+)
- Replace getNameString() with getValue() (Name extends Token)
- Replace getExpression() with getChild() (ExpressionStatement, etc.)
- Replace getAnArgument()/getArgument(i) with Arguments.getChild()
- Replace getObject()/getIndex() with getChild(0)/getChild(1)
- Replace getVariableName() with getName() (SimpleParameter)
- Replace getDefault() with getDefaultValue() (SimpleParameter)
- Fix EchoStatement.getExpression() -> getChild()
- Fix Http.qll superglobal name lookup via getChild().getValue()

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Implement the InputSig<Location> interface for PHP dataflow:
- DataFlowPrivate.qll: Core implementation with DataFlowCallable,
  DataFlowCall, ReturnKind, local flow steps, type system stubs,
  content stubs, position types, and all required predicates
- DataFlowPublic.qll: Node hierarchy (ExprNode, ParameterNode,
  ArgumentNode, ReturnNode, OutNode, PostUpdateNode, CastNode)
- DataFlowImplSpecific.qll: PhpDataFlow implements InputSig<Location>
- TaintTrackingPrivate.qll: Taint-specific predicates (stubs)
- TaintTrackingImplSpecific.qll: PhpTaintTracking implements InputSig
- TaintTrackingPublic.qll: Public localTaint predicate

Design decisions:
- Node extends Php::AstNode for backward compatibility with frameworks
- Single DataFlowType (PHP is dynamically typed)
- Integer-based parameter/argument positions
- Inter-procedural dispatch: function-call-to-definition by name
- Empty Content model (field/array flow deferred)
- PostUpdateNode empty (deferred)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add defaultAdditionalTaintStep for common PHP taint propagation:
- Array subscript: $arr["key"] is tainted if $arr is tainted
- Property access: $obj->prop is tainted if $obj is tainted
- String concatenation (.): taint propagates from either operand
- Encapsed strings: taint propagates from interpolated variables
- Heredoc: taint propagates from interpolated content

These steps enable security queries to find results for patterns
like $_GET["name"] flowing into SQL queries or echo statements.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add name-based variable flow steps in simpleLocalFlowStep so that
patterns like $x = $_GET["name"]; sink($x); now connect through
dataflow. This is the critical missing piece for security queries to
produce results.

Implementation:
- isVariableBinding: positive predicate identifying all write/binding
  contexts (assignments, augmented/reference assignments, parameters,
  catch vars, static vars, update expressions, property declarations,
  global declarations, use clauses, list destructuring)
- isVariableRead: VariableName nodes not in any binding context
- variableWrite/variableReadAt: cached helpers factored on (callable,
  name, line) for efficient joining
- Source-order constraint (writeLine <= readLine) prevents impossible
  backward flows without requiring full CFG

Also adds RHS → expression flow for ReferenceAssignmentExpression and
AugmentedAssignmentExpression (previously only regular AssignmentExpression
had this).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Implement the CFG for PHP using the shared controlflow library. This
covers all major control flow constructs:

- Sequential flow through compound statements and expressions
- If/elseif/else branching
- While, do-while, for, foreach loops with break/continue
- Switch statements with case/default
- Try/catch/finally (simplified sequential model)
- Return, break, continue, throw as abnormal completions

The implementation uses no splitting (over-approximates boolean
branches) and handles all AST nodes through default tree classes
ordered by source location.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Implement Static Single Assignment (SSA) form for PHP local variables,
replacing the imprecise name-based variable flow with precise def-use
chains powered by the control flow graph.

Key components:
- BasicBlocks.qll: Exposes basic blocks from the CFG, implements CfgSig
  for use with the shared SSA library
- SsaImpl.qll: SSA input implementation with canonical (scope, name)
  source variables, comprehensive write/read detection
- SSA.qll: Public API for SSA definitions and reads
- DataFlowPrivate.qll: Updated to use SSA-based flow with per-read
  fallback to name-based flow for uncovered cases

Variable write forms handled:
- Simple/reference/augmented assignments
- Update expressions (++/--)
- Parameters (at entry block)
- Catch clause variables
- Foreach binding variables
- Static variable declarations

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Extend the dataflow implementation with three capabilities essential for
end-to-end taint tracking:

Inter-procedural flow:
- viableCallable resolves function calls to FunctionDefinitions by name
- viableCallable resolves method/static/nullsafe calls to MethodDeclarations
- getCalledFunctionName handles qualified names and all call expression types

Lambda/closure flow:
- lambdaCreation identifies anonymous functions and arrow functions
- lambdaCall identifies variable function calls ($fn())
- Enables tracking taint through higher-order function patterns

Content flow (arrays and properties):
- ArrayElementContent tracks data through array subscript operations
- PropertyContent(name) tracks data through named property accesses
- storeStep: array assignment, property assignment, array literal creation
- readStep: subscript access, property access, nullsafe property access

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ccess

- Create Laravel.qll with Request input sources, DB::raw/whereRaw sinks
- Create Symfony.qll with Request sources and Response sinks
- Create Wordpress.qll with $wpdb sinks and wp_redirect models
- Fix argument value access in all framework models (getArgumentValue
  instead of getArgument().getValue() which referenced non-existent API)
- Add getArgumentValue() convenience method to Call class
- Update Frameworks.qll to import all framework modules

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- SQL injection: intval/floatval, numeric casts, real_escape_string,
  addslashes, PDO::quote
- XSS: htmlspecialchars, htmlentities, strip_tags, urlencode, json_encode,
  numeric casts
- Command injection: escapeshellarg, escapeshellcmd
- Path injection: basename, realpath
- Code injection: numeric casts, intval/floatval

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Add Deserialization, RequestForgery, RedirectSink concepts
- Add unserialize() as deserialization sink
- Add file_get_contents/fopen/curl_setopt as SSRF sinks
- Add header() as redirect sink
- Add Laravel redirect() and WordPress wp_redirect() redirect models
- Fix WordPress wp_redirect modeling (was FileSystemAccess, now RedirectSink)
- Create query files for CWE-502, CWE-918, CWE-601

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Library tests: AST functions, dataflow local flow steps, CFG basic blocks
- Query tests: SQL injection, XSS, command injection, code injection,
  path injection, deserialization, SSRF, open redirect
- Each test includes PHP source with vulnerable and safe patterns
- Test queries exercise the full source → sink pipeline

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Add overlay[local] module annotations to all files that import TreeSitter
  or depend on overlay-local entities (AST, CFG, SSA, dataflow files)
- Rewrite CfgImpl.qll to use shared CFG library patterns correctly:
  - Use CfgShared::Make instead of MakeWithSplitting (no splitting needed)
  - Fix circular dependency by using CfgScopeImpl.entry/exit pattern
  - Add stable AST node IDs via equivalenceRelation
  - Use shared tree classes (StandardPreOrderTree, PostOrderTree, LeafTree)
- Make AST.qll AstNode extend Php::AstNode from TreeSitter (inherits
  toString, getAPrimaryQlClass, getLocation, getParent)
- Remove redundant getAPrimaryQlClass overrides from all public AST classes
  (TreeSitter provides these via final override)
- Fix Literal.qll: remove references to non-existent @php_token_string type
  and non-existent predicates
- Add TrueCompletion/FalseCompletion classes to Completion.qll
- Fix SSA adjacentUseUse call signature to match shared library
- Fix BasicBlocks.qll Cfg module to use BasicBlocksImpl types directly
- Add missing DataFlow import to query files
- Create Locations.qll, FileSystem.qll, php.dbscheme.stats
- Add override annotation to ContentApprox.toString

All 8 security queries now compile successfully.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…upport

- All 8 query modules now implement DataFlow::ConfigSig and create
  Flow = TaintTracking::Global<Config> modules
- All 8 .ql files use Flow::PathGraph, PathNode, and flowPath()
  for proper path-problem query format
- Fix redundant instanceof checks in CfgImpl.qll TryStatementTree
- All queries compile with zero warnings

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request introduces first-class PHP support to the CodeQL repo, including a tree-sitter-based extractor, a PHP QL standard library (AST/CFG/SSA/dataflow/taint), framework models, security queries, and a corresponding test suite, plus the Bazel/Cargo wiring needed to build and package it.

Changes:

  • Added a Rust-based PHP extractor (tree-sitter-php) with autobuild/indexing tooling and packaging configuration.
  • Added PHP QL packs (library + queries), suites, framework models, and security queries (including path-problem queries).
  • Added PHP QL tests (library tests + query tests) and repository integration updates (Bazel deps, CODEOWNERS, workspace Cargo members).
Show a summary per file
File Description
php/tools/qltest.sh Adds qltest indexing script for PHP on POSIX.
php/tools/qltest.cmd Adds qltest indexing script for PHP on Windows.
php/tools/index-files.sh Adds file-list extraction entrypoint (POSIX).
php/tools/index-files.cmd Adds file-list extraction entrypoint (Windows).
php/tools/BUILD.bazel Packages PHP tool scripts into the distribution.
php/tools/autobuild.sh Adds PHP autobuild entrypoint (POSIX).
php/tools/autobuild.cmd Adds PHP autobuild entrypoint (Windows).
php/ql/test/query-tests/security/cwe-918/Ssrf.ql Adds SSRF query test harness.
php/ql/test/query-tests/security/cwe-918/Ssrf.php Adds SSRF test code sample.
php/ql/test/query-tests/security/cwe-601/OpenRedirect.ql Adds open-redirect query test harness.
php/ql/test/query-tests/security/cwe-601/OpenRedirect.php Adds open-redirect test code sample.
php/ql/test/query-tests/security/cwe-502/UnsafeDeserialization.ql Adds unsafe-deserialization query test harness.
php/ql/test/query-tests/security/cwe-502/UnsafeDeserialization.php Adds unsafe-deserialization test code sample.
php/ql/test/query-tests/security/cwe-094/test.php Adds code-injection test code sample.
php/ql/test/query-tests/security/cwe-094/CodeInjectionTest.ql Adds code-injection “problem” style test query.
php/ql/test/query-tests/security/cwe-094/CodeInjection.ql Adds code-injection query test harness.
php/ql/test/query-tests/security/cwe-094/CodeInjection.php Adds code-injection test code sample.
php/ql/test/query-tests/security/cwe-089/test.php Adds SQLi test code sample.
php/ql/test/query-tests/security/cwe-089/SqlInjectionTest.ql Adds SQLi “problem” style test query.
php/ql/test/query-tests/security/cwe-089/SqlInjection.ql Adds SQLi query test harness.
php/ql/test/query-tests/security/cwe-089/SqlInjection.php Adds SQLi test code sample.
php/ql/test/query-tests/security/cwe-079/test.php Adds reflected-XSS test code sample.
php/ql/test/query-tests/security/cwe-079/ReflectedXssTest.ql Adds reflected-XSS “problem” style test query.
php/ql/test/query-tests/security/cwe-079/ReflectedXSS.ql Adds reflected-XSS query test harness.
php/ql/test/query-tests/security/cwe-079/ReflectedXSS.php Adds reflected-XSS test code sample.
php/ql/test/query-tests/security/cwe-078/test.php Adds command-injection test code sample.
php/ql/test/query-tests/security/cwe-078/CommandInjectionTest.ql Adds command-injection “problem” style test query.
php/ql/test/query-tests/security/cwe-078/CommandInjection.ql Adds command-injection query test harness.
php/ql/test/query-tests/security/cwe-078/CommandInjection.php Adds command-injection test code sample.
php/ql/test/query-tests/security/cwe-022/test.php Adds path-injection test code sample.
php/ql/test/query-tests/security/cwe-022/PathInjectionTest.ql Adds path-injection “problem” style test query.
php/ql/test/query-tests/security/cwe-022/PathInjection.ql Adds path-injection query test harness.
php/ql/test/query-tests/security/cwe-022/PathInjection.php Adds path-injection test code sample.
php/ql/test/qlpack.yml Defines the PHP tests qlpack.
php/ql/test/library-tests/dataflow/test.php Adds PHP source for dataflow library tests.
php/ql/test/library-tests/dataflow/LocalFlowStep.ql Adds local flow-step library test query.
php/ql/test/library-tests/dataflow/DataFlowTest.ql Adds general dataflow library test query.
php/ql/test/library-tests/controlflow/test.php Adds PHP source for CFG library tests.
php/ql/test/library-tests/controlflow/BasicBlocks.ql Adds basic-blocks library test query.
php/ql/test/library-tests/ast/test.php Adds PHP source for AST library tests.
php/ql/test/library-tests/ast/Functions.ql Adds AST function-node library test query.
php/ql/test/library-tests/ast/AstNodes.ql Adds AST node enumeration library test query.
php/ql/src/queries/security/cwe-918/Ssrf.ql Adds SSRF path-problem query.
php/ql/src/queries/security/cwe-601/OpenRedirect.ql Adds open-redirect path-problem query.
php/ql/src/queries/security/cwe-502/UnsafeDeserialization.ql Adds unsafe-deserialization path-problem query.
php/ql/src/queries/security/cwe-094/CodeInjection.ql Adds code-injection path-problem query.
php/ql/src/queries/security/cwe-089/SqlInjection.ql Adds SQLi path-problem query.
php/ql/src/queries/security/cwe-079/ReflectedXSS.ql Adds reflected-XSS path-problem query.
php/ql/src/queries/security/cwe-078/CommandInjection.ql Adds command-injection path-problem query.
php/ql/src/queries/security/cwe-022/PathInjection.ql Adds path-injection path-problem query.
php/ql/src/qlpack.yml Defines the PHP queries qlpack.
php/ql/src/codeql-suites/php-security-extended.qls Adds PHP security-extended suite.
php/ql/src/codeql-suites/php-code-scanning.qls Adds PHP default code-scanning suite.
php/ql/lib/qlpack.yml Defines the PHP standard library qlpack.
php/ql/lib/php.qll Adds top-level PHP library entrypoint import file.
php/ql/lib/php.dbscheme.stats Adds dbscheme stats file for the PHP DB schema.
php/ql/lib/Customizations.qll Adds customization hook module documentation.
php/ql/lib/codeql/php/TaintTracking.qll Exposes taint-tracking module wiring for PHP.
php/ql/lib/codeql/php/security/UnsafeDeserializationQuery.qll Adds unsafe-deserialization taint configuration module.
php/ql/lib/codeql/php/security/UnsafeDeserializationCustomizations.qll Adds default sources/sinks/sanitizers + extension points.
php/ql/lib/codeql/php/security/SsrfQuery.qll Adds SSRF taint configuration module.
php/ql/lib/codeql/php/security/SsrfCustomizations.qll Adds SSRF default sources/sinks/sanitizers + extension points.
php/ql/lib/codeql/php/security/SqlInjectionQuery.qll Adds SQLi taint configuration module.
php/ql/lib/codeql/php/security/SqlInjectionCustomizations.qll Adds SQLi default sources/sinks/sanitizers + extension points.
php/ql/lib/codeql/php/security/ReflectedXssQuery.qll Adds reflected-XSS taint configuration module.
php/ql/lib/codeql/php/security/ReflectedXssCustomizations.qll Adds reflected-XSS default sources/sinks/sanitizers + extension points.
php/ql/lib/codeql/php/security/PathInjectionQuery.qll Adds path-injection taint configuration module.
php/ql/lib/codeql/php/security/PathInjectionCustomizations.qll Adds path-injection default sources/sinks/sanitizers + extension points.
php/ql/lib/codeql/php/security/OpenRedirectQuery.qll Adds open-redirect taint configuration module.
php/ql/lib/codeql/php/security/OpenRedirectCustomizations.qll Adds open-redirect default sources/sinks/sanitizers + extension points.
php/ql/lib/codeql/php/security/CommandInjectionQuery.qll Adds command-injection taint configuration module.
php/ql/lib/codeql/php/security/CommandInjectionCustomizations.qll Adds command-injection default sources/sinks/sanitizers + extension points.
php/ql/lib/codeql/php/security/CodeInjectionQuery.qll Adds code-injection taint configuration module.
php/ql/lib/codeql/php/security/CodeInjectionCustomizations.qll Adds code-injection default sources/sinks/sanitizers + extension points.
php/ql/lib/codeql/php/frameworks/Wordpress.qll Adds WordPress framework modeling.
php/ql/lib/codeql/php/frameworks/Symfony.qll Adds Symfony framework modeling.
php/ql/lib/codeql/php/frameworks/Pdo.qll Adds PDO modeling.
php/ql/lib/codeql/php/frameworks/Mysqli.qll Adds MySQLi modeling.
php/ql/lib/codeql/php/frameworks/Laravel.qll Adds Laravel framework modeling.
php/ql/lib/codeql/php/frameworks/Http.qll Adds modeling for PHP superglobals and php://input.
php/ql/lib/codeql/php/frameworks/Builtins.qll Adds modeling for built-in sinks/sources via concepts.
php/ql/lib/codeql/php/Frameworks.qll Adds convenience import for all framework models.
php/ql/lib/codeql/php/Diagnostics.qll Adds extractor diagnostics representation.
php/ql/lib/codeql/php/dataflow/SSA.qll Adds public SSA wrapper module/classes.
php/ql/lib/codeql/php/dataflow/RemoteFlowSources.qll Adds extension point for remote input sources.
php/ql/lib/codeql/php/dataflow/internal/TaintTrackingPublic.qll Adds public taint helpers (PHP-specific facade).
php/ql/lib/codeql/php/dataflow/internal/TaintTrackingPrivate.qll Adds PHP-specific taint defaults/additional steps.
php/ql/lib/codeql/php/dataflow/internal/TaintTrackingImplSpecific.qll Wires PHP into shared taint library input signature.
php/ql/lib/codeql/php/dataflow/internal/SsaImpl.qll Implements SSA for PHP using shared SSA library.
php/ql/lib/codeql/php/dataflow/internal/DataFlowPublic.qll Adds public PHP dataflow node classes.
php/ql/lib/codeql/php/dataflow/internal/DataFlowPrivate.qll Implements PHP-specific dataflow predicates for shared library.
php/ql/lib/codeql/php/dataflow/internal/DataFlowImplSpecific.qll Wires PHP into shared dataflow library input signature.
php/ql/lib/codeql/php/dataflow/FlowSummary.qll Adds placeholder for flow-summary support.
php/ql/lib/codeql/php/DataFlow.qll Adds public PHP dataflow entrypoint module.
php/ql/lib/codeql/php/controlflow/internal/Completion.qll Adds PHP completion kinds for CFG construction.
php/ql/lib/codeql/php/controlflow/internal/CfgImpl.qll Implements PHP CFG via shared controlflow library.
php/ql/lib/codeql/php/controlflow/BasicBlocks.qll Exposes PHP CFG basic blocks and nodes.
php/ql/lib/codeql/php/Concepts.qll Adds PHP security concepts (sources/sinks via frameworks).
php/ql/lib/codeql/php/CFG.qll Adds public CFG entrypoint module.
php/ql/lib/codeql/php/ast/Variable.qll Adds public AST wrapper for variables.
php/ql/lib/codeql/php/ast/Type.qll Adds public AST wrapper for type annotations.
php/ql/lib/codeql/php/ast/Stmt.qll Adds public AST wrapper classes for statements.
php/ql/lib/codeql/php/ast/Parameter.qll Adds public AST wrapper for parameters.
php/ql/lib/codeql/php/ast/Operation.qll Adds public AST wrapper classes for operations.
php/ql/lib/codeql/php/ast/Namespace.qll Adds public AST wrapper for namespaces/use declarations.
php/ql/lib/codeql/php/ast/Literal.qll Adds public AST wrapper for literals.
php/ql/lib/codeql/php/ast/internal/AST.qll Adds internal helpers for AST node conversions.
php/ql/lib/codeql/php/ast/Expr.qll Adds public AST wrapper classes for expressions.
php/ql/lib/codeql/php/ast/Declaration.qll Adds public AST wrapper for declarations.
php/ql/lib/codeql/php/ast/Call.qll Adds public AST wrapper for calls and arguments.
php/ql/lib/codeql/php/AST.qll Adds public AST entrypoint and file abstractions.
php/ql/lib/codeql/Locations.qll Adds PHP-local locations wrapper.
php/ql/lib/codeql/files/FileSystem.qll Adds PHP-local filesystem wrapper.
php/ql/lib/change-notes/0.0.1.md Adds initial PHP library change notes.
php/extractor/src/main.rs Adds extractor CLI entrypoint.
php/extractor/src/generator.rs Adds dbscheme/QLL generator entrypoint.
php/extractor/src/extractor.rs Adds extraction implementation (parallel extraction + overlay support).
php/extractor/src/autobuilder.rs Adds autobuilder wiring for PHP databases.
php/extractor/Cargo.toml Adds Cargo manifest for the PHP extractor.
php/extractor/BUILD.bazel Adds Bazel build rule for the PHP extractor binary.
php/codeql-extractor.yml Adds extractor metadata/config for PHP language integration.
php/BUILD.bazel Adds Bazel packaging for PHP extractor + dbscheme + tools.
MODULE.bazel Adds tree-sitter-php vendored repo to Bazel module deps.
misc/bazel/3rdparty/tree_sitter_extractors_deps/defs.bzl Adds tree-sitter-php crate wiring and aliases for Bazel.
misc/bazel/3rdparty/tree_sitter_extractors_deps/BUILD.tree-sitter-php-0.24.2.bazel Adds generated Bazel build file for vendored tree-sitter-php crate.
misc/bazel/3rdparty/tree_sitter_extractors_deps/BUILD.bazel Exposes tree-sitter-php aliases from the 3rdparty package.
CODEOWNERS Adds PHP ownership entries.
Cargo.toml Adds php/extractor to the workspace members.
Cargo.lock Adds lock entries for codeql-extractor-php and tree-sitter-php.

Copilot's findings

  • Files reviewed: 130/131 changed files
  • Comments generated: 4

Comment on lines +50 to +54
/**
* WordPress functions that output HTML: `the_title()`, `the_content()`,
* `the_excerpt()`, `bloginfo()`.
* These echo their output and are thus HTML construction sinks if user data flows in.
*/
Comment on lines +12 to +15
/**
* A call to `$request->get()`, `$request->query->get()`, `$request->request->get()`,
* `$request->getContent()`, etc. on a Symfony Request object.
*/
Comment on lines +28 to +30
/**
* A call to `assert()` with a string argument, modeled as code execution.
*/
Comment on lines +100 to +102
/**
* An `echo` statement or `print` expression in an HTML context, modeled as HTML construction.
*/
Address PR review comments:
- Wordpress.qll: WpOutputFunction docs now list wp_die/wp_send_json
- Symfony.qll: SymfonyRequestInput docs now list actual modeled methods
- Builtins.qll: AssertCall docs clarified for all assert() calls
- Builtins.qll: EchoAsHtmlSink docs now say echo only (not print)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces first-class PHP support into the CodeQL repo, including a tree-sitter-based PHP extractor, a PHP QL standard library (AST/CFG/SSA/dataflow/taint tracking), framework models, security queries, and accompanying tests/suites to integrate PHP into CodeQL’s packaging and build ecosystem.

Changes:

  • Adds a Rust-based PHP extractor (extract / generate / autobuild) plus indexing/qltest helper scripts.
  • Introduces a PHP QL library with core analysis layers (AST/CFG/SSA/dataflow/taint tracking), security concepts, and framework models.
  • Adds PHP security queries (8 CWEs), test packs, and suite definitions; wires PHP into Bazel/Cargo deps and CODEOWNERS.
Show a summary per file
File Description
php/tools/qltest.sh PHP qltest indexing helper (sh).
php/tools/qltest.cmd PHP qltest indexing helper (cmd).
php/tools/index-files.sh PHP extractor entrypoint for file-list extraction (sh).
php/tools/index-files.cmd PHP extractor entrypoint for file-list extraction (cmd).
php/tools/BUILD.bazel Packages PHP tool scripts into the distribution.
php/tools/autobuild.sh PHP autobuild entrypoint (sh).
php/tools/autobuild.cmd PHP autobuild entrypoint (cmd).
php/ql/test/query-tests/security/cwe-918/Ssrf.ql SSRF query test driver (QL).
php/ql/test/query-tests/security/cwe-918/Ssrf.php SSRF test cases (PHP).
php/ql/test/query-tests/security/cwe-601/OpenRedirect.ql Open redirect query test driver (QL).
php/ql/test/query-tests/security/cwe-601/OpenRedirect.php Open redirect test cases (PHP).
php/ql/test/query-tests/security/cwe-502/UnsafeDeserialization.ql Unsafe deserialization query test driver (QL).
php/ql/test/query-tests/security/cwe-502/UnsafeDeserialization.php Unsafe deserialization test cases (PHP).
php/ql/test/query-tests/security/cwe-094/test.php Code injection test cases (PHP).
php/ql/test/query-tests/security/cwe-094/CodeInjectionTest.ql Code injection problem-style test query (QL).
php/ql/test/query-tests/security/cwe-094/CodeInjection.ql Code injection query test driver (QL).
php/ql/test/query-tests/security/cwe-094/CodeInjection.php Code injection test cases (PHP).
php/ql/test/query-tests/security/cwe-089/test.php SQL injection test cases (PHP).
php/ql/test/query-tests/security/cwe-089/SqlInjectionTest.ql SQL injection problem-style test query (QL).
php/ql/test/query-tests/security/cwe-089/SqlInjection.ql SQL injection query test driver (QL).
php/ql/test/query-tests/security/cwe-089/SqlInjection.php SQL injection test cases (PHP).
php/ql/test/query-tests/security/cwe-079/test.php Reflected XSS test cases (PHP).
php/ql/test/query-tests/security/cwe-079/ReflectedXssTest.ql Reflected XSS problem-style test query (QL).
php/ql/test/query-tests/security/cwe-079/ReflectedXSS.ql Reflected XSS query test driver (QL).
php/ql/test/query-tests/security/cwe-079/ReflectedXSS.php Reflected XSS test cases (PHP).
php/ql/test/query-tests/security/cwe-078/test.php Command injection test cases (PHP).
php/ql/test/query-tests/security/cwe-078/CommandInjectionTest.ql Command injection problem-style test query (QL).
php/ql/test/query-tests/security/cwe-078/CommandInjection.ql Command injection query test driver (QL).
php/ql/test/query-tests/security/cwe-078/CommandInjection.php Command injection test cases (PHP).
php/ql/test/query-tests/security/cwe-022/test.php Path injection test cases (PHP).
php/ql/test/query-tests/security/cwe-022/PathInjectionTest.ql Path injection problem-style test query (QL).
php/ql/test/query-tests/security/cwe-022/PathInjection.ql Path injection query test driver (QL).
php/ql/test/query-tests/security/cwe-022/PathInjection.php Path injection test cases (PHP).
php/ql/test/qlpack.yml PHP test pack definition.
php/ql/test/library-tests/dataflow/test.php Dataflow library test fixture (PHP).
php/ql/test/library-tests/dataflow/LocalFlowStep.ql Dataflow step library test (QL).
php/ql/test/library-tests/dataflow/DataFlowTest.ql Dataflow node typing/library test (QL).
php/ql/test/library-tests/controlflow/test.php CFG library test fixture (PHP).
php/ql/test/library-tests/controlflow/BasicBlocks.ql Basic blocks library test (QL).
php/ql/test/library-tests/ast/test.php AST extraction library test fixture (PHP).
php/ql/test/library-tests/ast/Functions.ql AST functions library test (QL).
php/ql/test/library-tests/ast/AstNodes.ql AST node inventory/library test (QL).
php/ql/src/queries/security/cwe-918/Ssrf.ql SSRF security query (QL).
php/ql/src/queries/security/cwe-601/OpenRedirect.ql Open redirect security query (QL).
php/ql/src/queries/security/cwe-502/UnsafeDeserialization.ql Unsafe deserialization security query (QL).
php/ql/src/queries/security/cwe-094/CodeInjection.ql Code injection security query (QL).
php/ql/src/queries/security/cwe-089/SqlInjection.ql SQL injection security query (QL).
php/ql/src/queries/security/cwe-079/ReflectedXSS.ql Reflected XSS security query (QL).
php/ql/src/queries/security/cwe-078/CommandInjection.ql Command injection security query (QL).
php/ql/src/queries/security/cwe-022/PathInjection.ql Path injection security query (QL).
php/ql/src/qlpack.yml PHP queries pack definition.
php/ql/src/codeql-suites/php-security-extended.qls PHP security-extended suite definition.
php/ql/src/codeql-suites/php-code-scanning.qls PHP code-scanning suite definition.
php/ql/lib/qlpack.yml PHP library pack definition.
php/ql/lib/php.qll Public convenience import for PHP library layers.
php/ql/lib/php.dbscheme.stats PHP dbscheme stats placeholder file.
php/ql/lib/Customizations.qll Standard library customization hook for PHP.
php/ql/lib/codeql/php/TaintTracking.qll Public taint-tracking entrypoint for PHP.
php/ql/lib/codeql/php/security/UnsafeDeserializationQuery.qll Unsafe deserialization taint config (library).
php/ql/lib/codeql/php/security/UnsafeDeserializationCustomizations.qll Unsafe deserialization sources/sinks/sanitizers (library).
php/ql/lib/codeql/php/security/SsrfQuery.qll SSRF taint config (library).
php/ql/lib/codeql/php/security/SsrfCustomizations.qll SSRF sources/sinks/sanitizers (library).
php/ql/lib/codeql/php/security/SqlInjectionQuery.qll SQL injection taint config (library).
php/ql/lib/codeql/php/security/SqlInjectionCustomizations.qll SQL injection sources/sinks/sanitizers (library).
php/ql/lib/codeql/php/security/ReflectedXssQuery.qll Reflected XSS taint config (library).
php/ql/lib/codeql/php/security/ReflectedXssCustomizations.qll Reflected XSS sources/sinks/sanitizers (library).
php/ql/lib/codeql/php/security/PathInjectionQuery.qll Path injection taint config (library).
php/ql/lib/codeql/php/security/PathInjectionCustomizations.qll Path injection sources/sinks/sanitizers (library).
php/ql/lib/codeql/php/security/OpenRedirectQuery.qll Open redirect taint config (library).
php/ql/lib/codeql/php/security/OpenRedirectCustomizations.qll Open redirect sources/sinks/sanitizers (library).
php/ql/lib/codeql/php/security/CommandInjectionQuery.qll Command injection taint config (library).
php/ql/lib/codeql/php/security/CommandInjectionCustomizations.qll Command injection sources/sinks/sanitizers (library).
php/ql/lib/codeql/php/security/CodeInjectionQuery.qll Code injection taint config (library).
php/ql/lib/codeql/php/security/CodeInjectionCustomizations.qll Code injection sources/sinks/sanitizers (library).
php/ql/lib/codeql/php/frameworks/Wordpress.qll WordPress framework modeling.
php/ql/lib/codeql/php/frameworks/Symfony.qll Symfony framework modeling.
php/ql/lib/codeql/php/frameworks/Pdo.qll PDO framework/API modeling.
php/ql/lib/codeql/php/frameworks/Mysqli.qll MySQLi framework/API modeling.
php/ql/lib/codeql/php/frameworks/Laravel.qll Laravel framework modeling.
php/ql/lib/codeql/php/frameworks/Http.qll Superglobals / HTTP-related source modeling.
php/ql/lib/codeql/php/frameworks/Builtins.qll Built-in sinks/sources modeling (eval/exec/etc.).
php/ql/lib/codeql/php/Frameworks.qll Aggregates framework modeling imports.
php/ql/lib/codeql/php/Diagnostics.qll Extraction diagnostics modeling.
php/ql/lib/codeql/php/dataflow/SSA.qll Public SSA entrypoint for PHP.
php/ql/lib/codeql/php/dataflow/RemoteFlowSources.qll Public remote input extension points.
php/ql/lib/codeql/php/dataflow/internal/TaintTrackingPublic.qll Public taint predicates wiring for PHP.
php/ql/lib/codeql/php/dataflow/internal/TaintTrackingPrivate.qll PHP-specific taint step definitions.
php/ql/lib/codeql/php/dataflow/internal/TaintTrackingImplSpecific.qll Shared taint tracking input wiring.
php/ql/lib/codeql/php/dataflow/internal/SsaImpl.qll SSA implementation for PHP.
php/ql/lib/codeql/php/dataflow/internal/DataFlowPublic.qll Public dataflow node types for PHP.
php/ql/lib/codeql/php/dataflow/internal/DataFlowPrivate.qll PHP-specific dataflow step definitions.
php/ql/lib/codeql/php/dataflow/internal/DataFlowImplSpecific.qll Shared dataflow input wiring.
php/ql/lib/codeql/php/dataflow/FlowSummary.qll Placeholder flow summary module.
php/ql/lib/codeql/php/DataFlow.qll Public dataflow entrypoint for PHP.
php/ql/lib/codeql/php/controlflow/internal/Completion.qll CFG completion modeling for PHP.
php/ql/lib/codeql/php/controlflow/internal/CfgImpl.qll CFG implementation for PHP (shared controlflow integration).
php/ql/lib/codeql/php/controlflow/BasicBlocks.qll Basic block API for PHP CFG.
php/ql/lib/codeql/php/Concepts.qll Security concept classes for PHP modeling.
php/ql/lib/codeql/php/CFG.qll Public CFG entrypoint for PHP.
php/ql/lib/codeql/php/ast/Variable.qll Public AST variable APIs.
php/ql/lib/codeql/php/ast/Type.qll Public AST type annotation APIs.
php/ql/lib/codeql/php/ast/Stmt.qll Public AST statement APIs.
php/ql/lib/codeql/php/ast/Parameter.qll Public AST parameter APIs.
php/ql/lib/codeql/php/ast/Operation.qll Public AST operation APIs.
php/ql/lib/codeql/php/ast/Namespace.qll Public AST namespace/use APIs.
php/ql/lib/codeql/php/ast/Literal.qll Public AST literal APIs.
php/ql/lib/codeql/php/ast/internal/AST.qll Internal AST helpers.
php/ql/lib/codeql/php/ast/Expr.qll Public AST expression APIs.
php/ql/lib/codeql/php/ast/Declaration.qll Public AST declaration APIs.
php/ql/lib/codeql/php/ast/Call.qll Public AST call APIs.
php/ql/lib/codeql/php/AST.qll Public AST entrypoint for PHP.
php/ql/lib/codeql/Locations.qll PHP-local Locations library wiring.
php/ql/lib/codeql/files/FileSystem.qll PHP-local filesystem abstraction wiring.
php/ql/lib/change-notes/0.0.1.md Initial PHP library change notes.
php/extractor/src/main.rs PHP extractor CLI entrypoint (extract/generate/autobuild).
php/extractor/src/generator.rs PHP dbscheme/QL mapping generator wiring.
php/extractor/src/extractor.rs PHP extraction pipeline implementation.
php/extractor/src/autobuilder.rs PHP autobuilder implementation.
php/extractor/Cargo.toml PHP extractor crate manifest.
php/extractor/BUILD.bazel Bazel build target for PHP extractor binary.
php/codeql-extractor.yml Registers PHP extractor metadata and options.
php/BUILD.bazel Packages PHP extractor/dbscheme/tools into CodeQL distribution.
MODULE.bazel Adds vendored tree-sitter-php repo to Bazel module deps.
misc/bazel/3rdparty/tree_sitter_extractors_deps/defs.bzl Wires PHP extractor crate deps + adds tree-sitter-php vendoring.
misc/bazel/3rdparty/tree_sitter_extractors_deps/BUILD.tree-sitter-php-0.24.2.bazel Generated Bazel build file for tree-sitter-php crate.
misc/bazel/3rdparty/tree_sitter_extractors_deps/BUILD.bazel Adds Bazel aliases for tree-sitter-php.
CODEOWNERS Adds CODEOWNERS entries for PHP areas.
Cargo.toml Adds PHP extractor to the Rust workspace members.
Cargo.lock Locks PHP extractor + tree-sitter-php dependencies.

Copilot's findings

  • Files reviewed: 130/131 changed files
  • Comments generated: 5

Bug fixes:
- Call.qll: Fix getFunctionName() for qualified names — was returning
  'QualifiedName' string literal instead of actual function name
- FileAccessCall doc: Remove incorrect mention of include/require

New security models in Builtins.qll:
- create_function() as code execution sink (CWE-094)
- Backtick shell expressions as command execution sink (CWE-078)
- print expression as HTML construction sink (CWE-079)
- include/include_once/require/require_once as file access sinks (CWE-022)

Test cleanup:
- Remove 5 redundant *Test.ql files (duplicated @kind test versions)
- Use public codeql.php.DataFlow import instead of internal module
- Fix unqualified AstNode reference in AstNodes.ql

All 8 security queries and 13 test queries compile successfully.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@helen

helen commented May 2, 2026

Copy link
Copy Markdown
Owner Author

Alleged testing instructions:

Here's how to test against a real PHP codebase:

  1. Create a CodeQL database from a PHP project
codeql database create php-test-db \
 --language=php \
 --source-root=/path/to/php-project \
 --search-path=/Users/helen/repos/codeql
  1. Run a single query
codeql query run \
  --database=php-test-db \
  --search-path=/Users/helen/repos/codeql \
  php/ql/src/queries/security/cwe-089/SqlInjection.ql
  1. Or run all PHP security queries
codeql database analyze php-test-db \
 --search-path=/Users/helen/repos/codeql \
 php/ql/src/queries/security/ \
 --format=sarif-latest \
 --output=results.sarif

Good test projects:

  • A small WordPress plugin with raw SQL queries
  • A Laravel app with user input handling
  • Any PHP app using $_GET/$_POST with eval, exec, or SQL

Note: The extractor in php/extractor/ needs to be built first (cargo build in that directory) and placed where CodeQL can find it. The --search-path flag tells CodeQL where to find the PHP qlpacks and extractor config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants