Skip to content

Lesson 1 of 1

Framework configuration

Listen to an AI summary of Framework configuration

0:00 / 0:39

Software has settings, as a phone or a game has. A setting changes how the software behaves without a change to the code. The set of settings is called configuration. A security misconfiguration is a setting that leaves a protection switched off or shows too much to an attacker. A reviewer reads a change to a setting as carefully as a change to code.

The settings file of the sports shop

The shop is built on a framework. The shop keeps its settings in one file named config.js, and the code passes the settings to the framework. Each line gives one setting a name and a value.

  1. module.exports = {
  2. shopName: 'Sports shop',
  3. currency: 'GBP',
  4. itemsPerPage: 20,
  5. debug: false,// debug mode is off
  6. cookieSecure: true,// a cookie setting is on
  7. cookieHttpOnly: true,// a cookie setting is on
  8. maxUploadBytes: 1000000,// the largest upload allowed, in bytes
  9. sessionSeconds: 28800,// how long a sign-in can last, in seconds (8 hours)
  10. }
The settings file of the sports shop. The lesson covers the three marked lines.

What is debug mode

A programmer who is building software wants the full details of every error. Debug mode is a setting that shows the full details of every error, meant for the people building the software.

A programmer tests changes on a private copy of the software. Production is the live copy of the software that real people use. Debug mode belongs on the private copy and stays off in production.

What an error page shows

Each order in the sports shop has a number. The address shop.example/order?id=1042 shows order 1042. An attacker changes the end of the address to id=abc. No order has that number, and the code stops with an error on the line that reads the order.

With debug mode off, the server sends the attacker the page in the picture.

  • A web page at the address shop.example/order?id=abc.
  • Something went wrong. no detail about the code
  • Please try again later.
The error page a customer should see.

With debug mode on, the server sends the page in the next picture.

  1. TypeError: Cannot read properties of undefined (reading 'total')the kind of error
  2. at orderTotal (/srv/shop/database.js:17:16)the function where the error happened, with a file name and a line number
  3. at showOrder (/srv/shop/orders.js:42:19)the function that called orderTotal
  4. Variables
  5. orderId = 'abc'the value the attacker sent
  6. dbPassword = 'blue-kettle-42'the database password
The same error with debug mode on. Each framework lays the page out in its own way.

A stack trace is a list of the functions that were running when an error happened, with file names and line numbers. The stack trace in the picture names two functions. The function showOrder called the function orderTotal, and the error happened in orderTotal.

The debug page of some frameworks, such as Django, a framework for Python, also shows the variables the program held at the time, and the picture shows a page of that kind. One variable holds the password of the shop's database. The attacker now knows the file names, the line that failed and the database password. The debug page of some frameworks, such as Flask, also has a console that can run code on the server.

The weakness is named generation of an error message containing sensitive information (CWE-209).

One changed line

A programmer switches debug mode on to study an error, and the change reaches production. A diff shows a change as the lines removed, marked -, and the lines added, marked +. Look at the value of debug.

config.js+1-1

  1. itemsPerPage: 20,
  2. - debug: false,
  3. + debug: true,
  4. cookieSecure: true,
One changed line of the settings file.

The diff changes one value, and every page of the shop still works. After the change, every error page in production shows the stack trace and the variables.

A safer shop keeps one settings file for production and another for the private copy. The production page says nothing about the code. The full details go to a log, a list the software writes of what happened, as the lesson on error handling covers.

Cookie settings

The sports shop gives Sam's browser a cookie when Sam signs in. Anyone who copies the cookie can use the shop as Sam.

A cookie has settings of its own, which the lesson on sessions explains. Two of the cookie settings are weaknesses when switched off.

HttpOnly
Stops JavaScript in a web page reading the cookie. Switched off, the weakness is a sensitive cookie without the HttpOnly flag (CWE-1004).
Secure
Makes the browser send the cookie only over HTTPS, which hides the cookie from other people on the network. Switched off, the weakness is a sensitive cookie without the Secure attribute (CWE-614).

In config.js the two cookie settings are the lines cookieHttpOnly and cookieSecure. A diff that changes either value to false adds a weakness. The framework Express leaves both settings off until the code switches each setting on. A missing word is then the same weakness.

The JavaScript below gives the browser the sign-in cookie in two ways. Look for the words httpOnly and secure in each version.

