
# Firewall and limits

Before any rule is looked at, the firewall refuses with `400` the requests whose path could be read two ways. Around it, a set of limits keeps a single request, or a flood of them, from making the server do unbounded work: header and token sizes, form bodies, sessions, password hashing, calls to the identity provider.

## Why

If a rule and the router read `/admin` differently (`/admin;x`, `/%61dmin`, `/admin/.`), an attacker can reach a protected controller through an unprotected rule. Refusing every ambiguous path closes that door. The limits come from the same idea: an anonymous client should not be able to fill memory or take every core.

## Example

```dart
// An application that really needs encoded slashes in paths.
@Bean()
StrictHttpFirewall firewall() => StrictHttpFirewall(allowUrlEncodedSlash: true);
```

```bash
curl -i 'localhost:8080/admin;jsessionid=x'
# HTTP/1.1 400 Bad Request
```

## What the firewall refuses

- **In the path:** `;` and `%3B`, an encoded `/` (`%2F`), `\` and `%5C`, an encoded `%` (`%25`) or `.` (`%2E`), `//`, `.` and `..` segments, spaces, control and non-ASCII characters, `%00`, line separators.
- **A request target that Dart would read as a host** (`//host/path`).
- **Methods** other than `DELETE`, `GET`, `HEAD`, `OPTIONS`, `PATCH`, `POST` and `PUT` (`TRACE` echoes the request, cookies included).
- **Headers:** names that are not printable ASCII, and values with control characters (a tab is allowed).

A `StrictHttpFirewall` bean with one `allow...` option turned on relaxes a single rule, for an application that really needs such paths.

`dart:io` normalizes paths (`..`, `%2e%2e`, `\`) before the application, as RFC 3986 says. The rules see the normalized path, as the router does.

## Limits

| What | Limit |
|---|---|
| `Authorization: Basic` | Longer than 8 192 characters, or sent twice: fails as bad credentials. Two `Authorization` headers of any kind are refused. |
| Bearer token | At most 64 KiB. |
| DPoP proof | At most 8 KiB, one per request. |
| Login form body | At most 64 KB. When the CSRF token comes as a form field, the CSRF filter has already read the form with the web starter's 1 MiB limit, and the login filter uses those fields. |
| Form post callback of single sign-on | At most 64 KB. |
| Sessions | Only a login makes one. In total `server.session.max-sessions` (10 000): expired ones are removed, then a new login is refused with `503`. |
| Sessions per user | `spring.security.session.maximum-sessions` (10): the oldest ends, or the login is refused. |
| Password hashing | Only for requests that carry credentials; at most `spring.security.password-checks.max-concurrent` at once (half the processors), `max-queued` waiting (eight times that); above, `503` with `Retry-After: 1` at once. |
| Identity provider calls | A failed fetch of its metadata or keys is retried 30 seconds later at the earliest, never once per request. |
| DPoP replay store | 100 000 ids, 1 000 per client key. |

So a flood of wrong passwords takes half the CPU at most, and clients without a login fill no memory. Limit logins in front of the application all the same: the packages do not count failed attempts themselves (listen to the failure events to lock accounts).

## In Spring (Java)

```java
@Bean
StrictHttpFirewall httpFirewall() {
  StrictHttpFirewall firewall = new StrictHttpFirewall();
  firewall.setAllowUrlEncodedSlash(true);
  return firewall;
}
```

Spring's firewall refuses the raw path; here `dart:io` has already normalized it, and the rules see what the router sees. Spring Boot on Tomcat has no session limit of its own; the password check limits are additions.

## See also

- [Sessions](sessions.md)
- [All properties](properties.md)
- [spring_security_core properties](../spring-security-core/properties.md)
