Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 7 min read

πŸ” SAST vs DAST: Catching Security Flaws Before and After Deployment

πŸ” SAST vs DAST: Catching Security Flaws Before and After Deployment Most security incidents in web applications come from a small set of mistakes: injection flaws, broken authentication, insecure configuration, and sec

πŸ” SAST vs DAST: Catching Security Flaws Before and After Deployment

Most security incidents in web applications come from a small set of mistakes: injection flaws, broken authentication, insecure configuration, and secrets committed to source control. The OWASP Top 10 has documented these patterns for years, yet they keep showing up in production.

The good news is that you do not have to wait for a penetration test to find them. Two families of tools, SAST and DAST, can run automatically in your pipeline. They look at the same application from opposite directions, and understanding the difference tells you where each one belongs.

In this article I explain both approaches, show what each one catches, and build a practical setup for a Java and Spring Boot project.

The Problem: Two Different Views of the Same Application

Think of an application as a building. You can inspect the blueprints, or you can walk through the finished building and try the doors. The blueprints show you a wiring error before anyone flips the switch. Walking through the building shows you that the lock on the back door is broken, even if the blueprints looked fine.

  • SAST (Static Application Security Testing) reads the blueprints: source code, bytecode, or configuration files. It never runs the application.
  • DAST (Dynamic Application Security Testing) walks through the building: it sends requests to a running application and observes the responses. It treats the application as a black box.

Neither approach is complete on its own. Each one has blind spots that the other covers.

SAST: Finding Flaws in the Code

SAST tools parse your code and look for patterns that are known to be dangerous. They understand data flow to some degree, so they can trace a value from an HTTP parameter to a database query and flag the path if nothing sanitizes it.

What SAST Catches Well

  • SQL injection from string concatenation
  • Hardcoded credentials and API keys
  • Use of weak cryptographic algorithms such as MD5 or DES
  • Insecure deserialization calls
  • Path traversal in file handling
  • Missing input validation on code paths you can see

A Classic Example

Here is a method that a SAST tool will flag immediately:

public User findUser(Connection conn, String username) throws SQLException {
    // Vulnerable: user input is concatenated into the query
    String sql = "SELECT id, name, email FROM users WHERE username = '" + username + "'";
    try (Statement stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery(sql)) {
        if (rs.next()) {
            return new User(rs.getLong("id"), rs.getString("name"), rs.getString("email"));
        }
        return null;
    }
}

An input of ' OR '1'='1 turns the query into one that returns every row. The fix is a parameterized query:

public User findUser(Connection conn, String username) throws SQLException {
    String sql = "SELECT id, name, email FROM users WHERE username = ?";
    try (PreparedStatement stmt = conn.prepareStatement(sql)) {
        stmt.setString(1, username);
        try (ResultSet rs = stmt.executeQuery()) {
            if (rs.next()) {
                return new User(rs.getLong("id"), rs.getString("name"), rs.getString("email"));
            }
            return null;
        }
    }
}

If you use Spring Data JPA or JdbcTemplate with named parameters, you get the same protection by default. The risk appears when developers build queries by hand.

What SAST Misses

SAST has real limits:

  • False positives. A tool may flag code that is safe because the input is validated somewhere else. Teams that do not tune their rules end up ignoring the reports.
  • Runtime configuration. A setting such as server.error.include-stacktrace=always in a file deployed to production is not visible in the source.
  • Framework behavior. If your security depends on a filter registered in a configuration class that the tool does not understand, it may report a false negative.
  • Business logic flaws. A tool cannot know that a discount should only be applied once.

DAST: Finding Flaws in the Running Application

DAST tools start from the outside. They crawl your application, discover endpoints, and send crafted payloads. Then they look at the response: an error message that leaks a stack trace, a reflected script tag, a missing security header, or a login that accepts a weak password.

What DAST Catches Well

  • Reflected and stored cross-site scripting (XSS)
  • SQL injection that only appears through a real request path
  • Missing or misconfigured security headers such as Content-Security-Policy and Strict-Transport-Security
  • Verbose error pages and exposed endpoints such as /actuator or /swagger-ui
  • Authentication and session problems visible from the outside
  • Issues introduced by the deployment, such as a proxy that strips a header

Where DAST Fits

DAST needs a running environment. Run it against a staging environment that is as close to production as you can make it. Do not point an active scanner at production without approval, because active scans send attack payloads and can disrupt the service.

