Repository navigation
Expand file tree
/
Copy pathsecurity.py
More file actions
233 lines (202 loc) · 8.25 KB
/
Copy pathsecurity.py
File metadata and controls
233 lines (202 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""HTTP/websocket admission and private native credential storage."""
from __future__ import annotations
import asyncio
import json
from ipaddress import IPv4Address, IPv6Address
import os
from pathlib import Path
import stat
import tempfile
import re
from urllib.parse import urlsplit, urlunsplit
from starlette.responses import JSONResponse
from starlette.websockets import WebSocketDisconnect
from sqlrooms.server.access import AccessDenied, LocalAccess
NO_STORE = {"Cache-Control": "no-store", "Referrer-Policy": "no-referrer"}
def normalize_transport_url(url: str) -> str:
"""Normalize default ports, requiring unambiguous ASCII/IP authorities."""
try:
parsed = urlsplit(url)
defaults = {"http": 80, "https": 443, "ws": 80, "wss": 443}
if (
parsed.scheme not in defaults
or not parsed.hostname
or parsed.username is not None
or parsed.password is not None
or parsed.query
or parsed.fragment
):
raise ValueError
host = parsed.hostname
if not host.isascii():
raise ValueError
if ":" in host:
host = f"[{IPv6Address(host).compressed}]"
else:
last_label = host.rstrip(".").rsplit(".", 1)[-1]
if last_label.isdecimal() or re.fullmatch(r"0x[0-9a-f]+", last_label):
if str(IPv4Address(host)) != host:
raise ValueError
port = parsed.port
authority = (
f"{host}:{port}"
if port is not None and port != defaults[parsed.scheme]
else host
)
return urlunsplit((parsed.scheme, authority, parsed.path, "", ""))
except ValueError:
raise ValueError(
"Invalid transport URL; use an HTTP or WebSocket URL with an ASCII "
"hostname (punycode for international names) or canonical IP address, "
"without credentials, query, or fragment."
) from None
def bearer(headers) -> str:
value = headers.get("authorization", "")
return (
value[7:]
if value.lower().startswith("bearer ")
else headers.get("x-sqlrooms-token", "")
)
def check_private(path: Path, *, directory: bool = False) -> None:
"""Reject symlinks, foreign owners, and group/world-accessible POSIX files."""
if os.name != "posix":
raise RuntimeError(
"Private credential files require tested POSIX permissions; Windows ACL support is not yet available."
)
info = path.lstat()
correct_type = (
stat.S_ISDIR(info.st_mode) if directory else stat.S_ISREG(info.st_mode)
)
if (
not correct_type
or info.st_uid != os.getuid()
or stat.S_IMODE(info.st_mode) & 0o077
):
raise RuntimeError(
"SQLRooms credential storage must be owner-only and must not be a symlink."
)
def supports_private_credentials() -> bool:
"""Whether this platform has a tested owner-only native credential store."""
return os.name == "posix"
class CredentialFile:
"""Temporary owner-only native handoff; the environment contains only its path."""
def __init__(self, access: LocalAccess, api_url: str, mcp_url: str, ws_url: str):
if not supports_private_credentials():
raise RuntimeError("Windows credential ACL support is not yet verified.")
self.directory = Path(tempfile.mkdtemp(prefix="sqlrooms-credentials-"))
check_private(self.directory, directory=True)
self.path = self.directory / "credential.json"
fd = os.open(self.path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(fd, "w") as stream:
json.dump(
{
"token": access.native_token,
"binding": access.binding,
"apiUrl": api_url,
"mcpUrl": mcp_url,
"wsUrl": ws_url,
},
stream,
)
check_private(self.path)
def close(self):
self.path.unlink(missing_ok=True)
self.directory.rmdir()
def read_credential_file(path: Path) -> dict:
"""Read a private handoff without following a replaceable file symlink."""
check_private(path.parent, directory=True)
fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
with os.fdopen(fd) as stream:
info = os.fstat(stream.fileno())
if (
not stat.S_ISREG(info.st_mode)
or info.st_uid != os.getuid()
or stat.S_IMODE(info.st_mode) & 0o077
):
raise RuntimeError("Unsafe SQLRooms credential file permissions.")
return json.load(stream)
class TransportSecurity:
def __init__(self, access: LocalAccess, origins: set[str], hosts: set[str]):
self.access = access
self.origins = origins
self.hosts = hosts
def check_headers(self, headers):
if headers.get("host", "") not in self.hosts:
raise AccessDenied("forbidden")
origin = headers.get("origin")
if origin is not None and origin not in self.origins:
raise AccessDenied("forbidden")
def authorize(self, headers, operation: str):
self.check_headers(headers)
return self.access.verify(bearer(headers), operation)
async def authenticate_socket(
self, websocket, operation: str, *, message_type: str = "auth"
) -> tuple[str, dict] | None:
try:
self.check_headers(websocket.headers)
except AccessDenied:
await websocket.close(code=1008)
return None
await websocket.accept()
try:
raw = await asyncio.wait_for(websocket.receive_text(), 5)
if len(raw.encode()) > 4096:
raise AccessDenied()
message = json.loads(raw)
if not isinstance(message, dict) or message.get("type") != message_type:
raise AccessDenied()
token = message.get("token")
if not isinstance(token, str):
raise AccessDenied()
self.access.verify(token, operation)
return token, message
except WebSocketDisconnect:
return None
except Exception:
await websocket.close(code=1008, reason="authentication required")
return None
async def watch_socket(self, websocket, token: str, operation: str):
"""Bound expiry/revocation on idle sockets to one second."""
while True:
await asyncio.sleep(1)
try:
self.access.verify(token, operation)
except AccessDenied:
try:
await websocket.close(code=1008, reason="authorization expired")
except (RuntimeError, WebSocketDisconnect):
pass # A concurrent clean disconnect already closed it.
return
class McpAuthorization:
"""Authorize every HTTP request before the MCP SDK; preserve SDK DNS checks."""
def __init__(self, app, security: TransportSecurity):
self.app = app
self.security = security
async def __call__(self, scope, receive, send):
if scope["type"] == "http":
from starlette.datastructures import Headers
try:
caller = self.security.authorize(Headers(scope=scope), "mcp")
scope.setdefault("state", {})["sqlrooms_caller"] = caller
except AccessDenied as exc:
await JSONResponse(
{"error": exc.code},
status_code=401 if exc.code == "unauthorized" else 403,
headers=NO_STORE,
)(scope, receive, send)
return
async def no_store(message):
if message["type"] == "http.response.start":
message["headers"] = [
(key, value)
for key, value in message.get("headers", [])
if key.lower() not in {b"cache-control", b"referrer-policy"}
]
message["headers"].extend(
[
(b"cache-control", b"no-store"),
(b"referrer-policy", b"no-referrer"),
]
)
await send(message)
await self.app(scope, receive, no_store)