Lesson 1 of 1
Framework configuration
Listen to an AI summary of Framework configuration
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.
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.
With debug mode on, the server sends the page in the next picture.
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.
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.
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.
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 = Trueswitches debug mode on.ALLOWED_HOSTSlists the website names, such asshop.example, that the software answers for.- Express, for JavaScript
- Express shows the stack trace on an error page unless the setting
NODE_ENVisproduction. Express sends no security headers until a library namedhelmetadds the headers. The sign-in cookie of the libraryexpress-sessionhassecureoff until a line sayssecure: true. - Rails, for Ruby
config.consider_all_requests_local = trueshows the full details of every error to everyone.config.hostsis the same kind of list asALLOWED_HOSTS.- Spring Boot, for Java
server.error.include-stacktrace=alwaysputs 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 filenext.configsets the security headers. A list namedmatcher, in the fileproxy(namedmiddlewarebefore 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
secureor nohttpOnly. - 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
matcherlist 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
.gitfolder 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
Name two cookie settings that are weaknesses when switched off.
Show the answer
HttpOnlyandSecure.An error page of the sports shop shows a stack trace and the database password. Which line of
config.jscauses the page to show these details?Show the answer
The line
debug: true, which switches debug mode on in production.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.
- Sign in and remember meGiftwrap, BeginnerOpen
- Gift searchGiftwrap, BeginnerOpen
- Go live checklistSwiftcart, IntermediateOpen
- Year in reviewBrightledger, IntermediateOpen
- Delete a postChirp, IntermediateOpen
- Parent portalGradebook, BeginnerOpen