plain 0.56.1__tar.gz → 0.58.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {plain-0.56.1 → plain-0.58.0}/PKG-INFO +1 -1
- {plain-0.56.1 → plain-0.58.0}/plain/CHANGELOG.md +30 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/agent.py +6 -0
- plain-0.58.0/plain/csrf/README.md +75 -0
- plain-0.58.0/plain/csrf/middleware.py +134 -0
- {plain-0.56.1 → plain-0.58.0}/plain/exceptions.py +29 -19
- {plain-0.56.1 → plain-0.58.0}/plain/forms/README.md +0 -2
- {plain-0.56.1 → plain-0.58.0}/plain/http/__init__.py +0 -2
- {plain-0.56.1 → plain-0.58.0}/plain/http/response.py +7 -22
- {plain-0.56.1 → plain-0.58.0}/plain/internal/middleware/https.py +9 -5
- {plain-0.56.1 → plain-0.58.0}/plain/internal/middleware/slash.py +4 -2
- {plain-0.56.1 → plain-0.58.0}/plain/runtime/global_settings.py +48 -53
- {plain-0.56.1 → plain-0.58.0}/plain/templates/README.md +0 -1
- {plain-0.56.1 → plain-0.58.0}/plain/test/client.py +2 -10
- {plain-0.56.1 → plain-0.58.0}/plain/views/README.md +0 -2
- plain-0.58.0/plain/views/redirect.py +66 -0
- {plain-0.56.1 → plain-0.58.0}/plain/views/templates.py +0 -18
- {plain-0.56.1 → plain-0.58.0}/pyproject.toml +1 -1
- plain-0.58.0/tests/test_csrf.py +356 -0
- plain-0.56.1/plain/csrf/README.md +0 -23
- plain-0.56.1/plain/csrf/middleware.py +0 -445
- plain-0.56.1/plain/views/csrf.py +0 -4
- plain-0.56.1/plain/views/redirect.py +0 -77
- {plain-0.56.1 → plain-0.58.0}/.gitignore +0 -0
- {plain-0.56.1 → plain-0.58.0}/LICENSE +0 -0
- {plain-0.56.1 → plain-0.58.0}/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/__main__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/assets/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/assets/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/assets/compile.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/assets/finders.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/assets/fingerprints.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/assets/urls.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/assets/views.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/chores/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/chores/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/chores/registry.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/build.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/changelog.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/chores.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/core.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/docs.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/formatting.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/help.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/install.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/output.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/preflight.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/print.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/registry.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/scaffold.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/settings.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/shell.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/startup.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/upgrade.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/urls.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/cli/utils.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/csrf/views.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/debug.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/forms/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/forms/boundfield.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/forms/exceptions.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/forms/fields.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/forms/forms.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/http/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/http/cookie.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/http/multipartparser.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/http/request.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/files/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/files/base.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/files/locks.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/files/move.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/files/temp.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/files/uploadedfile.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/files/uploadhandler.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/files/utils.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/handlers/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/handlers/base.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/handlers/exception.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/handlers/wsgi.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/middleware/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/internal/middleware/headers.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/json.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/logs/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/logs/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/logs/configure.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/logs/loggers.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/logs/utils.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/packages/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/packages/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/packages/config.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/packages/registry.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/paginator.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/preflight/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/preflight/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/preflight/files.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/preflight/messages.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/preflight/registry.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/preflight/security.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/preflight/urls.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/runtime/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/runtime/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/runtime/user_settings.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/signals/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/signals/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/signals/dispatch/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/signals/dispatch/dispatcher.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/signals/dispatch/license.txt +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/signing.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/templates/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/templates/core.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/templates/jinja/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/templates/jinja/environments.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/templates/jinja/extensions.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/templates/jinja/filters.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/templates/jinja/globals.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/test/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/test/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/test/encoding.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/test/exceptions.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/urls/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/urls/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/urls/converters.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/urls/exceptions.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/urls/patterns.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/urls/resolvers.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/urls/routers.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/urls/utils.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/README.md +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/cache.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/crypto.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/datastructures.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/dateparse.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/deconstruct.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/decorators.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/duration.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/encoding.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/functional.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/hashable.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/html.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/http.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/inspect.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/ipv6.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/itercompat.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/module_loading.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/regex_helper.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/safestring.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/text.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/timesince.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/timezone.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/utils/tree.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/validators.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/views/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/views/base.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/views/errors.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/views/exceptions.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/views/forms.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/views/objects.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/plain/wsgi.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/tests/.gitignore +0 -0
- {plain-0.56.1 → plain-0.58.0}/tests/app/.gitignore +0 -0
- {plain-0.56.1 → plain-0.58.0}/tests/app/settings.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/tests/app/test/__init__.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/tests/app/test/default_settings.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/tests/app/urls.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/tests/conftest.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/tests/test_cli.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/tests/test_runtime.py +0 -0
- {plain-0.56.1 → plain-0.58.0}/tests/test_wsgi.py +0 -0
@@ -1,5 +1,35 @@
|
|
1
1
|
# plain changelog
|
2
2
|
|
3
|
+
## [0.58.0](https://github.com/dropseed/plain/releases/plain@0.58.0) (2025-08-19)
|
4
|
+
|
5
|
+
### What's changed
|
6
|
+
|
7
|
+
- Complete rewrite of CSRF protection using modern Sec-Fetch-Site headers and origin validation ([955150800c](https://github.com/dropseed/plain/commit/955150800c))
|
8
|
+
- Replaced CSRF view mixin with path-based exemptions using `CSRF_EXEMPT_PATHS` setting ([2a50a9154e](https://github.com/dropseed/plain/commit/2a50a9154e))
|
9
|
+
- Renamed `HTTPS_REDIRECT_EXEMPT` to `HTTPS_REDIRECT_EXEMPT_PATHS` with leading slash requirement ([b53d3bb7a7](https://github.com/dropseed/plain/commit/b53d3bb7a7))
|
10
|
+
- Agent commands now print prompts directly when running in Claude Code or Codex Sandbox environments ([6eaed8ae3b](https://github.com/dropseed/plain/commit/6eaed8ae3b))
|
11
|
+
|
12
|
+
### Upgrade instructions
|
13
|
+
|
14
|
+
- Remove any usage of `CsrfExemptViewMixin` and `request.csrf_exempt` and add exempt paths to the `CSRF_EXEMPT_PATHS` setting instead (ex. `CSRF_EXEMPT_PATHS = [r"^/api/", r"/webhooks/.*"]` -- but consider first whether the view still needs CSRF exemption under the new implementation)
|
15
|
+
- Replace `HTTPS_REDIRECT_EXEMPT` with `HTTPS_REDIRECT_EXEMPT_PATHS` and ensure patterns include leading slash (ex. `[r"^/health$", r"/api/internal/.*"]`)
|
16
|
+
- Remove all CSRF cookie and token related settings - the new implementation doesn't use cookies or tokens (ex. `{{ csrf_input }}` and `{{ csrf_token }}`)
|
17
|
+
|
18
|
+
## [0.57.0](https://github.com/dropseed/plain/releases/plain@0.57.0) (2025-08-15)
|
19
|
+
|
20
|
+
### What's changed
|
21
|
+
|
22
|
+
- The `ResponsePermanentRedirect` class has been removed; use `ResponseRedirect` with `status_code=301` instead ([d5735ea](https://github.com/dropseed/plain/commit/d5735ea4f8))
|
23
|
+
- The `RedirectView.permanent` attribute has been replaced with `status_code` for more flexible redirect status codes ([12dda16](https://github.com/dropseed/plain/commit/12dda16731))
|
24
|
+
- Updated `RedirectView` initialization parameters: `url_name` replaces `pattern_name`, `preserve_query_params` replaces `query_string`, and removed 410 Gone functionality ([3b9ca71](https://github.com/dropseed/plain/commit/3b9ca713bf))
|
25
|
+
|
26
|
+
### Upgrade instructions
|
27
|
+
|
28
|
+
- Replace `ResponsePermanentRedirect` imports with `ResponseRedirect` and pass `status_code=301` to the constructor
|
29
|
+
- Update `RedirectView` subclasses to use `status_code=301` instead of `permanent=True`
|
30
|
+
- Replace `pattern_name` with `url_name` in RedirectView usage
|
31
|
+
- Replace `query_string=True` with `preserve_query_params=True` in RedirectView usage
|
32
|
+
|
3
33
|
## [0.56.1](https://github.com/dropseed/plain/releases/plain@0.56.1) (2025-07-30)
|
4
34
|
|
5
35
|
### What's changed
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import os
|
1
2
|
import shlex
|
2
3
|
import subprocess
|
3
4
|
|
@@ -20,6 +21,11 @@ def prompt_agent(
|
|
20
21
|
True if the agent command succeeded (or no agent command was provided),
|
21
22
|
False if the agent command failed.
|
22
23
|
"""
|
24
|
+
# Check if running inside an agent and just print the prompt if so
|
25
|
+
if os.environ.get("CLAUDECODE") or os.environ.get("CODEX_SANDBOX"):
|
26
|
+
click.echo(prompt)
|
27
|
+
return True
|
28
|
+
|
23
29
|
if print_only or not agent_command:
|
24
30
|
click.echo(prompt)
|
25
31
|
if not print_only:
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# CSRF
|
2
|
+
|
3
|
+
**Cross-Site Request Forgery (CSRF) protection using modern request headers.**
|
4
|
+
|
5
|
+
- [Overview](#overview)
|
6
|
+
- [Usage](#usage)
|
7
|
+
- [CSRF Exempt Paths](#csrf-exempt-paths)
|
8
|
+
- [Trusted Origins](#trusted-origins)
|
9
|
+
|
10
|
+
## Overview
|
11
|
+
|
12
|
+
Plain provides modern CSRF protection based on [Filippo Valsorda's 2025 research](https://words.filippo.io/csrf/) using `Sec-Fetch-Site` headers and origin validation.
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
The `CsrfViewMiddleware` is [automatically installed](../internal/handlers/base.py#BUILTIN_BEFORE_MIDDLEWARE) and works transparently. **No changes to your forms or templates are needed.**
|
17
|
+
|
18
|
+
## CSRF Exempt Paths
|
19
|
+
|
20
|
+
In some cases, you may need to disable CSRF protection for specific paths (like API endpoints or webhooks). Configure exempt paths using regex patterns in your settings:
|
21
|
+
|
22
|
+
```python
|
23
|
+
# settings.py
|
24
|
+
CSRF_EXEMPT_PATHS = [
|
25
|
+
r"^/api/", # All API endpoints
|
26
|
+
r"^/api/v\d+/", # Versioned APIs: /api/v1/, /api/v2/, etc.
|
27
|
+
r"/webhooks/.*", # All webhook paths
|
28
|
+
r"/webhooks/github/", # Specific webhook
|
29
|
+
r"/health$", # Exact match for /health endpoint
|
30
|
+
]
|
31
|
+
```
|
32
|
+
|
33
|
+
**Pattern Matching**: Exempt paths use Python regex patterns with `re.search()` against the full URL path including the leading slash.
|
34
|
+
|
35
|
+
**Examples:**
|
36
|
+
|
37
|
+
- `r"^/api/"` - matches `/api/users/`, `/api/posts/`
|
38
|
+
- `r"/webhooks/.*"` - matches `/webhooks/github/push`, `/webhooks/stripe/payment`
|
39
|
+
- `r"/health$"` - matches `/health` but not `/health-check`
|
40
|
+
- `r"^/api/v\d+/"` - matches `/api/v1/users/`, `/api/v2/posts/`
|
41
|
+
|
42
|
+
**Common Use Cases:**
|
43
|
+
|
44
|
+
```python
|
45
|
+
CSRF_EXEMPT_PATHS = [
|
46
|
+
# API endpoints (often consumed by JavaScript/mobile apps)
|
47
|
+
r"^/api/",
|
48
|
+
|
49
|
+
# Webhooks (external services posting data)
|
50
|
+
r"/webhooks/.*",
|
51
|
+
|
52
|
+
# Health checks and monitoring
|
53
|
+
r"/health$",
|
54
|
+
r"/status$",
|
55
|
+
r"/metrics$",
|
56
|
+
|
57
|
+
# File uploads (if using direct POST)
|
58
|
+
r"/upload/",
|
59
|
+
]
|
60
|
+
```
|
61
|
+
|
62
|
+
## Trusted Origins
|
63
|
+
|
64
|
+
In some cases, you may need to allow requests from specific external origins (like API clients or mobile apps). You can configure trusted origins in your settings:
|
65
|
+
|
66
|
+
```python
|
67
|
+
# settings.py
|
68
|
+
CSRF_TRUSTED_ORIGINS = [
|
69
|
+
"https://api.example.com",
|
70
|
+
"https://mobile.example.com:8443",
|
71
|
+
"https://trusted-partner.com",
|
72
|
+
]
|
73
|
+
```
|
74
|
+
|
75
|
+
**Important**: Trusted origins bypass **all** CSRF protection. Only add origins you completely trust, as they can make requests that appear to come from your users.
|
@@ -0,0 +1,134 @@
|
|
1
|
+
import logging
|
2
|
+
import re
|
3
|
+
from urllib.parse import urlparse
|
4
|
+
|
5
|
+
from plain.exceptions import DisallowedHost
|
6
|
+
from plain.logs import log_response
|
7
|
+
from plain.runtime import settings
|
8
|
+
|
9
|
+
from .views import CsrfFailureView
|
10
|
+
|
11
|
+
logger = logging.getLogger("plain.security.csrf")
|
12
|
+
|
13
|
+
|
14
|
+
class CsrfViewMiddleware:
|
15
|
+
"""
|
16
|
+
Modern CSRF protection middleware using Sec-Fetch-Site headers and origin validation.
|
17
|
+
Based on Filippo Valsorda's 2025 research (https://words.filippo.io/csrf/).
|
18
|
+
|
19
|
+
Note: This provides same-origin (not same-site) protection. Same-site origins
|
20
|
+
like subdomains can have different trust levels and are rejected.
|
21
|
+
"""
|
22
|
+
|
23
|
+
def __init__(self, get_response):
|
24
|
+
self.get_response = get_response
|
25
|
+
|
26
|
+
# Compile CSRF exempt patterns once for performance
|
27
|
+
self.csrf_exempt_patterns = [re.compile(r) for r in settings.CSRF_EXEMPT_PATHS]
|
28
|
+
|
29
|
+
def __call__(self, request):
|
30
|
+
allowed, reason = self.should_allow_request(request)
|
31
|
+
|
32
|
+
if allowed:
|
33
|
+
return self.get_response(request)
|
34
|
+
else:
|
35
|
+
return self.reject(request, reason)
|
36
|
+
|
37
|
+
def should_allow_request(self, request) -> tuple[bool, str]:
|
38
|
+
# 1. Allow safe methods (GET, HEAD, OPTIONS)
|
39
|
+
if request.method in ("GET", "HEAD", "OPTIONS"):
|
40
|
+
return True, f"Safe HTTP method: {request.method}"
|
41
|
+
|
42
|
+
# 2. Path-based exemption (regex patterns)
|
43
|
+
for pattern in self.csrf_exempt_patterns:
|
44
|
+
if pattern.search(request.path_info):
|
45
|
+
return (
|
46
|
+
True,
|
47
|
+
f"Path {request.path_info} matches exempt pattern {pattern.pattern}",
|
48
|
+
)
|
49
|
+
|
50
|
+
origin = request.headers.get("Origin")
|
51
|
+
sec_fetch_site = request.headers.get("Sec-Fetch-Site", "").lower()
|
52
|
+
|
53
|
+
# 3. Check trusted origins allow-list
|
54
|
+
|
55
|
+
if origin and origin in settings.CSRF_TRUSTED_ORIGINS:
|
56
|
+
return True, f"Trusted origin: {origin}"
|
57
|
+
|
58
|
+
# 4. Primary protection: Check Sec-Fetch-Site header
|
59
|
+
if sec_fetch_site in ("same-origin", "none"):
|
60
|
+
return (
|
61
|
+
True,
|
62
|
+
f"Same-origin request from Sec-Fetch-Site: {sec_fetch_site}",
|
63
|
+
)
|
64
|
+
elif sec_fetch_site in ("cross-site", "same-site"):
|
65
|
+
return (
|
66
|
+
False,
|
67
|
+
f"Cross-origin request detected from Sec-Fetch-Site: {sec_fetch_site}",
|
68
|
+
)
|
69
|
+
|
70
|
+
# 5. No fetch metadata or Origin headers - allow (non-browser requests)
|
71
|
+
if not origin and not sec_fetch_site:
|
72
|
+
return (
|
73
|
+
True,
|
74
|
+
"No Origin or Sec-Fetch-Site header - likely non-browser or old browser",
|
75
|
+
)
|
76
|
+
|
77
|
+
# 6. Fallback: Origin vs Host comparison for older browsers
|
78
|
+
# Note: On pre-2023 browsers, HTTP→HTTPS transitions may cause mismatches
|
79
|
+
# (Origin shows :443, request sees :80 if TLS terminated upstream).
|
80
|
+
# HSTS helps here; otherwise add external origins to CSRF_TRUSTED_ORIGINS.
|
81
|
+
if origin == "null":
|
82
|
+
return False, "Cross-origin request detected - null Origin header"
|
83
|
+
|
84
|
+
try:
|
85
|
+
if (parsed_origin := urlparse(origin)) and (host := request.get_host()):
|
86
|
+
# Scheme-agnostic host:port comparison
|
87
|
+
origin_host = parsed_origin.hostname
|
88
|
+
origin_port = parsed_origin.port or (
|
89
|
+
80
|
90
|
+
if parsed_origin.scheme == "http"
|
91
|
+
else 443
|
92
|
+
if parsed_origin.scheme == "https"
|
93
|
+
else None
|
94
|
+
)
|
95
|
+
|
96
|
+
# Extract hostname from request host (similar to how we parse origin)
|
97
|
+
# Use a fake scheme since we only care about host parsing
|
98
|
+
parsed_host = urlparse(f"http://{host}")
|
99
|
+
request_host = parsed_host.hostname or host
|
100
|
+
request_port = request.get_port()
|
101
|
+
|
102
|
+
# Compare hostname and port (scheme-agnostic)
|
103
|
+
# Both origin_host and request_host are normalized by urlparse (IPv6 brackets stripped)
|
104
|
+
if origin_host and origin_port and request_host and request_port:
|
105
|
+
if (
|
106
|
+
origin_host.lower() == request_host.lower()
|
107
|
+
and origin_port == int(request_port)
|
108
|
+
):
|
109
|
+
return (
|
110
|
+
True,
|
111
|
+
f"Same-origin request - Origin {origin} matches Host {host}",
|
112
|
+
)
|
113
|
+
except (ValueError, DisallowedHost):
|
114
|
+
pass
|
115
|
+
|
116
|
+
# Origin present but doesn't match host
|
117
|
+
return (
|
118
|
+
False,
|
119
|
+
f"Cross-origin request detected - Origin {origin} does not match Host",
|
120
|
+
)
|
121
|
+
|
122
|
+
def reject(self, request, reason):
|
123
|
+
"""Reject a request with a 403 Forbidden response."""
|
124
|
+
|
125
|
+
response = CsrfFailureView.as_view()(request, reason=reason)
|
126
|
+
log_response(
|
127
|
+
"Forbidden (%s): %s",
|
128
|
+
reason,
|
129
|
+
request.path,
|
130
|
+
response=response,
|
131
|
+
request=request,
|
132
|
+
logger=logger,
|
133
|
+
)
|
134
|
+
return response
|
@@ -6,6 +6,23 @@ import operator
|
|
6
6
|
|
7
7
|
from plain.utils.hashable import make_hashable
|
8
8
|
|
9
|
+
# MARK: Configuration and Registry
|
10
|
+
|
11
|
+
|
12
|
+
class PackageRegistryNotReady(Exception):
|
13
|
+
"""The plain.packages registry is not populated yet"""
|
14
|
+
|
15
|
+
pass
|
16
|
+
|
17
|
+
|
18
|
+
class ImproperlyConfigured(Exception):
|
19
|
+
"""Plain is somehow improperly configured"""
|
20
|
+
|
21
|
+
pass
|
22
|
+
|
23
|
+
|
24
|
+
# MARK: Model and Field Errors
|
25
|
+
|
9
26
|
|
10
27
|
class FieldDoesNotExist(Exception):
|
11
28
|
"""The requested model field does not exist"""
|
@@ -13,8 +30,8 @@ class FieldDoesNotExist(Exception):
|
|
13
30
|
pass
|
14
31
|
|
15
32
|
|
16
|
-
class
|
17
|
-
"""
|
33
|
+
class FieldError(Exception):
|
34
|
+
"""Some kind of problem with a model field."""
|
18
35
|
|
19
36
|
pass
|
20
37
|
|
@@ -31,6 +48,9 @@ class MultipleObjectsReturned(Exception):
|
|
31
48
|
pass
|
32
49
|
|
33
50
|
|
51
|
+
# MARK: Security and Suspicious Operations
|
52
|
+
|
53
|
+
|
34
54
|
class SuspiciousOperation(Exception):
|
35
55
|
"""The user did something suspicious"""
|
36
56
|
|
@@ -53,12 +73,6 @@ class DisallowedHost(SuspiciousOperation):
|
|
53
73
|
pass
|
54
74
|
|
55
75
|
|
56
|
-
class DisallowedRedirect(SuspiciousOperation):
|
57
|
-
"""Redirect to scheme not in allowed list"""
|
58
|
-
|
59
|
-
pass
|
60
|
-
|
61
|
-
|
62
76
|
class TooManyFieldsSent(SuspiciousOperation):
|
63
77
|
"""
|
64
78
|
The number of fields in a GET or POST request exceeded
|
@@ -86,6 +100,9 @@ class RequestDataTooBig(SuspiciousOperation):
|
|
86
100
|
pass
|
87
101
|
|
88
102
|
|
103
|
+
# MARK: HTTP and Request Errors
|
104
|
+
|
105
|
+
|
89
106
|
class BadRequest(Exception):
|
90
107
|
"""The request is malformed and cannot be processed."""
|
91
108
|
|
@@ -98,17 +115,7 @@ class PermissionDenied(Exception):
|
|
98
115
|
pass
|
99
116
|
|
100
117
|
|
101
|
-
|
102
|
-
"""Plain is somehow improperly configured"""
|
103
|
-
|
104
|
-
pass
|
105
|
-
|
106
|
-
|
107
|
-
class FieldError(Exception):
|
108
|
-
"""Some kind of problem with a model field."""
|
109
|
-
|
110
|
-
pass
|
111
|
-
|
118
|
+
# MARK: Validation
|
112
119
|
|
113
120
|
NON_FIELD_ERRORS = "__all__"
|
114
121
|
|
@@ -211,6 +218,9 @@ class ValidationError(Exception):
|
|
211
218
|
return hash(tuple(sorted(self.error_list, key=operator.attrgetter("message"))))
|
212
219
|
|
213
220
|
|
221
|
+
# MARK: Database
|
222
|
+
|
223
|
+
|
214
224
|
class EmptyResultSet(Exception):
|
215
225
|
"""A database query predicate is impossible."""
|
216
226
|
|
@@ -19,7 +19,6 @@ from plain.http.response import (
|
|
19
19
|
ResponseNotAllowed,
|
20
20
|
ResponseNotFound,
|
21
21
|
ResponseNotModified,
|
22
|
-
ResponsePermanentRedirect,
|
23
22
|
ResponseRedirect,
|
24
23
|
ResponseServerError,
|
25
24
|
StreamingResponse,
|
@@ -36,7 +35,6 @@ __all__ = [
|
|
36
35
|
"ResponseBase",
|
37
36
|
"StreamingResponse",
|
38
37
|
"ResponseRedirect",
|
39
|
-
"ResponsePermanentRedirect",
|
40
38
|
"ResponseNotModified",
|
41
39
|
"ResponseBadRequest",
|
42
40
|
"ResponseForbidden",
|
@@ -10,10 +10,8 @@ from email.header import Header
|
|
10
10
|
from functools import cached_property
|
11
11
|
from http.client import responses
|
12
12
|
from http.cookies import SimpleCookie
|
13
|
-
from urllib.parse import urlparse
|
14
13
|
|
15
14
|
from plain import signals
|
16
|
-
from plain.exceptions import DisallowedRedirect
|
17
15
|
from plain.http.cookie import sign_cookie_value
|
18
16
|
from plain.json import PlainJSONEncoder
|
19
17
|
from plain.runtime import settings
|
@@ -566,19 +564,18 @@ class FileResponse(StreamingResponse):
|
|
566
564
|
self.headers["Content-Disposition"] = content_disposition
|
567
565
|
|
568
566
|
|
569
|
-
class
|
570
|
-
|
567
|
+
class ResponseRedirect(Response):
|
568
|
+
"""HTTP redirect response"""
|
569
|
+
|
570
|
+
status_code = 302
|
571
571
|
|
572
572
|
def __init__(self, redirect_to, **kwargs):
|
573
573
|
super().__init__(**kwargs)
|
574
574
|
self.headers["Location"] = iri_to_uri(redirect_to)
|
575
|
-
parsed = urlparse(str(redirect_to))
|
576
|
-
if parsed.scheme and parsed.scheme not in self.allowed_schemes:
|
577
|
-
raise DisallowedRedirect(
|
578
|
-
f"Unsafe redirect to URL with protocol '{parsed.scheme}'"
|
579
|
-
)
|
580
575
|
|
581
|
-
|
576
|
+
@property
|
577
|
+
def url(self):
|
578
|
+
return self.headers["Location"]
|
582
579
|
|
583
580
|
def __repr__(self):
|
584
581
|
return (
|
@@ -592,18 +589,6 @@ class ResponseRedirectBase(Response):
|
|
592
589
|
)
|
593
590
|
|
594
591
|
|
595
|
-
class ResponseRedirect(ResponseRedirectBase):
|
596
|
-
"""HTTP 302 response"""
|
597
|
-
|
598
|
-
status_code = 302
|
599
|
-
|
600
|
-
|
601
|
-
class ResponsePermanentRedirect(ResponseRedirectBase):
|
602
|
-
"""HTTP 301 response"""
|
603
|
-
|
604
|
-
status_code = 301
|
605
|
-
|
606
|
-
|
607
592
|
class ResponseNotModified(Response):
|
608
593
|
"""HTTP 304 response"""
|
609
594
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import re
|
2
2
|
|
3
|
-
from plain.http import
|
3
|
+
from plain.http import ResponseRedirect
|
4
4
|
from plain.runtime import settings
|
5
5
|
|
6
6
|
|
@@ -12,7 +12,7 @@ class HttpsRedirectMiddleware:
|
|
12
12
|
self.https_redirect_enabled = settings.HTTPS_REDIRECT_ENABLED
|
13
13
|
self.https_redirect_host = settings.HTTPS_REDIRECT_HOST
|
14
14
|
self.https_redirect_exempt = [
|
15
|
-
re.compile(r) for r in settings.
|
15
|
+
re.compile(r) for r in settings.HTTPS_REDIRECT_EXEMPT_PATHS
|
16
16
|
]
|
17
17
|
|
18
18
|
def __call__(self, request):
|
@@ -26,11 +26,15 @@ class HttpsRedirectMiddleware:
|
|
26
26
|
return self.get_response(request)
|
27
27
|
|
28
28
|
def maybe_https_redirect(self, request):
|
29
|
-
path = request.path.lstrip("/")
|
30
29
|
if (
|
31
30
|
self.https_redirect_enabled
|
32
31
|
and not request.is_https()
|
33
|
-
and not any(
|
32
|
+
and not any(
|
33
|
+
pattern.search(request.path_info)
|
34
|
+
for pattern in self.https_redirect_exempt
|
35
|
+
)
|
34
36
|
):
|
35
37
|
host = self.https_redirect_host or request.get_host()
|
36
|
-
return
|
38
|
+
return ResponseRedirect(
|
39
|
+
f"https://{host}{request.get_full_path()}", status_code=301
|
40
|
+
)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
from plain.http import
|
1
|
+
from plain.http import ResponseRedirect
|
2
2
|
from plain.runtime import settings
|
3
3
|
from plain.urls import Resolver404, get_resolver
|
4
4
|
from plain.utils.http import escape_leading_slashes
|
@@ -22,7 +22,9 @@ class RedirectSlashMiddleware:
|
|
22
22
|
# If the given URL is "Not Found", then check if we should redirect to
|
23
23
|
# a path with a slash appended.
|
24
24
|
if response.status_code == 404 and self.should_redirect_with_slash(request):
|
25
|
-
return
|
25
|
+
return ResponseRedirect(
|
26
|
+
self.get_full_path_with_slash(request), status_code=301
|
27
|
+
)
|
26
28
|
|
27
29
|
return response
|
28
30
|
|
@@ -3,31 +3,20 @@ Default Plain settings. Override these with settings in the module pointed to
|
|
3
3
|
by the PLAIN_SETTINGS_MODULE environment variable.
|
4
4
|
"""
|
5
5
|
|
6
|
-
|
7
|
-
# CORE #
|
8
|
-
####################
|
6
|
+
# MARK: Core Settings
|
9
7
|
|
10
8
|
DEBUG: bool = False
|
11
9
|
|
12
|
-
# Hosts/domain names that are valid for this site.
|
13
|
-
# "*" matches anything, ".example.com" matches example.com and all subdomains
|
14
|
-
ALLOWED_HOSTS: list[str] = []
|
15
|
-
|
16
|
-
# Local time zone for this installation. All choices can be found here:
|
17
|
-
# https://en.wikipedia.org/wiki/List_of_tz_zones_by_name (although not all
|
18
|
-
# systems may support all possibilities). This is interpreted as the default
|
19
|
-
# user time zone.
|
20
|
-
TIME_ZONE: str = "UTC"
|
21
|
-
|
22
|
-
# Default charset to use for all Response objects, if a MIME type isn't
|
23
|
-
# manually specified. It's used to construct the Content-Type header.
|
24
|
-
DEFAULT_CHARSET = "utf-8"
|
25
|
-
|
26
10
|
# List of strings representing installed packages.
|
27
11
|
INSTALLED_PACKAGES: list[str] = []
|
28
12
|
|
29
|
-
|
30
|
-
|
13
|
+
URLS_ROUTER: str
|
14
|
+
|
15
|
+
# MARK: HTTP and Security
|
16
|
+
|
17
|
+
# Hosts/domain names that are valid for this site.
|
18
|
+
# "*" matches anything, ".example.com" matches example.com and all subdomains
|
19
|
+
ALLOWED_HOSTS: list[str] = []
|
31
20
|
|
32
21
|
# Default headers for all responses.
|
33
22
|
DEFAULT_RESPONSE_HEADERS = {
|
@@ -42,7 +31,13 @@ DEFAULT_RESPONSE_HEADERS = {
|
|
42
31
|
|
43
32
|
# Whether to redirect all non-HTTPS requests to HTTPS.
|
44
33
|
HTTPS_REDIRECT_ENABLED = True
|
45
|
-
|
34
|
+
|
35
|
+
# Regex patterns for paths that should be exempt from HTTPS redirect
|
36
|
+
# Examples: [r"^/health$", r"/api/internal/.*", r"/dev/.*"]
|
37
|
+
HTTPS_REDIRECT_EXEMPT_PATHS: list[str] = []
|
38
|
+
|
39
|
+
# Custom host to redirect to for HTTPS. If None, uses the same host as the request.
|
40
|
+
# Useful for redirecting to a different domain (e.g., "secure.example.com")
|
46
41
|
HTTPS_REDIRECT_HOST = None
|
47
42
|
|
48
43
|
# If your Plain app is behind a proxy that sets a header to specify secure
|
@@ -68,7 +63,24 @@ SECRET_KEY: str
|
|
68
63
|
# secret key rotation.
|
69
64
|
SECRET_KEY_FALLBACKS: list[str] = []
|
70
65
|
|
71
|
-
|
66
|
+
# MARK: Internationalization
|
67
|
+
|
68
|
+
# Local time zone for this installation. All choices can be found here:
|
69
|
+
# https://en.wikipedia.org/wiki/List_of_tz_zones_by_name (although not all
|
70
|
+
# systems may support all possibilities). This is interpreted as the default
|
71
|
+
# user time zone.
|
72
|
+
TIME_ZONE: str = "UTC"
|
73
|
+
|
74
|
+
# Default charset to use for all Response objects, if a MIME type isn't
|
75
|
+
# manually specified. It's used to construct the Content-Type header.
|
76
|
+
DEFAULT_CHARSET = "utf-8"
|
77
|
+
|
78
|
+
# MARK: URL Configuration
|
79
|
+
|
80
|
+
# Whether to append trailing slashes to URLs.
|
81
|
+
APPEND_SLASH = True
|
82
|
+
|
83
|
+
# MARK: File Uploads
|
72
84
|
|
73
85
|
# List of upload handler classes to be applied in order.
|
74
86
|
FILE_UPLOAD_HANDLERS = [
|
@@ -100,41 +112,30 @@ FILE_UPLOAD_TEMP_DIR = None
|
|
100
112
|
# User-defined overrides for error views by status code
|
101
113
|
HTTP_ERROR_VIEWS: dict[int] = {}
|
102
114
|
|
103
|
-
|
104
|
-
# MIDDLEWARE #
|
105
|
-
##############
|
115
|
+
# MARK: Middleware
|
106
116
|
|
107
117
|
# List of middleware to use. Order is important; in the request phase, these
|
108
118
|
# middleware will be applied in the order given, and in the response
|
109
119
|
# phase the middleware will be applied in reverse order.
|
110
120
|
MIDDLEWARE: list[str] = []
|
111
121
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
#
|
117
|
-
CSRF_COOKIE_NAME = "csrftoken"
|
118
|
-
CSRF_COOKIE_AGE = 60 * 60 * 24 * 7 * 52 # 1 year
|
119
|
-
CSRF_COOKIE_DOMAIN = None
|
120
|
-
CSRF_COOKIE_PATH = "/"
|
121
|
-
CSRF_COOKIE_SECURE = True
|
122
|
-
CSRF_COOKIE_HTTPONLY = False
|
123
|
-
CSRF_COOKIE_SAMESITE = "Lax"
|
124
|
-
CSRF_HEADER_NAME = "CSRF-Token"
|
125
|
-
CSRF_FIELD_NAME = "_csrftoken"
|
122
|
+
# MARK: CSRF
|
123
|
+
|
124
|
+
# A list of trusted origins for unsafe (POST/PUT/DELETE etc.) requests.
|
125
|
+
# These origins will be allowed regardless of the normal CSRF checks.
|
126
|
+
# Each origin should be a full origin like "https://example.com" or "https://sub.example.com:8080"
|
126
127
|
CSRF_TRUSTED_ORIGINS: list[str] = []
|
127
128
|
|
128
|
-
|
129
|
-
#
|
130
|
-
|
129
|
+
# Regex patterns for paths that should be exempt from CSRF protection
|
130
|
+
# Examples: [r"^/api/", r"/webhooks/.*", r"/health$"]
|
131
|
+
CSRF_EXEMPT_PATHS: list[str] = []
|
132
|
+
|
133
|
+
# MARK: Logging
|
131
134
|
|
132
135
|
# Custom logging configuration.
|
133
136
|
LOGGING = {}
|
134
137
|
|
135
|
-
|
136
|
-
# ASSETS #
|
137
|
-
###############
|
138
|
+
# MARK: Assets
|
138
139
|
|
139
140
|
# Whether to redirect the original asset path to the fingerprinted path.
|
140
141
|
ASSETS_REDIRECT_ORIGINAL = True
|
@@ -143,9 +144,7 @@ ASSETS_REDIRECT_ORIGINAL = True
|
|
143
144
|
# Ex. "https://cdn.example.com/assets/"
|
144
145
|
ASSETS_BASE_URL: str = ""
|
145
146
|
|
146
|
-
|
147
|
-
# PREFLIGHT CHECKS #
|
148
|
-
####################
|
147
|
+
# MARK: Preflight Checks
|
149
148
|
|
150
149
|
# List of all issues generated by system checks that should be silenced. Light
|
151
150
|
# issues like warnings, infos or debugs will not generate a message. Silencing
|
@@ -153,14 +152,10 @@ ASSETS_BASE_URL: str = ""
|
|
153
152
|
# message, but Plain will not stop you from e.g. running server.
|
154
153
|
PREFLIGHT_SILENCED_CHECKS = []
|
155
154
|
|
156
|
-
|
157
|
-
# Templates #
|
158
|
-
#############
|
155
|
+
# MARK: Templates
|
159
156
|
|
160
157
|
TEMPLATES_JINJA_ENVIRONMENT = "plain.templates.jinja.DefaultEnvironment"
|
161
158
|
|
162
|
-
|
163
|
-
# Shell #
|
164
|
-
#########
|
159
|
+
# MARK: Shell
|
165
160
|
|
166
161
|
SHELL_IMPORT: str = ""
|