Skip to content
Visual explainer 2h 30m to learn 4 steps

Security headers, WAF & secure SDLC

Add browser security headers, explain what a WAF can and cannot do and build security into development.

By Invicly Tech, , in Cyber Security Fundamentals. About 11 minutes to read.

Topic 27 of 49

In short

Browser security headers, a web application firewall and a secure development lifecycle work together. Headers like HSTS and CSP harden the browser, a WAF filters obvious attacks, and building security into development prevents flaws from shipping.

Explained simply

Fixing bugs in code is the main defence, but a web application also gets help from three other places: the browser, a filter in front of the app, and the way the software is built.

Security headers are instructions the server sends to the browser, such as "always use HTTPS for this site" or "do not run scripts from other places". They cost almost nothing to add and block whole classes of attacks.

A web application firewall (WAF) sits in front of the app and inspects requests. It blocks well-known attack patterns, like obvious SQL injection, before they reach your code. It is useful, but attackers can often get around it, and it cannot understand your business rules.

A secure software development lifecycle (SDLC) builds security into every step: thinking about threats at design time, scanning code and libraries automatically in CI (continuous integration), and testing before release. Bugs found early are much cheaper to fix than bugs found by attackers.

Press Play on a diagram to walk through it one step at a time.

LayersWeb defence layers
  1. 1
    Secure SDLC

    Design review and testing before release.

  2. 2
    CI scanning

    Dependency and code checks.

  3. 3
    WAF

    Filters common attack patterns.

  4. 4
    Security headers

    Hardens the browser side.

ComparePrevent, filter, detect
In the code (prevent)
  • Parameterised queries, encoding
  • Authorization checks
  • Fixes the root cause
  • Found by review, SAST, tests
At the edge (filter)
  • WAF rules and rate limits
  • Virtual patches
  • Can be bypassed
  • Buys time to fix
In the browser (contain)
  • CSP, HSTS, nosniff
  • Limits damage of bugs
  • Cheap to add
  • Check with curl -I

In depth

The headers worth setting, and what each does

Strict-Transport-Security (HSTS) tells the browser to use only HTTPS for the site for max-age seconds, so an attacker cannot downgrade a user to plain HTTP. A common value is max-age=31536000; includeSubDomains (one year). Adding preload and submitting to the preload list builds this into browsers, but it is hard to undo, so be sure every subdomain supports HTTPS.

Content-Security-Policy limits where scripts and other resources may come from, and its frame-ancestors directive controls who may embed your page in a frame, which stops clickjacking. X-Frame-Options: DENY is the older header for the same job. X-Content-Type-Options: nosniff stops browsers guessing a file's type, so an uploaded text file cannot be run as a script. Referrer-Policy: strict-origin-when-cross-origin keeps full URLs, which may contain tokens, from leaking to other sites. Permissions-Policy turns off browser features you do not use, such as the camera.

The old X-XSS-Protection header is deprecated; leave it out or set it to 0.

MapSecurity headers and the attacks they block
Response headers
  • Strict-Transport-Security
    • HTTPS downgrade
    • SSL stripping
  • Content-Security-Policy
    • Injected scripts (XSS)
    • frame-ancestors: clickjacking
  • X-Content-Type-Options: nosniff
    • MIME sniffing of uploads
  • Referrer-Policy
    • Leaking URLs to other sites
  • Permissions-Policy
    • Abuse of camera, mic, location

What a WAF can and cannot do

A WAF inspects HTTP requests and responses against rules. The widely used open-source rule set is the OWASP Core Rule Set (CRS), which runs on engines such as ModSecurity and Coraza and on many cloud WAFs. It uses anomaly scoring: each suspicious pattern adds points, and the request is blocked when the total passes a threshold. Paranoia levels from 1 to 4 trade more detection for more false positives.

Strengths: it blocks noisy automated attacks, gives one place to add a virtual patch for a new vulnerability while the real fix is being made (many teams did this for Log4Shell in 2021), adds rate limiting and bot controls, and produces useful logs.

Limits: attackers bypass signature rules with encoding, unusual syntax or splitting payloads. A WAF cannot know that user 7 should not read invoice 9, so broken access control and business logic flaws pass straight through. It often cannot inspect traffic it cannot decrypt. Start in detection-only mode, tune false positives, then block.

  • Good at: noisy scanners, known payloads, virtual patching
  • Bad at: IDOR, logic flaws, novel bypasses
  • Deploy: log-only first, tune, then block

Dependency and secret scanning in CI

Most application code is open-source libraries, so a vulnerable dependency is a vulnerable app. Dependency scanning (also called software composition analysis) compares your lock files against vulnerability databases. Common tools are npm audit, pip-audit, osv-scanner, OWASP Dependency-Check, Trivy, and Dependabot or Renovate, which also open pull requests to upgrade.

Run the scan on every pull request and fail the build for high or critical issues that have a fix available. Also run it on a schedule, because new CVEs appear for code that has not changed. Keep a software bill of materials (SBOM) in a standard format like CycloneDX or SPDX, so when the next big library bug appears you can answer "where do we use it?" in minutes.

Secret scanning catches API keys and passwords committed by mistake. Tools like gitleaks run in CI, and GitHub push protection blocks many known secret formats before they land. A leaked secret must be rotated, not just deleted, because Git history keeps it.

