Code scanning in Azure is achieved by integrating GitHub Advanced Security (GHAS) with your Azure DevOps repositories or GitHub repositories hosted within the Azure ecosystem. This process automates the detection of security vulnerabilities, such as SQL injection or cross-site scripting, by analyzing your source code every time a developer pushes a change or creates a pull request. By shifting security to the left, you identify flaws during the development phase rather than after the code has been deployed to a production environment.
What You'll Need
- An Azure DevOps organization with an active subscription.
- Administrator permissions for the project and repository you wish to scan.
- GitHub Advanced Security for Azure DevOps enabled (this is a paid add-on license).
- A repository containing code in a supported language (e.g., JavaScript, Python, C#, Java).
- A configured build pipeline (YAML) to trigger the scanning process.
Step-by-Step
- Enable Advanced Security: Navigate to your Azure DevOps Organization Settings. Look for the section labeled "Security" or "Preview Features" (labels may vary slightly by version or region). Locate the GitHub Advanced Security toggle and switch it to "On." You will be prompted to select the specific projects or repositories that should be covered by the license to manage costs.
- Configure Dependency Scanning: Once the global setting is active, go to your specific Project Settings. Select "Repositories" and then choose the repository you want to protect. In the security settings for that repository, enable "Dependency Scanning." This allows Azure to check your third-party libraries against known vulnerability databases.
- Enable Secret Scanning: In the same repository security menu, toggle on "Secret Scanning." This feature scans your commit history and current code for accidentally committed credentials, such as API keys, connection strings, or SSH keys. If a secret is found, Azure will alert you immediately so you can rotate the compromised credential.
- Set Up CodeQL Analysis: To perform deep static analysis, you must integrate CodeQL into your pipeline. Open your YAML pipeline file. Add the "Advanced Security" task to your build steps. This task initializes the CodeQL database, analyzes the code for patterns that indicate vulnerabilities, and uploads the results back to the Azure DevOps interface.
- Define the Build Trigger: Ensure your pipeline is set to run on "pull_request" triggers. This ensures that every single piece of new code is scanned before it is merged into the main branch. In your YAML file, define the trigger section to include the target branches (e.g., main or develop).
- Run the Initial Scan: Manually trigger the pipeline to perform a baseline scan of your existing codebase. This will populate the "Security" tab in your repository with all current alerts.
- Review and Triage Alerts: Navigate to the "Repos" menu and select "Advanced Security." You will see a list of alerts categorized by severity (Critical, High, Medium, Low). Click on an individual alert to see the exact line of code causing the issue and the recommended fix.
- Remediate and Close: Once a developer fixes the code and pushes the change, the scanner will automatically re-evaluate the line. If the vulnerability is gone, the alert will be marked as "Fixed." For false positives, you can manually mark the alert as "Dismissed" and provide a reason for the audit trail.
Common Mistakes
One of the most frequent mistakes is enabling the security features but forgetting to add the actual scanning task to the YAML pipeline. Without the pipeline task, the "Advanced Security" tab will remain empty because the analysis engine never actually ran against the code. Another common error is failing to rotate secrets after they are detected. Simply deleting the line of code that contained the API key does not make the key secure; because the key exists in the git history, it must be invalidated and replaced at the source.
Many teams also overlook the "False Positive" noise. If you do not actively triage alerts, the security tab becomes cluttered with low-risk warnings, leading to "alert fatigue" where developers begin to ignore critical warnings because they are buried under hundreds of insignificant ones. Finally, some users attempt to run scans on unsupported languages. While GHAS supports most major languages, trying to force a scan on a proprietary or obscure language will result in a successful pipeline run but zero findings, which can give a false sense of security.
Tips
To maximize efficiency, integrate the security results directly into your Pull Request (PR) workflow. By configuring the pipeline to fail the build if a "Critical" vulnerability is found, you prevent insecure code from ever reaching your main branch. This creates a hard gate that enforces security standards without requiring a manual security audit for every small change.
Use the "Custom Queries" feature in CodeQL if your organization has specific coding standards or banned functions that aren't covered by the default security rules. This allows you to write your own queries to find patterns specific to your business logic. Additionally, schedule a monthly "Security Debt" review. Instead of just fixing new bugs, dedicate one day a month to clearing out the backlog of Medium and Low alerts to keep the codebase clean and maintainable.
Troubleshooting
If the scan is not running or alerts are not appearing, start with the simplest check: verify that the GitHub Advanced Security license is still active and applied to that specific repository in the Organization Settings. If the license is active but the pipeline fails, check the pipeline logs for "Permission Denied" errors. This usually means the build service account does not have the necessary permissions to write security results back to the repository.
If the pipeline completes successfully but no alerts appear, verify that your code is actually being compiled during the scan. CodeQL often requires a build step to understand the program's structure; if you are using a compiled language like C# or Java and the build step is skipped or fails, the scanner will have no data to analyze. If you suspect a service outage, check the official Azure DevOps status page to see if the security services are experiencing downtime.
If you are seeing an overwhelming number of alerts that seem incorrect, check your CodeQL configuration file. You may have the wrong "query suite" selected. For example, running a full security suite on a project that only needs a basic linting check can result in excessive noise. Adjust the query level to match the criticality of the project.
Frequently Asked Questions
Does code scanning slow down the build pipeline?
Yes, static analysis takes more time than a standard build. To mitigate this, you can configure the scan to run on a separate schedule or only on pull requests to the main branch, rather than on every single commit to a feature branch.
What is the difference between dependency scanning and code scanning?
Dependency scanning looks at the external libraries (packages) your project uses to see if they have known vulnerabilities. Code scanning (via CodeQL) analyzes the original code you wrote to find logic flaws and security holes.
Can I use this with a private repository?
Yes, Azure DevOps Advanced Security is specifically designed for private repositories within an organization to ensure that proprietary code is analyzed securely without being exposed to the public.
Do I need to install any software on my local machine?
No, the scanning happens entirely in the cloud via the Azure DevOps pipeline. Developers only need to interact with the results through the web interface or their PR comments.
Will this scan for hardcoded passwords?
Yes, the Secret Scanning feature specifically looks for patterns that match known API keys, tokens, and password formats across your entire commit history.
Is it possible to ignore specific alerts?
Yes, you can dismiss alerts in the Advanced Security tab. You will be asked to provide a reason, such as "False Positive" or "Risk Accepted," which is then logged for compliance purposes.