site-paths 0.1.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.
@@ -0,0 +1,7 @@
1
+ node_modules/
2
+ *.tgz
3
+ __pycache__/
4
+ *.egg-info/
5
+ dist/
6
+ .venv/
7
+ .mypy_cache/
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,226 @@
1
+ Metadata-Version: 2.5
2
+ Name: site-paths
3
+ Version: 0.1.0
4
+ Summary: Stop a site from telling strangers which private routes exist: a wall for ASGI, WSGI and Django.
5
+ Project-URL: Repository, https://github.com/webodik/toller-web
6
+ Project-URL: Issues, https://github.com/webodik/toller-web/issues
7
+ Project-URL: Why, https://github.com/webodik/toller-web/blob/main/docs/why.md
8
+ License-Expression: Apache-2.0
9
+ License-File: LICENSE
10
+ Keywords: access-control,asgi,django,fastapi,flask,middleware,route-enumeration,security,starlette,wsgi
11
+ Classifier: Framework :: Django
12
+ Classifier: Framework :: FastAPI
13
+ Classifier: Framework :: Flask
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Programming Language :: Python :: 3.15
19
+ Classifier: Topic :: Internet :: WWW/HTTP
20
+ Classifier: Topic :: Security
21
+ Classifier: Typing :: Typed
22
+ Requires-Python: >=3.11
23
+ Description-Content-Type: text/markdown
24
+
25
+ # site-paths
26
+
27
+ Stop your site from telling strangers which private routes exist.
28
+
29
+ If `/admin/reports` answers "sign in" and a made-up address answers
30
+ "not found", anybody can tell that your site has a reports page without
31
+ seeing a single report. Asking for a list of names gives the list of
32
+ what you have. Scanners do that in the first minutes of an attack, and
33
+ what they try next is built on it.
34
+ [Why hide whether a route exists?](https://github.com/webodik/toller-web/blob/main/docs/why.md)
35
+
36
+ This is the wall for Python: in your server, it answers every path of a
37
+ private part alike, there or not, for whoever your own code does not
38
+ let in. It is for ASGI (Starlette, FastAPI), WSGI (Flask) and Django.
39
+
40
+ No dependencies. Typed. Apache-2.0.
41
+
42
+ ## Install
43
+
44
+ ```sh
45
+ pip install site-paths
46
+ ```
47
+
48
+ Python 3.11 or later.
49
+
50
+ ## Quick start
51
+
52
+ **1. Ask your deployment what it gives away.** The check is one command
53
+ for a site written in anything, and needs Node.js where it runs, which
54
+ is not on your server. Name the private part and one route in it that
55
+ exists, at the address your visitors use:
56
+
57
+ ```sh
58
+ npx -p site-paths site-paths-check --url https://staging.shop.example --under /admin --has /admin/reports
59
+ ```
60
+
61
+ It asks as a stranger for that route and for made-up names beside it.
62
+ If anything tells them apart, it says what and ends with 1.
63
+ [What the check asks and what it takes](https://github.com/webodik/toller-web/tree/main/js#the-check).
64
+
65
+ **2. If it fails, put a wall before the part,** after whatever says who
66
+ is signed in and before your routes. On FastAPI:
67
+
68
+ ```python
69
+ from site_paths.asgi import Wall
70
+
71
+ app.add_middleware(
72
+ Wall,
73
+ under="/admin",
74
+ allow=lambda scope: scope["session"].get("role") == "staff",
75
+ missing="/nothing-here",
76
+ )
77
+ app.add_middleware(SessionMiddleware, secret_key=...)
78
+ ```
79
+
80
+ `allow` is your own decision about who is let in. `missing` is a path
81
+ your site does not have: whoever is not let in gets your site's own
82
+ answer for it, for every path under `/admin`, so a route that is there
83
+ and one that is not look the same. Staff go on to your routes as before.
84
+
85
+ FastAPI also lists every route in `/openapi.json` to whoever asks. Leave
86
+ the part's routes out of it, with
87
+ `APIRouter(prefix="/admin", include_in_schema=False)`: a wall keeps what
88
+ is asked for, and not what the site publishes of itself.
89
+
90
+ **3. Run the check again, and keep it where your tests run.** It fails
91
+ when a later change lets an answer tell again.
92
+
93
+ ## The wall
94
+
95
+ A wall runs before your site looks a route up, and asks your function
96
+ whether the caller is let in. That order is the point: an answer that
97
+ is made to look the same after the route was found can still be told
98
+ apart by how long it took.
99
+
100
+ ### FastAPI and Starlette
101
+
102
+ See [Quick start](#quick-start).
103
+
104
+ - **Add the wall before the middleware that says who is signed in:**
105
+ the session's, or `AuthenticationMiddleware`. The middleware that is
106
+ added last runs first.
107
+ - **Leave the part's routes out of what FastAPI publishes.** FastAPI
108
+ lists every route in `/openapi.json`, and shows them at `/docs`, to
109
+ whoever asks. A wall keeps what is asked for, and not what the site
110
+ says of itself:
111
+
112
+ ```python
113
+ staff = APIRouter(prefix="/admin", include_in_schema=False)
114
+ ```
115
+
116
+ - **`allow` is given the connection's scope.** A coroutine is awaited,
117
+ and a caller whose answer does not come in three seconds is kept out.
118
+ A plain function is called at once and is not cut short: have it read
119
+ only what the connection carries already, such as the session.
120
+ - **An application mounted below another** keeps its part whether
121
+ `under` names it from the top of the site or from where the
122
+ application is mounted.
123
+
124
+ ### Flask
125
+
126
+ ```python
127
+ from site_paths.wsgi import Wall
128
+
129
+ def is_staff(environ):
130
+ with app.app_context():
131
+ session = app.session_interface.open_session(app, app.request_class(environ))
132
+ return session is not None and session.get("role") == "staff"
133
+
134
+ app.wsgi_app = Wall(app.wsgi_app, under="/admin", allow=is_staff, missing="/nothing-here")
135
+ ```
136
+
137
+ The wall stands before the application, and so before Flask has opened
138
+ the session: `allow` is given the request's environment, and opens the
139
+ session itself as above. A session that is kept on the server needs the
140
+ application's context to open, which is what the `with` is for.
141
+
142
+ ### Django
143
+
144
+ ```python
145
+ # walls.py, in your project
146
+ from site_paths.django import wall
147
+
148
+ staff = wall(under="/admin", allow=lambda request: request.user.is_staff)
149
+ ```
150
+
151
+ ```python
152
+ # settings.py
153
+ MIDDLEWARE = [
154
+ "django.middleware.security.SecurityMiddleware",
155
+ "django.contrib.sessions.middleware.SessionMiddleware",
156
+ "django.contrib.auth.middleware.AuthenticationMiddleware",
157
+ "project.walls.staff",
158
+ "django.middleware.common.CommonMiddleware",
159
+ ...,
160
+ ]
161
+ ```
162
+
163
+ - **After the middleware that says who is signed in, and before
164
+ `CommonMiddleware` and `LocaleMiddleware`.** Those two look at an
165
+ answer that nothing is there and redirect where the path with a
166
+ slash, or with a language before it, is a route. Above the wall they
167
+ would redirect `/admin/reports` when `/admin/reports/` is a route,
168
+ and not a name that is no route, which tells the two apart. A wall
169
+ that finds either above itself does not start, and says why.
170
+ - **Whoever is not let in gets the site's own page for a missing
171
+ path,** as Django makes it.
172
+ - **Where routes carry a language before them** (`i18n_patterns`), the
173
+ part is under each language: keep it with a pattern, such as
174
+ `match=r"^/(en|de)/admin(/|$)"`.
175
+ - **Where Django runs on ASGI,** a decision that is not a coroutine is
176
+ made in a thread.
177
+
178
+ ### Options
179
+
180
+ | Option | |
181
+ | --- | --- |
182
+ | `under` | The start of the part, as a visitor asks for it: the path itself and everything below it |
183
+ | `match` | In place of `under`: a pattern for the paths to keep, such as `r"\.js\.map$"` |
184
+ | `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 |
185
+ | `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 |
186
+
187
+ ### What to know
188
+
189
+ - **A path is read as the most forgiving server would read it,** so no
190
+ other spelling of it goes round: `/Admin//reports`, `/%61dmin/reports`
191
+ and `/admin;v=1/reports` are all `/admin/reports`.
192
+ - **A path that enters the part and leaves it again by dots is kept
193
+ too,** such as `/admin/../reports`: these frameworks find a route by
194
+ the path as it is written. So is a path of more than 64 segments,
195
+ wherever it leads.
196
+ - **No cache may keep what a caller who is kept out is answered.** The
197
+ wall says `cache-control: private, no-store` on it, whatever made the
198
+ answer. What your staff are answered is yours to mark the same: a
199
+ cache in between that keeps it gives it to whoever asks next.
200
+ - **A decision that is not awaited is not cut short:** on WSGI, and a
201
+ plain function on ASGI. Give what it asks a time of its own to
202
+ answer in.
203
+ - **A websocket in the part is closed before it is accepted,** for
204
+ whoever is not let in.
205
+ - **A wall does not hide what your site sends every browser.** A script
206
+ that names a route has given it away. The check looks for that.
207
+ - **It does not stand before** anything your platform serves without
208
+ asking your application, such as static files at a CDN.
209
+ - **It is no replacement for authorization.** It keeps the list of
210
+ routes from strangers. What a signed-in caller may do is still your
211
+ code's to decide.
212
+
213
+ ## What it was tried on
214
+
215
+ Python 3.11 to 3.15, on every change. FastAPI on uvicorn, Flask on
216
+ gunicorn, and Django on gunicorn and on uvicorn, each asked by the
217
+ check as a stranger, a customer and the staff: without the wall the
218
+ check fails, with it the check passes. Django with its own
219
+ `CommonMiddleware` and routes that end with a slash; FastAPI with an
220
+ application mounted below it, and with nothing of the part in what it
221
+ publishes. No cache or CDN was put in front. It reads a path by the same two
222
+ lists that the JavaScript package beside it is held to.
223
+
224
+ ## License
225
+
226
+ Apache-2.0: see `LICENSE`.
@@ -0,0 +1,202 @@
1
+ # site-paths
2
+
3
+ Stop your site from telling strangers which private routes exist.
4
+
5
+ If `/admin/reports` answers "sign in" and a made-up address answers
6
+ "not found", anybody can tell that your site has a reports page without
7
+ seeing a single report. Asking for a list of names gives the list of
8
+ what you have. Scanners do that in the first minutes of an attack, and
9
+ what they try next is built on it.
10
+ [Why hide whether a route exists?](https://github.com/webodik/toller-web/blob/main/docs/why.md)
11
+
12
+ This is the wall for Python: in your server, it answers every path of a
13
+ private part alike, there or not, for whoever your own code does not
14
+ let in. It is for ASGI (Starlette, FastAPI), WSGI (Flask) and Django.
15
+
16
+ No dependencies. Typed. Apache-2.0.
17
+
18
+ ## Install
19
+
20
+ ```sh
21
+ pip install site-paths
22
+ ```
23
+
24
+ Python 3.11 or later.
25
+
26
+ ## Quick start
27
+
28
+ **1. Ask your deployment what it gives away.** The check is one command
29
+ for a site written in anything, and needs Node.js where it runs, which
30
+ is not on your server. Name the private part and one route in it that
31
+ exists, at the address your visitors use:
32
+
33
+ ```sh
34
+ npx -p site-paths site-paths-check --url https://staging.shop.example --under /admin --has /admin/reports
35
+ ```
36
+
37
+ It asks as a stranger for that route and for made-up names beside it.
38
+ If anything tells them apart, it says what and ends with 1.
39
+ [What the check asks and what it takes](https://github.com/webodik/toller-web/tree/main/js#the-check).
40
+
41
+ **2. If it fails, put a wall before the part,** after whatever says who
42
+ is signed in and before your routes. On FastAPI:
43
+
44
+ ```python
45
+ from site_paths.asgi import Wall
46
+
47
+ app.add_middleware(
48
+ Wall,
49
+ under="/admin",
50
+ allow=lambda scope: scope["session"].get("role") == "staff",
51
+ missing="/nothing-here",
52
+ )
53
+ app.add_middleware(SessionMiddleware, secret_key=...)
54
+ ```
55
+
56
+ `allow` is your own decision about who is let in. `missing` is a path
57
+ your site does not have: whoever is not let in gets your site's own
58
+ answer for it, for every path under `/admin`, so a route that is there
59
+ and one that is not look the same. Staff go on to your routes as before.
60
+
61
+ FastAPI also lists every route in `/openapi.json` to whoever asks. Leave
62
+ the part's routes out of it, with
63
+ `APIRouter(prefix="/admin", include_in_schema=False)`: a wall keeps what
64
+ is asked for, and not what the site publishes of itself.
65
+
66
+ **3. Run the check again, and keep it where your tests run.** It fails
67
+ when a later change lets an answer tell again.
68
+
69
+ ## The wall
70
+
71
+ A wall runs before your site looks a route up, and asks your function
72
+ whether the caller is let in. That order is the point: an answer that
73
+ is made to look the same after the route was found can still be told
74
+ apart by how long it took.
75
+
76
+ ### FastAPI and Starlette
77
+
78
+ See [Quick start](#quick-start).
79
+
80
+ - **Add the wall before the middleware that says who is signed in:**
81
+ the session's, or `AuthenticationMiddleware`. The middleware that is
82
+ added last runs first.
83
+ - **Leave the part's routes out of what FastAPI publishes.** FastAPI
84
+ lists every route in `/openapi.json`, and shows them at `/docs`, to
85
+ whoever asks. A wall keeps what is asked for, and not what the site
86
+ says of itself:
87
+
88
+ ```python
89
+ staff = APIRouter(prefix="/admin", include_in_schema=False)
90
+ ```
91
+
92
+ - **`allow` is given the connection's scope.** A coroutine is awaited,
93
+ and a caller whose answer does not come in three seconds is kept out.
94
+ A plain function is called at once and is not cut short: have it read
95
+ only what the connection carries already, such as the session.
96
+ - **An application mounted below another** keeps its part whether
97
+ `under` names it from the top of the site or from where the
98
+ application is mounted.
99
+
100
+ ### Flask
101
+
102
+ ```python
103
+ from site_paths.wsgi import Wall
104
+
105
+ def is_staff(environ):
106
+ with app.app_context():
107
+ session = app.session_interface.open_session(app, app.request_class(environ))
108
+ return session is not None and session.get("role") == "staff"
109
+
110
+ app.wsgi_app = Wall(app.wsgi_app, under="/admin", allow=is_staff, missing="/nothing-here")
111
+ ```
112
+
113
+ The wall stands before the application, and so before Flask has opened
114
+ the session: `allow` is given the request's environment, and opens the
115
+ session itself as above. A session that is kept on the server needs the
116
+ application's context to open, which is what the `with` is for.
117
+
118
+ ### Django
119
+
120
+ ```python
121
+ # walls.py, in your project
122
+ from site_paths.django import wall
123
+
124
+ staff = wall(under="/admin", allow=lambda request: request.user.is_staff)
125
+ ```
126
+
127
+ ```python
128
+ # settings.py
129
+ MIDDLEWARE = [
130
+ "django.middleware.security.SecurityMiddleware",
131
+ "django.contrib.sessions.middleware.SessionMiddleware",
132
+ "django.contrib.auth.middleware.AuthenticationMiddleware",
133
+ "project.walls.staff",
134
+ "django.middleware.common.CommonMiddleware",
135
+ ...,
136
+ ]
137
+ ```
138
+
139
+ - **After the middleware that says who is signed in, and before
140
+ `CommonMiddleware` and `LocaleMiddleware`.** Those two look at an
141
+ answer that nothing is there and redirect where the path with a
142
+ slash, or with a language before it, is a route. Above the wall they
143
+ would redirect `/admin/reports` when `/admin/reports/` is a route,
144
+ and not a name that is no route, which tells the two apart. A wall
145
+ that finds either above itself does not start, and says why.
146
+ - **Whoever is not let in gets the site's own page for a missing
147
+ path,** as Django makes it.
148
+ - **Where routes carry a language before them** (`i18n_patterns`), the
149
+ part is under each language: keep it with a pattern, such as
150
+ `match=r"^/(en|de)/admin(/|$)"`.
151
+ - **Where Django runs on ASGI,** a decision that is not a coroutine is
152
+ made in a thread.
153
+
154
+ ### Options
155
+
156
+ | Option | |
157
+ | --- | --- |
158
+ | `under` | The start of the part, as a visitor asks for it: the path itself and everything below it |
159
+ | `match` | In place of `under`: a pattern for the paths to keep, such as `r"\.js\.map$"` |
160
+ | `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 |
161
+ | `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 |
162
+
163
+ ### What to know
164
+
165
+ - **A path is read as the most forgiving server would read it,** so no
166
+ other spelling of it goes round: `/Admin//reports`, `/%61dmin/reports`
167
+ and `/admin;v=1/reports` are all `/admin/reports`.
168
+ - **A path that enters the part and leaves it again by dots is kept
169
+ too,** such as `/admin/../reports`: these frameworks find a route by
170
+ the path as it is written. So is a path of more than 64 segments,
171
+ wherever it leads.
172
+ - **No cache may keep what a caller who is kept out is answered.** The
173
+ wall says `cache-control: private, no-store` on it, whatever made the
174
+ answer. What your staff are answered is yours to mark the same: a
175
+ cache in between that keeps it gives it to whoever asks next.
176
+ - **A decision that is not awaited is not cut short:** on WSGI, and a
177
+ plain function on ASGI. Give what it asks a time of its own to
178
+ answer in.
179
+ - **A websocket in the part is closed before it is accepted,** for
180
+ whoever is not let in.
181
+ - **A wall does not hide what your site sends every browser.** A script
182
+ that names a route has given it away. The check looks for that.
183
+ - **It does not stand before** anything your platform serves without
184
+ asking your application, such as static files at a CDN.
185
+ - **It is no replacement for authorization.** It keeps the list of
186
+ routes from strangers. What a signed-in caller may do is still your
187
+ code's to decide.
188
+
189
+ ## What it was tried on
190
+
191
+ Python 3.11 to 3.15, on every change. FastAPI on uvicorn, Flask on
192
+ gunicorn, and Django on gunicorn and on uvicorn, each asked by the
193
+ check as a stranger, a customer and the staff: without the wall the
194
+ check fails, with it the check passes. Django with its own
195
+ `CommonMiddleware` and routes that end with a slash; FastAPI with an
196
+ application mounted below it, and with nothing of the part in what it
197
+ publishes. No cache or CDN was put in front. It reads a path by the same two
198
+ lists that the JavaScript package beside it is held to.
199
+
200
+ ## License
201
+
202
+ Apache-2.0: see `LICENSE`.
@@ -0,0 +1,40 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "site-paths"
7
+ version = "0.1.0"
8
+ description = "Stop a site from telling strangers which private routes exist: a wall for ASGI, WSGI and Django."
9
+ readme = "README.md"
10
+ license = "Apache-2.0"
11
+ license-files = ["LICENSE"]
12
+ requires-python = ">=3.11"
13
+ dependencies = []
14
+ keywords = ["security", "middleware", "access-control", "route-enumeration", "asgi", "wsgi", "django", "fastapi", "flask", "starlette"]
15
+ classifiers = [
16
+ "Programming Language :: Python :: 3.11",
17
+ "Programming Language :: Python :: 3.12",
18
+ "Programming Language :: Python :: 3.13",
19
+ "Programming Language :: Python :: 3.14",
20
+ "Programming Language :: Python :: 3.15",
21
+ "Framework :: Django",
22
+ "Framework :: FastAPI",
23
+ "Framework :: Flask",
24
+ "Topic :: Internet :: WWW/HTTP",
25
+ "Topic :: Security",
26
+ "Typing :: Typed",
27
+ ]
28
+
29
+ [project.urls]
30
+ Repository = "https://github.com/webodik/toller-web"
31
+ Issues = "https://github.com/webodik/toller-web/issues"
32
+ Why = "https://github.com/webodik/toller-web/blob/main/docs/why.md"
33
+
34
+ [tool.hatch.build.targets.wheel]
35
+ packages = ["src/site_paths"]
36
+
37
+ # The lists the tests read are at the top of the repository. A source
38
+ # archive carries them, so that its tests run where nothing else is.
39
+ [tool.hatch.build.targets.sdist.force-include]
40
+ "../vectors" = "vectors"