FlowSecurity checks in a CI pipeline
  1. 1
    Pull request

    Developer pushes a change

  2. 2
    Secret scan

    gitleaks or push protection

  3. 3
    SAST

    Semgrep or CodeQL on the code

  4. 4
    Dependency scan

    pip-audit, npm audit, osv-scanner

  5. 5
    Build + SBOM

    Image scan, e.g. Trivy

  6. 6
    Merge gate

    Block on high or critical findings

A secure SDLC in practice

A secure SDLC adds a few security activities to each stage rather than one big test at the end. Requirements: write security requirements, for example from OWASP ASVS, such as "all endpoints check object ownership". Design: run a short threat model for new features to find abuse cases. Code: use safe frameworks and libraries, peer review with a security checklist, and SAST (static analysis) tools such as Semgrep or CodeQL. Test: automated authorization tests, DAST (dynamic scanning of the running app) with a tool such as ZAP, and penetration tests for major releases. Release and operate: hardened configuration, security headers, logging, monitoring and a way for outsiders to report bugs, such as a security.txt file.

Frameworks help you measure maturity: the NIST Secure Software Development Framework (SP 800-218) and OWASP SAMM. The key is making security a normal part of the developer workflow, with fast tools and clear guidance, so it is not skipped under deadline pressure.

Learn it in 4 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Set HSTS, CSP and X-Content-Type-Options~40m
  2. 2
    Explain WAF strengths and limits~40m
  3. 3
    Add dependency scanning to CI~40m
  4. 4
    Describe a secure SDLC~40m

Key points

  • HSTS forces HTTPS; CSP limits what the page can load.
  • A WAF blocks common patterns but is not a substitute for fixes.
  • Dependency scanning catches known-vulnerable libraries.
  • A secure SDLC builds security in rather than bolting it on.

Key terms

Tap a card to flip it. 0/4 known

Examples

Adding security headers in nginx

Step 1: add the headers in the server block. The always parameter makes nginx send them on error pages too. Step 2: test the config and reload. Step 3: check the result with curl -I. Step 4: watch for a common nginx trap: if a location block defines any add_header of its own, the server-level headers are not inherited by that location, so repeat them there or use an include file. Start CSP in Report-Only mode if the site has inline scripts.

nginx
server {
    listen 443 ssl;
    server_name www.example.com;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
    add_header Content-Security-Policy "default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'" always;
}
# sudo nginx -t && sudo systemctl reload nginx
# curl -sI https://www.example.com | grep -iE 'strict|content-security|nosniff'

Failing a pull request on vulnerable dependencies

Step 1: add a GitHub Actions workflow that runs on every pull request. Step 2: check out the code and set up Python. Step 3: run pip-audit against the requirements file; it exits with an error if any dependency has a known vulnerability, which fails the check. Step 4: add gitleaks to catch committed secrets. Step 5: make these checks required in branch protection so a failing pull request cannot be merged.

yaml
name: security-checks
on: [pull_request]
jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with: { fetch-depth: 0 }
      - uses: actions/setup-python@v5
        with: { python-version: "3.12" }
      - run: pip install pip-audit && pip-audit -r requirements.txt
      - uses: gitleaks/gitleaks-action@v2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Quick reference

  • HSTS: Strict-Transport-Security: max-age=31536000; includeSubDomains
  • X-Content-Type-Options: nosniff stops MIME sniffing
  • CSP frame-ancestors 'none' (or X-Frame-Options: DENY) stops clickjacking
  • Referrer-Policy: strict-origin-when-cross-origin keeps full URLs private
  • X-XSS-Protection is deprecated: omit it or set it to 0
  • WAF: start in detection mode, tune, then block; it cannot catch IDOR
  • Scan dependencies on every PR and on a schedule; keep an SBOM
  • A leaked secret must be rotated; deleting the commit is not enough

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which header stops a browser from ever connecting to your site over plain HTTP after its first visit?

Question 2

2Which vulnerability is a WAF least able to stop?

Question 3

3A developer commits an AWS key, then deletes it in the next commit. What should happen?

Question 4

4Why run dependency scans on a schedule as well as on pull requests?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Check the security headers of three websites with curl and note which of HSTS, CSP, nosniff and Referrer-Policy each sends.

  2. Exercise 2medium

    Run pip-audit or npm audit on one of your own projects, pick one finding, and decide: upgrade now, upgrade later, or not affected. Write down why.

  3. Exercise 3hard

    Set up ModSecurity or Coraza with the OWASP Core Rule Set in front of a lab app in detection-only mode, send a test SQL injection string, and find the matching log entry.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Do I still need X-Frame-Options if I use CSP frame-ancestors?
Modern browsers follow frame-ancestors and ignore X-Frame-Options when both are present. Many sites send both for old browsers; it does no harm.
Is a cloud WAF enough to protect my web app?
No. It reduces noise and blocks common attacks, but it cannot fix access control or logic flaws, and payloads can often be disguised. Fix the code and use the WAF as an extra layer.
What is an SBOM and why do I need one?
A software bill of materials is a list of every component and version in your software. When a new vulnerability is announced, it lets you find affected applications quickly.
What is the difference between SAST and DAST?
SAST reads the source code without running it and finds risky patterns early. DAST tests the running application from the outside, like an attacker, and finds issues in the real deployment.

Go deeper

ShareXLinkedInWhatsApp