Skip to content

doc: annotate server.js with extra info regarding session and static …#286

Merged
lirantal merged 1 commit intoOWASP:masterfrom
cfabianski:patch-1
Mar 18, 2023
Merged

doc: annotate server.js with extra info regarding session and static …#286
lirantal merged 1 commit intoOWASP:masterfrom
cfabianski:patch-1

Conversation

@cfabianski
Copy link
Contributor

@cfabianski cfabianski commented Mar 17, 2023

Express uses a middleware system to execute code, modify requests and responses, and perform other tasks whenever a network action takes place. The order you define these middleware matters. From the express documentation:

The order of middleware loading is important: middleware functions that are loaded first are also executed first.

This is the problem we can look for. Here's an example of what not to do:

app.use(session());
app.use(express.static(__dirname + '/public'));

In this case, the session handler is initialized before returning a static asset.

Instead, we want to confirm that the static asset middleware, express.static is set prior to the session middleware.

app.use(express.static(__dirname + '/public'));
// any other middelware
app.use(session());

This will serve static assets as expected, but not apply the session middleware to them since it is defined later.

Links

…assets

Express uses a middleware system to execute code, modify requests and responses, and perform other tasks whenever a network action takes place. The order you define these middleware matters. From the [express documentation](https://expressjs.com/en/guide/writing-middleware.html):

> The order of middleware loading is important: middleware functions that are loaded first are also executed first.
> 

*This* is the problem we can look for. Here's an example of what not to do:

```javascript
app.use(session());
app.use(express.static(__dirname + '/public'));
```

In this case, the session handler is initialized before returning a static asset. 

Instead, we want to confirm that the static asset middleware, `express.static` is set prior to the `session` middleware.

```javascript
app.use(express.static(__dirname + '/public'));
// any other middelware
app.use(session());
```

This will serve static assets as expected, but not apply the session middleware to them since it is defined later.
@lirantal
Copy link
Collaborator

@cfabianski thanks for adding and noting the context with the links.

Would there be an actual viable exploit we can demonstrate based on the vulnerable code here?

@lirantal lirantal self-requested a review March 18, 2023 12:37
@lirantal lirantal merged commit b2aed38 into OWASP:master Mar 18, 2023
@cfabianski
Copy link
Contributor Author

Would there be an actual viable exploit we can demonstrate based on the vulnerable code here?

That's a very good point. I'll think about it 🤔

@lirantal
Copy link
Collaborator

lirantal commented Apr 2, 2023

If there are any updates, I'm happy to pick this up with you, potentially to also write a short demo with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants