Metadata-Version: 2.5
Name: site-paths
Version: 0.1.0
Summary: Stop a site from telling strangers which private routes exist: a wall for ASGI, WSGI and Django.
Project-URL: Repository, https://github.com/webodik/toller-web
Project-URL: Issues, https://github.com/webodik/toller-web/issues
Project-URL: Why, https://github.com/webodik/toller-web/blob/main/docs/why.md
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: access-control,asgi,django,fastapi,flask,middleware,route-enumeration,security,starlette,wsgi
Classifier: Framework :: Django
Classifier: Framework :: FastAPI
Classifier: Framework :: Flask
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Security
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown

# site-paths

Stop your site from telling strangers which private routes exist.

If `/admin/reports` answers "sign in" and a made-up address answers
"not found", anybody can tell that your site has a reports page without
seeing a single report. Asking for a list of names gives the list of
what you have. Scanners do that in the first minutes of an attack, and
what they try next is built on it.
[Why hide whether a route exists?](https://github.com/webodik/toller-web/blob/main/docs/why.md)

This is the wall for Python: in your server, it answers every path of a
private part alike, there or not, for whoever your own code does not
let in. It is for ASGI (Starlette, FastAPI), WSGI (Flask) and Django.

No dependencies. Typed. Apache-2.0.

## Install

```sh
pip install site-paths
```

Python 3.11 or later.

## Quick start

**1. Ask your deployment what it gives away.** The check is one command
for a site written in anything, and needs Node.js where it runs, which
is not on your server. Name the private part and one route in it that
exists, at the address your visitors use:

```sh
npx -p site-paths site-paths-check --url https://staging.shop.example --under /admin --has /admin/reports
```

It asks as a stranger for that route and for made-up names beside it.
If anything tells them apart, it says what and ends with 1.
[What the check asks and what it takes](https://github.com/webodik/toller-web/tree/main/js#the-check).

**2. If it fails, put a wall before the part,** after whatever says who
is signed in and before your routes. On FastAPI:

```python
from site_paths.asgi import Wall

app.add_middleware(
    Wall,
    under="/admin",
    allow=lambda scope: scope["session"].get("role") == "staff",
    missing="/nothing-here",
)
app.add_middleware(SessionMiddleware, secret_key=...)
```

`allow` is your own decision about who is let in. `missing` is a path
your site does not have: whoever is not let in gets your site's own
answer for it, for every path under `/admin`, so a route that is there
and one that is not look the same. Staff go on to your routes as before.

FastAPI also lists every route in `/openapi.json` to whoever asks. Leave
the part's routes out of it, with
`APIRouter(prefix="/admin", include_in_schema=False)`: a wall keeps what
is asked for, and not what the site publishes of itself.

**3. Run the check again, and keep it where your tests run.** It fails
when a later change lets an answer tell again.

## The wall

A wall runs before your site looks a route up, and asks your function
whether the caller is let in. That order is the point: an answer that
is made to look the same after the route was found can still be told
apart by how long it took.

### FastAPI and Starlette

See [Quick start](#quick-start).

- **Add the wall before the middleware that says who is signed in:**
  the session's, or `AuthenticationMiddleware`. The middleware that is
  added last runs first.
- **Leave the part's routes out of what FastAPI publishes.** FastAPI
  lists every route in `/openapi.json`, and shows them at `/docs`, to
  whoever asks. A wall keeps what is asked for, and not what the site
  says of itself:

  ```python
  staff = APIRouter(prefix="/admin", include_in_schema=False)
  ```

- **`allow` is given the connection's scope.** A coroutine is awaited,
  and a caller whose answer does not come in three seconds is kept out.
  A plain function is called at once and is not cut short: have it read
  only what the connection carries already, such as the session.
- **An application mounted below another** keeps its part whether
  `under` names it from the top of the site or from where the
  application is mounted.

### Flask

```python
from site_paths.wsgi import Wall

def is_staff(environ):
    with app.app_context():
        session = app.session_interface.open_session(app, app.request_class(environ))
        return session is not None and session.get("role") == "staff"

app.wsgi_app = Wall(app.wsgi_app, under="/admin", allow=is_staff, missing="/nothing-here")
```

The wall stands before the application, and so before Flask has opened
the session: `allow` is given the request's environment, and opens the
session itself as above. A session that is kept on the server needs the
application's context to open, which is what the `with` is for.

### Django

```python
# walls.py, in your project
from site_paths.django import wall

staff = wall(under="/admin", allow=lambda request: request.user.is_staff)
```

```python
# settings.py
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "project.walls.staff",
    "django.middleware.common.CommonMiddleware",
    ...,
]
```

- **After the middleware that says who is signed in, and before
  `CommonMiddleware` and `LocaleMiddleware`.** Those two look at an
  answer that nothing is there and redirect where the path with a
  slash, or with a language before it, is a route. Above the wall they
  would redirect `/admin/reports` when `/admin/reports/` is a route,
  and not a name that is no route, which tells the two apart. A wall
  that finds either above itself does not start, and says why.
- **Whoever is not let in gets the site's own page for a missing
  path,** as Django makes it.
- **Where routes carry a language before them** (`i18n_patterns`), the
  part is under each language: keep it with a pattern, such as
  `match=r"^/(en|de)/admin(/|$)"`.
- **Where Django runs on ASGI,** a decision that is not a coroutine is
  made in a thread.

### Options

| Option | |
| --- | --- |
| `under` | The start of the part, as a visitor asks for it: the path itself and everything below it |
| `match` | In place of `under`: a pattern for the paths to keep, such as `r"\.js\.map$"` |
| `allow` | Your decision. Only `True` lets a caller in: anything else, or an error, keeps the caller out. So does a decision that is awaited and does not come in three seconds |
| `missing` | What your site answers for a path it does not have. For ASGI and WSGI: a path the site does not have, or an application that answers in its place; left out, a bare `404`. For Django: a function that returns the answer; left out, Django's own page for a missing path |

### What to know

- **A path is read as the most forgiving server would read it,** so no
  other spelling of it goes round: `/Admin//reports`, `/%61dmin/reports`
  and `/admin;v=1/reports` are all `/admin/reports`.
- **A path that enters the part and leaves it again by dots is kept
  too,** such as `/admin/../reports`: these frameworks find a route by
  the path as it is written. So is a path of more than 64 segments,
  wherever it leads.
- **No cache may keep what a caller who is kept out is answered.** The
  wall says `cache-control: private, no-store` on it, whatever made the
  answer. What your staff are answered is yours to mark the same: a
  cache in between that keeps it gives it to whoever asks next.
- **A decision that is not awaited is not cut short:** on WSGI, and a
  plain function on ASGI. Give what it asks a time of its own to
  answer in.
- **A websocket in the part is closed before it is accepted,** for
  whoever is not let in.
- **A wall does not hide what your site sends every browser.** A script
  that names a route has given it away. The check looks for that.
- **It does not stand before** anything your platform serves without
  asking your application, such as static files at a CDN.
- **It is no replacement for authorization.** It keeps the list of
  routes from strangers. What a signed-in caller may do is still your
  code's to decide.

## What it was tried on

Python 3.11 to 3.15, on every change. FastAPI on uvicorn, Flask on
gunicorn, and Django on gunicorn and on uvicorn, each asked by the
check as a stranger, a customer and the staff: without the wall the
check fails, with it the check passes. Django with its own
`CommonMiddleware` and routes that end with a slash; FastAPI with an
application mounted below it, and with nothing of the part in what it
publishes. No cache or CDN was put in front. It reads a path by the same two
lists that the JavaScript package beside it is held to.

## License

Apache-2.0: see `LICENSE`.