What DAST Misses

  • Code location. A DAST finding says "this endpoint is vulnerable," not "line 42 of UserRepository concatenates the input." Finding the fix takes more work.
  • Paths it cannot reach. If the scanner cannot log in, it cannot test the pages behind the login.
  • Dormant code. If a vulnerable method is not reachable through any route, DAST will never see it.

SAST and DAST Side by Side

Aspect SAST DAST
What it analyzes Source, bytecode, configuration Running application
Needs a running app No Yes
When it runs Early, on every commit or pull request After deployment to staging
Typical findings Injection patterns, secrets, weak crypto XSS, headers, exposed endpoints, auth issues
Location of the fix Exact file and line Endpoint or behavior
Common noise False positives Scan time and login setup

The two approaches cover different parts of the risk. A SAST scan of a Java service can find the SQL injection in your repository, while a DAST scan can find that the same service sends a stack trace to the browser when it fails.

Running Both in a Java CI/CD Pipeline

Here is a practical setup for a Maven project. It runs SAST on every build and DAST against staging after a deployment.

Step 1: Add SpotBugs with the FindSecBugs Plugin

FindSecBugs extends SpotBugs with security-focused detectors. Add it to your pom.xml:

<plugin>
    <groupId>com.github.spotbugs</groupId>
    <artifactId>spotbugs-maven-plugin</artifactId>
    <version>4.8.6.0</version>
    <configuration>
        <effort>Max</effort>
        <threshold>Medium</threshold>
        <failOnError>true</failOnError>
        <plugins>
            <plugin>
                <groupId>com.h3xstream.findsecbugs</groupId>
                <artifactId>findsecbugs-plugin</artifactId>
                <version>1.13.0</version>
            </plugin>
        </plugins>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Run it with mvn verify. The check goal fails the build when it finds issues at or above the threshold, so the team sees the problem before the code merges.

Step 2: Run OWASP ZAP After Deployment

OWASP ZAP is an open-source DAST scanner maintained as an OWASP project. The baseline scan is a passive scan that is safe to run against staging:

docker run --rm -t ghcr.io/zaproxy/zaproxy:stable \
  zap-baseline.py -t https://staging.example.com -r zap-report.html

The baseline scan only observes traffic, so it does not send attack payloads. For deeper testing, use the full scan, but only against environments you own and are allowed to test.

Step 3: Wire Both Into the Pipeline

A GitLab CI example:

stages:
  - build
  - sast
  - deploy
  - dast

sast:
  stage: sast
  image: maven:3.9-eclipse-temurin-21
  script:
    - mvn -B spotbugs:check

deploy_staging:
  stage: deploy
  script:
    - ./scripts/deploy-staging.sh

dast:
  stage: dast
  image: docker:latest
  services:
    - docker:dind
  script:
    - docker run --rm -v "$PWD:/zap/wrk:rw" ghcr.io/zaproxy/zaproxy:stable
      zap-baseline.py -t "$STAGING_URL" -r zap-report.html
  artifacts:
    paths:
      - zap-report.html
    when: always

The when: always on the artifact keeps the report even when the scan fails, so you can read it.

Best Practices

  • Tune before you enforce. Run SAST in report-only mode for a sprint, mark false positives, and then make the build fail on new high-severity findings.
  • Fail on new issues, not on old ones. Many tools support baselines. This lets you adopt them in a legacy codebase without blocking every merge.
  • Scan dependencies too. SAST does not cover third-party libraries. Use a software composition analysis tool such as OWASP Dependency-Check alongside SAST.
  • Protect staging. DAST targets should be isolated and should not contain real customer data.
  • Keep secrets out of the scan targets. Pass credentials to ZAP through the pipeline's secret store, never in the repository.
  • Track findings like bugs. A security finding without an owner and a due date will sit in a dashboard forever.

Key Takeaways

  • SAST reads your code and configuration. It is fast, runs early, and points to the exact line to fix, but it can be noisy.
  • DAST tests the running application from outside. It finds runtime and configuration problems, but it needs a deployed environment and gives less detail about the code.
  • Use both. Each one covers the other's blind spots.
  • In a Java stack, SpotBugs with FindSecBugs is a good starting point for SAST, and OWASP ZAP is a solid choice for DAST.
  • The most valuable step is not the tool. It is making findings part of the normal workflow, with owners and follow-up.

Security is not a phase at the end of a project. Running these checks on every change means the next vulnerability is caught by a pipeline and not by an attacker.

What is the most useful security check you have added to your pipeline? I would like to hear what has worked for your team.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.