Skip to article content
FrontendXLesson 15 / 16

Security hardening

Move from a laptop shell to a safe, observable delivery pipeline: version control, Linux processes and permissions, container images, networking, configuration and secrets, deployment, health checks, observability, rollback, reproducibility and security hardening.

advanced · about 55 minutes · 3 exercises

  • Explain
  • Implement
  • Debug

What you will be able to do

#
  • Apply least privilege to containers, pipelines and credentials.
  • Reduce attack surface with minimal images and a non-root user.
  • Describe supply-chain risks and how scanning and pinning reduce them.

Overview

#

Hardening is the practice of removing capability an attacker could use. In a delivery pipeline the main surfaces are the container, the build and the credentials the pipeline holds.

Least privilege

Run containers as a non-root user, drop unnecessary Linux capabilities, and mount the filesystem read-only where possible. A pipeline should hold only the credentials it needs, scoped to the resources it deploys.

snippet.docker
FROM node:20-alpine
RUN addgroup -S app && adduser -S app -G app
WORKDIR /app
COPY --chown=app:app . .
USER app
CMD ["node", "server.js"]

Minimal images

A smaller image has fewer packages and therefore fewer known vulnerabilities. Use a slim or distroless base and install only what you need. Multi-stage builds keep build tools out of the runtime image.

Supply chain

Your artifact depends on a base image, language packages, and build tools. Risks include a compromised dependency, a typosquatted package, and a base image with known CVEs. Mitigations: pin versions and digests, scan images for vulnerabilities, generate an SBOM, and verify artifact signatures before deploy.

Pipeline credentials

Prefer short-lived, scoped credentials over long-lived keys. If a pipeline can deploy to production, a compromised pipeline is a production compromise, so limit what it can reach.

Common pitfalls

  • Running as root because it is the default, so a container escape has more power.
  • Granting the pipeline broad cloud permissions for convenience.
  • Ignoring scan results because fixing them is tedious, leaving known CVEs deployed.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Write the Dockerfile lines that create a non-root user, copy the app with that ownership, and switch to it before the CMD.

    starter.dockerfile
    # create user, chown, USER
    

    Reveal the reference answer
    reference.dockerfile
    RUN addgroup -S app && adduser -S app -G app
    WORKDIR /app
    COPY --chown=app:app . .
    USER app
    CMD ["node", "server.js"]

    Authored expected output: The container runs as the unprivileged app user, not root.

    Creating the user before copying and using --chown avoids a later chown layer. USER app makes the process unprivileged, limiting the impact of a compromise.

  2. Exercise 2Debug

    A scan reports the deployed image contains 40 known vulnerabilities, all from build tools that are not needed at runtime. What is the fix?

    starter.text
    # single-stage image with full toolchain
    

    Reveal the reference answer
    reference.text
    Convert to a multi-stage build: build in a stage that has the toolchain, then copy only the compiled output and production dependencies into a slim runtime stage. This removes the build tools and their CVEs from the shipped image.

    Authored expected output: A smaller runtime image with far fewer reported vulnerabilities.

    Most CVEs in a naive image come from build-time packages. Multi-stage builds keep them out of the artifact that is scanned and deployed.

  3. Exercise 3Explain

    Explain why a pipeline that can deploy to production should hold only scoped, short-lived credentials.

    starter.text
    // Explain the blast radius of a compromised pipeline.
    

    Reveal the reference answer
    reference.text
    The pipeline is a high-value target because it already has the access needed to change production. If it holds broad, long-lived credentials, an attacker who compromises it inherits that access and can deploy malicious code or read data. Scoping credentials to exactly the resources the pipeline deploys limits the blast radius, and short-lived credentials reduce the window in which a leaked value is useful.

    Authored expected output: An explanation tying pipeline compromise to production access and how scoping and expiry limit damage.

    Least privilege applies to automation as much as to humans. The pipeline's permissions define the worst case if it is compromised.

Common misconceptions

#
  • A container is a security boundary by itself: containers share the host kernel, so least privilege still matters.
  • Scanning finds all vulnerabilities: it finds known ones; unknown and logic flaws remain.

Follow-up practice

#
  • devops-capstone-pipeline

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.