// Weakness: neither setting is named, so both are off
res.cookie('session', sessionId, { maxAge: 28800000, sameSite: 'lax' })

// Safer: both settings are on
res.cookie('session', sessionId, { maxAge: 28800000, sameSite: 'lax', httpOnly: true, secure: true })

res.cookie sets a cookie, and the part in curly brackets holds the settings of the cookie. maxAge is how long the cookie lasts, in thousandths of a second.

What is a security header

A security header is a header in a response that switches on a protection in the browser.

  1. HTTP/1.1 200 OKthe first line says the request worked
  2. Content-Security-Policy: script-src 'self'a security header that allows only the shop's own JavaScript
  3. Strict-Transport-Security: max-age=31536000a security header that keeps the browser on HTTPS for a year
  4. Set-Cookie: session=7f3a9c; Secure; HttpOnlythe header that gives the browser a cookie, with both cookie settings on
The top of a response from the sports shop.

The header Content-Security-Policy is covered in a separate lesson.

Some frameworks send no security headers until a setting or a line of code switches the headers on. A page looks the same with or without a security header. A test that only looks at the page does not show a missing header.

Files left where anyone can open them

A file left by mistake in the web root (File uploads) is sent to anyone who asks for the name.

  1. public/the web root
  2. app.jsthe JavaScript the browser runs
  3. app.js.mapa source map, with the code as the programmers wrote it
  4. config.js.baka backup copy of the settings file
  5. .git/the history of the code, with every old version
Two files and a folder in the web root that anyone can open.

A source map is a file that turns the JavaScript sent to a browser back into the code as the programmers wrote the code, with the comments. The browser already has the same code in a harder form, so a source map matters most where the map holds a secret or a comment an attacker can use. A backup copy can hold a password. The folder .git holds every old version of every file, with any secret that was ever saved (Secrets in code).

A comment in a page is sent to every browser. A comment that names a hidden path, an account or how the code works tells an attacker where to look. A file left in the web root is the weakness named files or directories accessible to external parties (CWE-552).

Examples of setting names in other frameworks

Every framework has its own names for the same kinds of setting.

Flask, for Python
app.run(debug=True) switches debug mode on, and the debug page has a console that can run Python on the server.
Django, for Python
DEBUG = True switches debug mode on. ALLOWED_HOSTS lists the website names, such as shop.example, that the software answers for.
Express, for JavaScript
Express shows the stack trace on an error page unless the setting NODE_ENV is production. Express sends no security headers until a library named helmet adds the headers. The sign-in cookie of the library express-session has secure off until a line says secure: true.
Rails, for Ruby
config.consider_all_requests_local = true shows the full details of every error to everyone. config.hosts is the same kind of list as ALLOWED_HOSTS.
Spring Boot, for Java
server.error.include-stacktrace=always puts the stack trace in every error response. management.endpoints.web.exposure.include=* opens pages that show the settings and the memory of the software.
Next.js, for JavaScript
The function headers() in the file next.config sets the security headers. A list named matcher, in the file proxy (named middleware before version 16), names the pages that a sign-in check runs on. A page left off the list has no check.

Examples of what to look for

  • A debug setting switched on, such as debug: true, or a debug setting that is on when no value is given.
  • Code that puts the details of an error in a response, such as a stack trace.
  • A cookie setting switched off, such as secure: false.
  • New code that gives the browser a sign-in cookie, with no secure or no httpOnly.
  • A security header removed, or a shorter list of pages that get the header.
  • A longer list of website names that the software answers for, such as ALLOWED_HOSTS = ["*"], where * means every name.
  • A matcher list that no longer names a page.
  • A backup copy of a file in the web root, such as a name that ends in .bak.
  • A .git folder that the server sends to anyone who asks.
  • A source map, a file whose name ends in .map, sent with the live website, where the map holds a secret or a comment an attacker can use.
  • A comment in a page that names a path, an account or how the code works.

Check your understanding

  1. Name two cookie settings that are weaknesses when switched off.

    Show the answer

    HttpOnly and Secure.

  2. An error page of the sports shop shows a stack trace and the database password. Which line of config.js causes the page to show these details?

    Show the answer

    The line debug: true, which switches debug mode on in production.

  3. Give one reason why a reviewer reads a change to a setting as carefully as a change to code.

    Show the answer

    One changed setting can switch a protection off or show private details, such as a stack trace.

Practise it

Challenges with a weakness this lesson teaches.