Guide to installing and running the latest CodeQL CLI on Windows for AEM multi-module projects.
CodeQL for AEM Projects
Introduction to CodeQL
CodeQL is GitHub's industry-leading semantic code analysis engine. It treats source code like relational data: your codebase is extracted into a specialized database (CodeQL Database), allowing you to execute expressive queries written in QL (an object-oriented query language) to detect security vulnerabilities, dangerous data flows, and code smells before code merges into production.
Key Advantages for Local AEM Development
- Inter-Procedural Taint Tracking: Tracks untrusted input across multiple functions, classes, and service layers (e.g., from an HTTP request parameter in a Sling Servlet down to a JCR session or query execution).
- Multi-Language Support in AEM: Seamlessly scans both the OSGi Java backend (
core) and the client-side JavaScript/TypeScript frontend (ui.frontend). - Standardized Diagnostic Reports: Generates standard SARIF (Static Analysis Results Interchange Format) or CSV outputs that can be inspected directly inside VS Code or integrated into CI/CD pipelines.
1. Installing CodeQL CLI on Windows
The CodeQL Bundle includes the CLI binary, all standard query packs for supported languages, and an embedded Java Runtime Environment (JRE) for running the query engine.
While the CodeQL Bundle includes its own embedded JRE to run the CLI engine, extracting a Java database requires a locally installed JDK compatible with your AEM version (e.g., JDK 11 for AEM 6.5 or JDK 17/21 for AEM as a Cloud Service) configured in your PATH or JAVA_HOME.
1.1 Automatic Setup Script (PowerShell)
Run this PowerShell script to automatically download the latest CodeQL bundle and configure your environment:
1.2 Verifying the Installation
Close and reopen your terminal, then run:
Expected output should show java and javascript in the supported languages list, and query packs like codeql/java-queries and codeql/javascript-queries in the packs list.
2. Core Concepts
Understanding CodeQL terminology helps configure appropriate analysis targets for AEM projects:
Commonly Used Query Suites
code-scanning: The default suite used in GitHub Code Scanning. Highly tuned for high precision with low false-positive rates.security-extended: Extends standard scanning to cover additional common vulnerabilities and lower-severity security risks.security-and-quality: The most comprehensive suite. Includes all security rules plus code maintainability, dead code detection, and code smells.
3. Creating CodeQL Databases for AEM
A typical AEM Maven Archetype project contains multiple modules with distinct languages:
3.1 Creating the Java Database (Maven Integration)
Because Java is a compiled language, CodeQL's extractor must observe javac compiler invocations during an active build. Pass the Maven compilation command via --command:
Always specify clean in mvn clean install. If Maven skips compilation due to existing build artifacts (incremental build), CodeQL will not intercept any javac executions and will fail with the error:
"No source code seen during build".
3.2 Creating the Frontend JS/TS Database (ui.frontend)
The JavaScript/TypeScript extractor does not require a build step; it crawls and indexes files directly from the specified directory:
4. Running Local Analysis & Exporting Results
Once the database has been constructed, run queries against it using codeql database analyze.
4.1 Running the Security and Quality Suite (SARIF Format)
4.2 Exporting Results to CSV (Spreadsheet Review)
If you need to review issues in Excel or Google Sheets, export using --format=csv:
5. Reviewing Findings in Visual Studio Code
Inspecting raw SARIF files in text editors is difficult due to verbose JSON formatting. Using dedicated VS Code extensions enables an interactive security dashboard:
- Install the Extension: Install SARIF Viewer (by Microsoft DevLabs) or the official CodeQL extension from the VS Code Marketplace.
- Load Results: Press Ctrl + Shift + P, run
SARIF: Open SARIF Log, and pickresults-java.sariforresults-js.sarif. - Trace Taint Paths: Clicking on any finding in the SARIF Viewer panel will navigate directly to the affected source code file, highlighting the tainted data-flow path step-by-step from source to sink.
With the CodeQL for Visual Studio Code extension installed, you can add db-java to the database view, author custom .ql queries, and execute them on-the-fly with full syntax highlighting, AST inspection, and auto-complete.
6. Performance Tips & Best Practices
- Maximize Concurrency with
--threads=0: Passes all available CPU cores to the query evaluator and extractor. For systems with limited RAM, specify an explicit thread count (e.g.,--threads=4) to avoid resource exhaustion. - Allocate Sufficient Heap Memory: For large AEM multi-module projects, provide explicit memory limit via
--ram(e.g.,--ram=8192for 8GB) to accelerate QL query compilation and graph evaluation. - Exclude Generated Databases from Version Control: Always add CodeQL output directories and database folders to your
.gitignore:
- Speed Up Maven Database Creation: Use
-DskipTests -B -ntpduringcodeql database createto omit test compilation and eliminate verbose download progress logs, reducing creation time by up to 70%.