Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Projects and Practice

Project: A Loopback Security-Header Audit Tool

Build a small Node.js tool that starts two local demo web servers, audits their HTTP security headers and cookies, scores them and prints a report, all on loopback.

In this project you will write a real scanner-style tool, but you will point it only at servers you start yourself on the loopback address (127.0.0.1). That is the safe way to practice: the target is yours, the traffic never leaves your computer, and nobody else can be affected. The same code, pointed at a staging site you own or are authorized to test, becomes a genuine configuration check. Never point it at a system you do not have permission to test.

The tool does three things:

  1. starts two demo web servers, one sloppy and one hardened,
  2. requests each one and inspects the response headers and cookies,
  3. scores them and prints a report like the ones from web testing and headers.

The code runs in the Node.js runner on this site (Node 24, which has fetch built in). Servers listen on port 0, which asks the operating system for a free port, so nothing clashes.

Step 1: two demo servers

The "legacy" server leaks its version and sets weak cookies. The "hardened" server sends the headers we recommended earlier.

Node.js Step 1: start two local servers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

server.listen(0, "127.0.0.1") binds only to the loopback interface, so the demo is not reachable from other machines. The script closes both servers at the end, which also lets the program exit.

Step 2: the audit function

Now separate the checks from the servers. An audit function takes a Response and returns a list of findings with a severity and a fix, so you can test it against any URL.

Node.js Step 2: audit one response

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice what was reasoned about: only cookies whose names look like session or auth tokens are held to the strict standard, as in the cookie audit earlier. getSetCookie() returns one entry per cookie, which matters because several Set-Cookie headers must not be merged.

Step 3: score and compare

A score gives a quick comparison, but be careful: the score is a triage aid, not a verdict. Each finding takes points off a starting 100, and the report prints a grade next to the list of problems.

Node.js Step 3: score both servers and print a report

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The legacy server scores a D, the "improving" one a middle grade, and the hardened one a clean A. A trend like this is what you would show a client after a remediation sprint. The numbers are simple on purpose: every change to a penalty is a visible decision you can explain.

Extend the project

  • Fetch several paths (/login, /api/, /admin) and compare headers between them, because configuration often differs per route.
  • Check redirect behaviour: does HTTP redirect to HTTPS? (Add a redirecting demo server and use redirect: "manual".)
  • Parse the CSP and flag unsafe directives such as 'unsafe-inline' and wildcard sources.
  • Add a command-line argument for a URL and a safety rule that refuses anything but 127.0.0.1, localhost or hosts in an allowlist file you control.
  • Output JSON, then feed it to the findings tracker.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does this project start its own servers on 127.0.0.1?

    Practicing on your own loopback servers keeps all traffic local and legal.
  2. 2. What does listening on port 0 do?

    It avoids port conflicts, and the real port is read from server.address().
  3. 3. Why use res.headers.getSetCookie() instead of reading one Set-Cookie header string?

    Merging cookies into one string makes parsing their attributes unreliable.
  4. 4. What is the right way to treat the numeric score in the report?

    A grade summarises configuration hygiene; real risk needs the findings and the context.

Summary

  • Practice scanner code against servers you start yourself on loopback; never against systems you do not own or have permission to test.
  • Separate the audit logic from the target so the same function checks any response.
  • Check security headers, version disclosure and cookie attributes, holding sensitive cookies to a stricter standard.
  • Use a simple, explainable score as a triage aid, and always list the findings.
  • Extend the tool with redirects, CSP parsing, per-route checks and JSON output.

Next: Practice labs, CTFs and review