localhost-guard 0.1.0__tar.gz → 0.1.2__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.
- {localhost_guard-0.1.0 → localhost_guard-0.1.2}/PKG-INFO +18 -13
- {localhost_guard-0.1.0 → localhost_guard-0.1.2}/README.md +9 -12
- {localhost_guard-0.1.0 → localhost_guard-0.1.2}/localhost_guard.egg-info/PKG-INFO +18 -13
- {localhost_guard-0.1.0 → localhost_guard-0.1.2}/localhost_guard.py +11 -4
- {localhost_guard-0.1.0 → localhost_guard-0.1.2}/pyproject.toml +11 -1
- {localhost_guard-0.1.0 → localhost_guard-0.1.2}/tests/test_localhost_guard.py +58 -0
- {localhost_guard-0.1.0 → localhost_guard-0.1.2}/LICENSE +0 -0
- {localhost_guard-0.1.0 → localhost_guard-0.1.2}/localhost_guard.egg-info/SOURCES.txt +0 -0
- {localhost_guard-0.1.0 → localhost_guard-0.1.2}/localhost_guard.egg-info/dependency_links.txt +0 -0
- {localhost_guard-0.1.0 → localhost_guard-0.1.2}/localhost_guard.egg-info/top_level.txt +0 -0
- {localhost_guard-0.1.0 → localhost_guard-0.1.2}/setup.cfg +0 -0
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: localhost-guard
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Header checks that stop a random web page from driving your loopback HTTP service
|
|
5
5
|
Author: suimajian
|
|
6
6
|
License: MIT
|
|
7
7
|
Project-URL: Homepage, https://github.com/suimajian/localhost-guard
|
|
8
|
+
Project-URL: Source, https://github.com/suimajian/localhost-guard
|
|
9
|
+
Project-URL: Issues, https://github.com/suimajian/localhost-guard/issues
|
|
8
10
|
Keywords: localhost,csrf,dns-rebinding,security,http
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Security
|
|
16
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
|
|
9
17
|
Requires-Python: >=3.9
|
|
10
18
|
Description-Content-Type: text/markdown
|
|
11
19
|
License-File: LICENSE
|
|
@@ -33,17 +41,10 @@ paste into an existing `BaseHTTPRequestHandler`.
|
|
|
33
41
|
|
|
34
42
|
## Install
|
|
35
43
|
|
|
36
|
-
Not on PyPI yet — `pip install localhost-guard` will 404 until 0.1.0 is
|
|
37
|
-
published. Install from the repository instead:
|
|
38
|
-
|
|
39
44
|
```bash
|
|
40
|
-
pip install
|
|
45
|
+
pip install localhost-guard
|
|
41
46
|
```
|
|
42
47
|
|
|
43
|
-
The sdist and wheel are attached to the
|
|
44
|
-
[releases](https://github.com/suimajian/localhost-guard/releases); the publish
|
|
45
|
-
step is waiting for credentials, so nothing has been uploaded to the index.
|
|
46
|
-
|
|
47
48
|
## Use
|
|
48
49
|
|
|
49
50
|
```python
|
|
@@ -103,7 +104,9 @@ class Handler(LoopbackOnly, BaseHTTPRequestHandler):
|
|
|
103
104
|
approval. Never put a state change behind GET (`/delete?id=`, `/sync`,
|
|
104
105
|
`/switch-profile`) — that endpoint is unprotected no matter what this library
|
|
105
106
|
does. Either keep every mutation on POST/PUT/PATCH/DELETE, or call
|
|
106
|
-
`guard(write=True)` in that handler so the write rules apply to it anyway
|
|
107
|
+
`guard(write=True)` in that handler so the write rules apply to it anyway
|
|
108
|
+
(that flag now reaches `check()`; the Host and Content-Type rules then apply
|
|
109
|
+
even though the method is `GET`).
|
|
107
110
|
- **It is a function, not middleware.** Every handler has to call it; one handler
|
|
108
111
|
that forgets is one unguarded endpoint, silently. If you want it enforced
|
|
109
112
|
structurally, wrap your framework's dispatcher so a response cannot be produced
|
|
@@ -132,9 +135,11 @@ class Handler(LoopbackOnly, BaseHTTPRequestHandler):
|
|
|
132
135
|
|
|
133
136
|
## Status
|
|
134
137
|
|
|
135
|
-
0.1.
|
|
136
|
-
|
|
137
|
-
|
|
138
|
+
0.1.2 — on PyPI (`pip install localhost-guard`); 0.1.1 is yanked and 0.1.0 is
|
|
139
|
+
yanked as well. 0.1.1 silently ignored `guard(write=True)` on `GET`; 0.1.0
|
|
140
|
+
additionally carried the pre-release README. Trusted Publishing (OIDC) does the upload,
|
|
141
|
+
so no token lives in this repository. Tags (`v*`) build sdist + wheel and attach
|
|
142
|
+
them to the GitHub release.
|
|
138
143
|
|
|
139
144
|
## Tests
|
|
140
145
|
|
|
@@ -20,17 +20,10 @@ paste into an existing `BaseHTTPRequestHandler`.
|
|
|
20
20
|
|
|
21
21
|
## Install
|
|
22
22
|
|
|
23
|
-
Not on PyPI yet — `pip install localhost-guard` will 404 until 0.1.0 is
|
|
24
|
-
published. Install from the repository instead:
|
|
25
|
-
|
|
26
23
|
```bash
|
|
27
|
-
pip install
|
|
24
|
+
pip install localhost-guard
|
|
28
25
|
```
|
|
29
26
|
|
|
30
|
-
The sdist and wheel are attached to the
|
|
31
|
-
[releases](https://github.com/suimajian/localhost-guard/releases); the publish
|
|
32
|
-
step is waiting for credentials, so nothing has been uploaded to the index.
|
|
33
|
-
|
|
34
27
|
## Use
|
|
35
28
|
|
|
36
29
|
```python
|
|
@@ -90,7 +83,9 @@ class Handler(LoopbackOnly, BaseHTTPRequestHandler):
|
|
|
90
83
|
approval. Never put a state change behind GET (`/delete?id=`, `/sync`,
|
|
91
84
|
`/switch-profile`) — that endpoint is unprotected no matter what this library
|
|
92
85
|
does. Either keep every mutation on POST/PUT/PATCH/DELETE, or call
|
|
93
|
-
`guard(write=True)` in that handler so the write rules apply to it anyway
|
|
86
|
+
`guard(write=True)` in that handler so the write rules apply to it anyway
|
|
87
|
+
(that flag now reaches `check()`; the Host and Content-Type rules then apply
|
|
88
|
+
even though the method is `GET`).
|
|
94
89
|
- **It is a function, not middleware.** Every handler has to call it; one handler
|
|
95
90
|
that forgets is one unguarded endpoint, silently. If you want it enforced
|
|
96
91
|
structurally, wrap your framework's dispatcher so a response cannot be produced
|
|
@@ -119,9 +114,11 @@ class Handler(LoopbackOnly, BaseHTTPRequestHandler):
|
|
|
119
114
|
|
|
120
115
|
## Status
|
|
121
116
|
|
|
122
|
-
0.1.
|
|
123
|
-
|
|
124
|
-
|
|
117
|
+
0.1.2 — on PyPI (`pip install localhost-guard`); 0.1.1 is yanked and 0.1.0 is
|
|
118
|
+
yanked as well. 0.1.1 silently ignored `guard(write=True)` on `GET`; 0.1.0
|
|
119
|
+
additionally carried the pre-release README. Trusted Publishing (OIDC) does the upload,
|
|
120
|
+
so no token lives in this repository. Tags (`v*`) build sdist + wheel and attach
|
|
121
|
+
them to the GitHub release.
|
|
125
122
|
|
|
126
123
|
## Tests
|
|
127
124
|
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: localhost-guard
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Header checks that stop a random web page from driving your loopback HTTP service
|
|
5
5
|
Author: suimajian
|
|
6
6
|
License: MIT
|
|
7
7
|
Project-URL: Homepage, https://github.com/suimajian/localhost-guard
|
|
8
|
+
Project-URL: Source, https://github.com/suimajian/localhost-guard
|
|
9
|
+
Project-URL: Issues, https://github.com/suimajian/localhost-guard/issues
|
|
8
10
|
Keywords: localhost,csrf,dns-rebinding,security,http
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Security
|
|
16
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
|
|
9
17
|
Requires-Python: >=3.9
|
|
10
18
|
Description-Content-Type: text/markdown
|
|
11
19
|
License-File: LICENSE
|
|
@@ -33,17 +41,10 @@ paste into an existing `BaseHTTPRequestHandler`.
|
|
|
33
41
|
|
|
34
42
|
## Install
|
|
35
43
|
|
|
36
|
-
Not on PyPI yet — `pip install localhost-guard` will 404 until 0.1.0 is
|
|
37
|
-
published. Install from the repository instead:
|
|
38
|
-
|
|
39
44
|
```bash
|
|
40
|
-
pip install
|
|
45
|
+
pip install localhost-guard
|
|
41
46
|
```
|
|
42
47
|
|
|
43
|
-
The sdist and wheel are attached to the
|
|
44
|
-
[releases](https://github.com/suimajian/localhost-guard/releases); the publish
|
|
45
|
-
step is waiting for credentials, so nothing has been uploaded to the index.
|
|
46
|
-
|
|
47
48
|
## Use
|
|
48
49
|
|
|
49
50
|
```python
|
|
@@ -103,7 +104,9 @@ class Handler(LoopbackOnly, BaseHTTPRequestHandler):
|
|
|
103
104
|
approval. Never put a state change behind GET (`/delete?id=`, `/sync`,
|
|
104
105
|
`/switch-profile`) — that endpoint is unprotected no matter what this library
|
|
105
106
|
does. Either keep every mutation on POST/PUT/PATCH/DELETE, or call
|
|
106
|
-
`guard(write=True)` in that handler so the write rules apply to it anyway
|
|
107
|
+
`guard(write=True)` in that handler so the write rules apply to it anyway
|
|
108
|
+
(that flag now reaches `check()`; the Host and Content-Type rules then apply
|
|
109
|
+
even though the method is `GET`).
|
|
107
110
|
- **It is a function, not middleware.** Every handler has to call it; one handler
|
|
108
111
|
that forgets is one unguarded endpoint, silently. If you want it enforced
|
|
109
112
|
structurally, wrap your framework's dispatcher so a response cannot be produced
|
|
@@ -132,9 +135,11 @@ class Handler(LoopbackOnly, BaseHTTPRequestHandler):
|
|
|
132
135
|
|
|
133
136
|
## Status
|
|
134
137
|
|
|
135
|
-
0.1.
|
|
136
|
-
|
|
137
|
-
|
|
138
|
+
0.1.2 — on PyPI (`pip install localhost-guard`); 0.1.1 is yanked and 0.1.0 is
|
|
139
|
+
yanked as well. 0.1.1 silently ignored `guard(write=True)` on `GET`; 0.1.0
|
|
140
|
+
additionally carried the pre-release README. Trusted Publishing (OIDC) does the upload,
|
|
141
|
+
so no token lives in this repository. Tags (`v*`) build sdist + wheel and attach
|
|
142
|
+
them to the GitHub release.
|
|
138
143
|
|
|
139
144
|
## Tests
|
|
140
145
|
|
|
@@ -25,6 +25,8 @@ browsers omit ``Sec-Fetch-*``, and ``Origin`` is missing from same-origin GETs.
|
|
|
25
25
|
|
|
26
26
|
from __future__ import annotations
|
|
27
27
|
|
|
28
|
+
__version__ = "0.1.2"
|
|
29
|
+
|
|
28
30
|
from typing import Iterable, Mapping, Optional
|
|
29
31
|
|
|
30
32
|
__all__ = ["Decision", "check", "LoopbackOnly"]
|
|
@@ -67,19 +69,24 @@ def check(method: str,
|
|
|
67
69
|
headers: Mapping[str, str],
|
|
68
70
|
*,
|
|
69
71
|
trusted_origins: Iterable[str] = (),
|
|
70
|
-
require_json: bool = True
|
|
72
|
+
require_json: bool = True,
|
|
73
|
+
write: Optional[bool] = None) -> Decision:
|
|
71
74
|
"""Decide whether a request may touch the service.
|
|
72
75
|
|
|
73
76
|
``headers`` is case-insensitive in practice; pass the request headers as a
|
|
74
77
|
plain mapping and the lookup below lower-cases both sides.
|
|
78
|
+
|
|
79
|
+
``write`` overrides the method-based guess: pass ``True`` from a GET handler
|
|
80
|
+
that mutates state, so the write rules (Host and Content-Type) apply to it.
|
|
75
81
|
"""
|
|
82
|
+
is_write = method.upper() in WRITE_METHODS if write is None else write
|
|
76
83
|
lowered = {k.lower(): v for k, v in headers.items()}
|
|
77
84
|
host = _host_port(lowered.get("host", ""))
|
|
78
85
|
if not host:
|
|
79
86
|
# No Host at all: HTTP/1.0, or something in the middle stripped it. A
|
|
80
87
|
# browser always sends one, so a write without it is not a browser we
|
|
81
88
|
# want to serve; a read is harmless and keeps old clients working.
|
|
82
|
-
if
|
|
89
|
+
if is_write:
|
|
83
90
|
return Decision(False, 403, "write without a Host header")
|
|
84
91
|
return _ALLOW
|
|
85
92
|
if host not in LOOPBACK_HOSTS:
|
|
@@ -100,7 +107,7 @@ def check(method: str,
|
|
|
100
107
|
if origin_host not in LOOPBACK_HOSTS:
|
|
101
108
|
return Decision(False, 403, f"untrusted origin: {origin!r}")
|
|
102
109
|
|
|
103
|
-
if require_json and
|
|
110
|
+
if require_json and is_write:
|
|
104
111
|
ctype = lowered.get("content-type", "").split(";", 1)[0].strip().lower()
|
|
105
112
|
if ctype != JSON_CONTENT_TYPE:
|
|
106
113
|
return Decision(False, 415, f"write requires application/json, got {ctype!r}")
|
|
@@ -131,7 +138,7 @@ class LoopbackOnly:
|
|
|
131
138
|
if write is None:
|
|
132
139
|
write = method.upper() in WRITE_METHODS
|
|
133
140
|
decision = check(method, self.headers, trusted_origins=self.trusted_origins,
|
|
134
|
-
require_json=
|
|
141
|
+
require_json=self.require_json, write=write)
|
|
135
142
|
if decision.allowed:
|
|
136
143
|
return True
|
|
137
144
|
self.send_response(decision.status)
|
|
@@ -4,16 +4,26 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "localhost-guard"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.2"
|
|
8
8
|
description = "Header checks that stop a random web page from driving your loopback HTTP service"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.9"
|
|
11
11
|
license = { text = "MIT" }
|
|
12
12
|
authors = [{ name = "suimajian" }]
|
|
13
13
|
keywords = ["localhost", "csrf", "dns-rebinding", "security", "http"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Topic :: Security",
|
|
20
|
+
"Topic :: Internet :: WWW/HTTP :: HTTP Servers",
|
|
21
|
+
]
|
|
14
22
|
|
|
15
23
|
[project.urls]
|
|
16
24
|
Homepage = "https://github.com/suimajian/localhost-guard"
|
|
25
|
+
Source = "https://github.com/suimajian/localhost-guard"
|
|
26
|
+
Issues = "https://github.com/suimajian/localhost-guard/issues"
|
|
17
27
|
|
|
18
28
|
[tool.setuptools]
|
|
19
29
|
py-modules = ["localhost_guard"]
|
|
@@ -162,3 +162,61 @@ def test_read_without_host_header_is_allowed():
|
|
|
162
162
|
def test_empty_host_string_is_not_loopback():
|
|
163
163
|
d = check("POST", {"host": "", "content-type": "application/json"})
|
|
164
164
|
assert not d and d.status == 403
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def test_write_flag_applies_write_rules_to_get():
|
|
168
|
+
"""README promises guard(write=True) makes a GET obey the write rules."""
|
|
169
|
+
denied = check("GET", {}, write=True)
|
|
170
|
+
assert not denied.allowed and denied.status == 403
|
|
171
|
+
assert check("GET", {}).allowed # a plain read stays allowed
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def test_write_flag_requires_json_on_get():
|
|
175
|
+
decision = check("GET", {"Host": "127.0.0.1:8080",
|
|
176
|
+
"Content-Type": "application/x-www-form-urlencoded"}, write=True)
|
|
177
|
+
assert not decision.allowed and decision.status == 415
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _guarded_get(headers, write):
|
|
181
|
+
class Request(LoopbackOnly):
|
|
182
|
+
command = "GET"
|
|
183
|
+
|
|
184
|
+
def __init__(self):
|
|
185
|
+
self.headers = headers
|
|
186
|
+
self.sent = None
|
|
187
|
+
|
|
188
|
+
def send_response(self, code, message=None):
|
|
189
|
+
self.sent = code
|
|
190
|
+
|
|
191
|
+
def send_header(self, *args):
|
|
192
|
+
pass
|
|
193
|
+
|
|
194
|
+
def end_headers(self):
|
|
195
|
+
pass
|
|
196
|
+
|
|
197
|
+
@property
|
|
198
|
+
def wfile(self):
|
|
199
|
+
import io
|
|
200
|
+
return io.BytesIO()
|
|
201
|
+
|
|
202
|
+
request = Request()
|
|
203
|
+
return request.guard(write=write), request.sent
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def test_guard_write_flag_reaches_the_handler():
|
|
207
|
+
allowed, status = _guarded_get({}, write=True)
|
|
208
|
+
assert (allowed, status) == (False, 403)
|
|
209
|
+
allowed, status = _guarded_get({}, write=False)
|
|
210
|
+
assert (allowed, status) == (True, None)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def test_version_matches_pyproject():
|
|
214
|
+
import pathlib
|
|
215
|
+
import re
|
|
216
|
+
|
|
217
|
+
import localhost_guard
|
|
218
|
+
|
|
219
|
+
repo = pathlib.Path(__file__).resolve().parents[1]
|
|
220
|
+
declared = re.search(r'^version = "([^"]+)"', (repo / "pyproject.toml").read_text(encoding="utf-8"), re.M)
|
|
221
|
+
assert declared, "version missing from pyproject.toml"
|
|
222
|
+
assert declared.group(1) == localhost_guard.__version__
|
|
File without changes
|
|
File without changes
|
{localhost_guard-0.1.0 → localhost_guard-0.1.2}/localhost_guard.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|