sweatstack-cli 0.15.0__py3-none-any.whl → 0.16.0__py3-none-any.whl

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,153 @@
1
+ """Tool commands: serve a tool locally under the SweatStack Tools policy, and check it.
2
+
3
+ `tool create`, `deploy`, `publish` and `rollback` follow when the SweatStack app's tool API exists.
4
+ See plans/developer-kit/cli.md in sweatstack-tools.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import socket
10
+ from pathlib import Path
11
+
12
+ import typer
13
+
14
+ from sweatstack_cli.console import console
15
+ from sweatstack_cli.tools.check import REMOVED_KINDS, Finding, check_folder
16
+ from sweatstack_cli.tools.policy import Policy, load_policy
17
+ from sweatstack_cli.tools.server import make_server
18
+
19
+ DOCS_DEPENDENCIES = "https://docs.sweatstack.no/learn/tools/dependencies/"
20
+
21
+ app = typer.Typer(
22
+ name="tool",
23
+ help="Build tools for SweatStack Tools: serve and check them locally.",
24
+ no_args_is_help=True,
25
+ )
26
+
27
+
28
+ def _directory(directory: Path) -> Path:
29
+ if not directory.is_dir():
30
+ console.print(f"[red]Error:[/red] '{directory}' is not a directory.")
31
+ raise typer.Exit(1)
32
+ return directory.resolve()
33
+
34
+
35
+ def _policy() -> Policy:
36
+ policy = load_policy()
37
+ if policy.source == "cached":
38
+ console.print(
39
+ "[yellow]sweatstack.tools is unreachable: using the policy from the last run.[/yellow]"
40
+ )
41
+ elif policy.source == "built-in":
42
+ console.print(
43
+ "[yellow]sweatstack.tools is unreachable: using the policy this CLI shipped with.[/yellow]"
44
+ )
45
+ return policy
46
+
47
+
48
+ def _free_port(start: int, attempts: int = 100) -> int:
49
+ for port in range(start, start + attempts):
50
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as probe:
51
+ try:
52
+ probe.bind(("localhost", port))
53
+ return port
54
+ except OSError:
55
+ continue
56
+ console.print(f"[red]Error:[/red] No free port from {start} to {start + attempts - 1}.")
57
+ raise typer.Exit(1)
58
+
59
+
60
+ @app.command()
61
+ def serve(
62
+ directory: Path = typer.Argument(Path("."), help="The tool's folder."),
63
+ port: int | None = typer.Option(
64
+ None, "--port", "-p", help="Port to serve on. The first free one from 8000 by default."
65
+ ),
66
+ slug: str = typer.Option("local", "--slug", help="The tool's slug, for its report link."),
67
+ ) -> None:
68
+ """Serve a tool locally, with the same network policy as SweatStack Tools."""
69
+ root = _directory(directory)
70
+ policy = _policy()
71
+ chosen = port if port is not None else _free_port(8000)
72
+ try:
73
+ server = make_server(root, policy, slug=slug, port=chosen)
74
+ except OSError as error:
75
+ console.print(f"[red]Error:[/red] Cannot serve on port {chosen}: {error.strerror}.")
76
+ raise typer.Exit(1)
77
+ console.print(
78
+ "Serving with the SweatStack Tools network policy: requests to other hosts are blocked, "
79
+ "as they will be when deployed. Blocked requests show in the browser console."
80
+ )
81
+ console.print(f" [cyan]http://localhost:{chosen}[/cyan] ({root})")
82
+ try:
83
+ server.serve_forever()
84
+ except KeyboardInterrupt:
85
+ pass
86
+ finally:
87
+ server.server_close()
88
+
89
+
90
+ def say(text: str = "", *, highlight: bool = True) -> None:
91
+ """Print one line without wrapping."""
92
+ console.print(text, soft_wrap=True, highlight=highlight)
93
+
94
+
95
+ def print_findings(findings: list[Finding], directory: Path) -> None:
96
+ """The check's report, written for a person and for an agent: what, where, and the fix.
97
+
98
+ Lines never wrap, so each finding stays on one line with its whole URL, at any terminal width.
99
+ """
100
+ blocked = [f for f in findings if f.kind not in REMOVED_KINDS]
101
+ removed = [f for f in findings if f.kind in REMOVED_KINDS]
102
+ if blocked:
103
+ files = len({f.file for f in blocked})
104
+ say(
105
+ f"[red]✗[/red] {len(blocked)} load{'s' if len(blocked) != 1 else ''} in {files} "
106
+ f"file{'s' if files != 1 else ''} would be blocked on SweatStack Tools"
107
+ )
108
+ say()
109
+ width = max(len(f"{f.file}:{f.line}") for f in blocked)
110
+ kind_width = max(len(f.kind) for f in blocked)
111
+ for f in blocked:
112
+ say(
113
+ f" {f'{f.file}:{f.line}':<{width}} {f.kind:<{kind_width}} {f.url}",
114
+ highlight=False,
115
+ )
116
+ say()
117
+ say(" Tools can load only their own files and the SweatStack API.")
118
+ say(
119
+ " Download these files into your tool (for example into vendor/) and use relative paths."
120
+ )
121
+ say(f" How, including DuckDB and Pyodide: {DOCS_DEPENDENCIES}")
122
+ if removed:
123
+ if blocked:
124
+ say()
125
+ say(
126
+ f"[yellow]![/yellow] {len(removed)} element{'s' if len(removed) != 1 else ''} SweatStack Tools removes, so they do nothing there:"
127
+ )
128
+ say()
129
+ width = max(len(f"{f.file}:{f.line}") for f in removed)
130
+ for f in removed:
131
+ say(f" {f'{f.file}:{f.line}':<{width}} {f.kind} {f.url}", highlight=False)
132
+ say()
133
+ say(
134
+ " Speculation rules, preconnect and DNS prefetch are not available in SweatStack Tools."
135
+ )
136
+ if not findings:
137
+ say(f"[green]✓[/green] Nothing in {directory} loads from another site.")
138
+
139
+
140
+ @app.command()
141
+ def check(
142
+ directory: Path = typer.Argument(Path("."), help="The tool's folder."),
143
+ ) -> None:
144
+ """Check a tool for files it would load from other sites, which SweatStack Tools blocks.
145
+
146
+ Exits with 1 when anything would be blocked, so an agent's loop treats it as a failure to fix.
147
+ """
148
+ root = _directory(directory)
149
+ policy = _policy()
150
+ findings = check_folder(root, policy.load_origins())
151
+ print_findings(findings, directory)
152
+ if any(f.kind not in REMOVED_KINDS for f in findings):
153
+ raise typer.Exit(1)
sweatstack_cli/config.py CHANGED
@@ -7,6 +7,7 @@ from dataclasses import dataclass
7
7
  from functools import cache
8
8
 
9
9
  DEFAULT_API_URL = "https://app.sweatstack.no"
10
+ DEFAULT_TOOLS_URL = "https://sweatstack.tools"
10
11
 
11
12
 
12
13
  @dataclass(frozen=True, slots=True)
@@ -14,6 +15,7 @@ class Settings:
14
15
  """Immutable application settings."""
15
16
 
16
17
  api_url: str
18
+ tools_url: str
17
19
  version: str
18
20
 
19
21
 
@@ -28,5 +30,6 @@ def get_settings() -> Settings:
28
30
 
29
31
  return Settings(
30
32
  api_url=os.environ.get("SWEATSTACK_URL", DEFAULT_API_URL).rstrip("/"),
33
+ tools_url=os.environ.get("SWEATSTACK_TOOLS_URL", DEFAULT_TOOLS_URL).rstrip("/"),
31
34
  version=__version__,
32
35
  )
sweatstack_cli/main.py CHANGED
@@ -6,7 +6,7 @@ import typer
6
6
 
7
7
  from sweatstack_cli import __version__
8
8
  from sweatstack_cli.commands import app as app_commands
9
- from sweatstack_cli.commands import auth, page
9
+ from sweatstack_cli.commands import auth, page, tool
10
10
  from sweatstack_cli.console import console
11
11
  from sweatstack_cli.exceptions import CLIError
12
12
  from sweatstack_cli.version_check import get_update_message
@@ -23,6 +23,7 @@ app = typer.Typer(
23
23
  # Register command groups
24
24
  app.add_typer(page.app, name="page")
25
25
  app.add_typer(app_commands.app, name="app")
26
+ app.add_typer(tool.app, name="tool")
26
27
 
27
28
  # Register top-level auth commands for convenience
28
29
  # These are the most common operations, so we promote them to top level
@@ -0,0 +1 @@
1
+ """SweatStack Tools: serving a tool locally under the production policy, and checking it."""
@@ -0,0 +1,184 @@
1
+ """The deploy check: what in a tool's folder would load from another origin, and be blocked.
2
+
3
+ Only patterns that cannot be anything but a load are reported, so the check stays free of noise:
4
+ links, licence comments and XML namespaces are never findings. What it cannot see, URLs built at
5
+ runtime, `tool serve` shows in the browser console. See plans/developer-kit/cli.md#the-deploy-check
6
+ in sweatstack-tools.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import re
12
+ from collections.abc import Iterable, Iterator
13
+ from dataclasses import dataclass
14
+ from html.parser import HTMLParser
15
+ from pathlib import Path
16
+
17
+ from sweatstack_cli.tools.rewrite import (
18
+ SPECULATIVE_HINTS,
19
+ is_speculation_rules,
20
+ is_speculative_hint,
21
+ )
22
+
23
+ HTML_SUFFIXES = frozenset({".html", ".htm"})
24
+ JS_SUFFIXES = frozenset({".js", ".mjs"})
25
+ CSS_SUFFIXES = frozenset({".css"})
26
+
27
+ LINK_LOADS = frozenset({"stylesheet", "preload", "modulepreload", "icon", "manifest"})
28
+ # Element, attribute: each loads what it names.
29
+ SOURCE_ATTRIBUTES = {
30
+ "img": ("src", "srcset"),
31
+ "source": ("src", "srcset"),
32
+ "video": ("src", "poster"),
33
+ "audio": ("src",),
34
+ "track": ("src",),
35
+ "iframe": ("src",),
36
+ "embed": ("src",),
37
+ "object": ("data",),
38
+ "input": ("src",),
39
+ }
40
+
41
+ _URL = r"""(?P<url>(?:https?:)?//[^\s"'`)]+)"""
42
+ JS_PATTERNS = (
43
+ ("import", re.compile(r"""\b(?:import|export)\b[^;'"`]*?\bfrom\s*(["'`])""" + _URL + r"\1")),
44
+ ("import", re.compile(r"""\bimport\s*(["'`])""" + _URL + r"\1")),
45
+ ("import", re.compile(r"""\bimport\s*\(\s*(["'`])""" + _URL + r"\1")),
46
+ ("importScripts", re.compile(r"""\bimportScripts\s*\(\s*(["'`])""" + _URL + r"\1")),
47
+ ("worker", re.compile(r"""\bnew\s+(?:Shared)?Worker\s*\(\s*(["'`])""" + _URL + r"\1")),
48
+ )
49
+ CSS_PATTERNS = (
50
+ ("@import", re.compile(r"""@import\s+(["'])""" + _URL + r"\1")),
51
+ ("url()", re.compile(r"""\burl\(\s*(["']?)""" + _URL + r"""\1\s*\)""")),
52
+ )
53
+
54
+
55
+ @dataclass(frozen=True, slots=True)
56
+ class Finding:
57
+ """One load from another origin, or one thing SweatStack Tools removes."""
58
+
59
+ file: str
60
+ line: int
61
+ kind: str
62
+ url: str
63
+
64
+
65
+ def is_external(url: str, allowed_origins: Iterable[str]) -> bool:
66
+ """Whether loading `url` would leave the tool's own origin and the allowed ones."""
67
+ url = url.strip()
68
+ lowered = url.lower()
69
+ if not (
70
+ lowered.startswith("http://") or lowered.startswith("https://") or lowered.startswith("//")
71
+ ):
72
+ return False # Relative, data:, blob: and the like stay on the tool's own origin.
73
+ return not any(
74
+ lowered == o.lower() or lowered.startswith(o.lower() + "/") for o in allowed_origins
75
+ )
76
+
77
+
78
+ def _lines_before(text: str, index: int) -> int:
79
+ return text.count("\n", 0, index)
80
+
81
+
82
+ def _scan_text(
83
+ text: str, patterns: Iterable[tuple[str, re.Pattern[str]]], file: str, first_line: int
84
+ ) -> Iterator[Finding]:
85
+ for kind, pattern in patterns:
86
+ for match in pattern.finditer(text):
87
+ yield Finding(
88
+ file, first_line + _lines_before(text, match.start()), kind, match.group("url")
89
+ )
90
+
91
+
92
+ class _HTMLScanner(HTMLParser):
93
+ def __init__(self, file: str) -> None:
94
+ super().__init__(convert_charrefs=True)
95
+ self.file = file
96
+ self.findings: list[Finding] = []
97
+ self._inline: str | None = None # "script" or "style", while inside one
98
+
99
+ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
100
+ line = self.getpos()[0]
101
+ values = {name: value or "" for name, value in attrs}
102
+ found: list[tuple[str, str]] = []
103
+ if tag == "script":
104
+ if is_speculation_rules(values.get("type")):
105
+ found.append(("speculation rules", "removed by SweatStack Tools"))
106
+ self._inline = None
107
+ elif "src" in values:
108
+ found.append(("script", values["src"]))
109
+ elif values.get("type", "").strip().lower() in (
110
+ "",
111
+ "module",
112
+ "text/javascript",
113
+ "application/javascript",
114
+ ):
115
+ self._inline = "script"
116
+ elif tag == "style":
117
+ self._inline = "style"
118
+ elif tag == "link":
119
+ rel = values.get("rel")
120
+ if is_speculative_hint(rel):
121
+ rel_tokens = (rel or "").lower().split()
122
+ hint = next(token for token in rel_tokens if token in SPECULATIVE_HINTS)
123
+ found.append((hint, values.get("href", "")))
124
+ else:
125
+ tokens = set((rel or "").lower().split())
126
+ kinds = sorted(tokens & LINK_LOADS)
127
+ if kinds and "href" in values:
128
+ found.append((kinds[0], values["href"]))
129
+ for attribute in SOURCE_ATTRIBUTES.get(tag, ()):
130
+ if attribute in values:
131
+ urls = (
132
+ [
133
+ part.strip().split()[0]
134
+ for part in values[attribute].split(",")
135
+ if part.strip()
136
+ ]
137
+ if attribute == "srcset"
138
+ else [values[attribute]]
139
+ )
140
+ found.extend((tag, url) for url in urls)
141
+ if "style" in values:
142
+ self.findings.extend(_scan_text(values["style"], CSS_PATTERNS, self.file, line))
143
+ for kind, url in found:
144
+ self.findings.append(Finding(self.file, line, kind, url))
145
+
146
+ def handle_endtag(self, tag: str) -> None:
147
+ if tag in ("script", "style"):
148
+ self._inline = None
149
+
150
+ def handle_data(self, data: str) -> None:
151
+ if self._inline is None:
152
+ return
153
+ patterns = JS_PATTERNS if self._inline == "script" else CSS_PATTERNS
154
+ self.findings.extend(_scan_text(data, patterns, self.file, self.getpos()[0]))
155
+
156
+
157
+ def _scan_file(path: Path, name: str) -> list[Finding]:
158
+ suffix = path.suffix.lower()
159
+ if suffix not in HTML_SUFFIXES | JS_SUFFIXES | CSS_SUFFIXES:
160
+ return []
161
+ text = path.read_bytes().decode("utf-8", errors="replace")
162
+ if suffix in JS_SUFFIXES:
163
+ return list(_scan_text(text, JS_PATTERNS, name, 1))
164
+ if suffix in CSS_SUFFIXES:
165
+ return list(_scan_text(text, CSS_PATTERNS, name, 1))
166
+ scanner = _HTMLScanner(name)
167
+ scanner.feed(text)
168
+ scanner.close()
169
+ return scanner.findings
170
+
171
+
172
+ REMOVED_KINDS = frozenset({"speculation rules", *SPECULATIVE_HINTS})
173
+
174
+
175
+ def check_folder(root: Path, allowed_origins: Iterable[str]) -> list[Finding]:
176
+ """Every load from another origin in the folder, and every element SweatStack Tools removes."""
177
+ allowed = list(allowed_origins)
178
+ findings: list[Finding] = []
179
+ for path in sorted(p for p in root.rglob("*") if p.is_file()):
180
+ name = path.relative_to(root).as_posix()
181
+ for finding in _scan_file(path, name):
182
+ if finding.kind in REMOVED_KINDS or is_external(finding.url, allowed):
183
+ findings.append(finding)
184
+ return sorted(set(findings), key=lambda f: (f.file, f.line, f.kind, f.url))
@@ -0,0 +1,55 @@
1
+ {
2
+ "rule": "The extension is the text after the last '.' in the last path segment, lowercased. A segment with no '.' after its first character has no extension. Anything not listed gets the default.",
3
+ "default": "application/octet-stream",
4
+ "byExtension": {
5
+ "html": "text/html; charset=utf-8",
6
+ "htm": "text/html; charset=utf-8",
7
+ "js": "text/javascript; charset=utf-8",
8
+ "mjs": "text/javascript; charset=utf-8",
9
+ "css": "text/css; charset=utf-8",
10
+ "json": "application/json",
11
+ "map": "application/json",
12
+ "webmanifest": "application/manifest+json",
13
+ "wasm": "application/wasm",
14
+ "txt": "text/plain; charset=utf-8",
15
+ "csv": "text/csv; charset=utf-8",
16
+ "md": "text/markdown; charset=utf-8",
17
+ "xml": "application/xml",
18
+ "svg": "image/svg+xml",
19
+ "png": "image/png",
20
+ "jpg": "image/jpeg",
21
+ "jpeg": "image/jpeg",
22
+ "gif": "image/gif",
23
+ "webp": "image/webp",
24
+ "avif": "image/avif",
25
+ "ico": "image/x-icon",
26
+ "woff2": "font/woff2",
27
+ "woff": "font/woff",
28
+ "ttf": "font/ttf",
29
+ "otf": "font/otf",
30
+ "pdf": "application/pdf",
31
+ "mp4": "video/mp4",
32
+ "webm": "video/webm",
33
+ "mp3": "audio/mpeg",
34
+ "wav": "audio/wav",
35
+ "parquet": "application/vnd.apache.parquet",
36
+ "arrow": "application/vnd.apache.arrow.file",
37
+ "zip": "application/zip",
38
+ "gz": "application/gzip"
39
+ },
40
+ "examples": {
41
+ "index.html": "text/html; charset=utf-8",
42
+ "docs/Guide.HTM": "text/html; charset=utf-8",
43
+ "vendor/duckdb-browser.mjs": "text/javascript; charset=utf-8",
44
+ "vendor/duckdb-eh.wasm": "application/wasm",
45
+ "data/athletes.tar.gz": "application/gzip",
46
+ "app.js.map": "application/json",
47
+ "pyodide/numpy-2.0.2-cp312-cp312-pyodide_2024_0_wasm32.whl": "application/octet-stream",
48
+ "pyodide/python_stdlib.data": "application/octet-stream",
49
+ ".htaccess": "application/octet-stream",
50
+ "_headers": "application/octet-stream",
51
+ "Makefile": "application/octet-stream",
52
+ "v1.2/README": "application/octet-stream",
53
+ "file.": "application/octet-stream"
54
+ }
55
+ }
@@ -0,0 +1,25 @@
1
+ {
2
+ "version": 1,
3
+ "headers": {
4
+ "Content-Security-Policy": "default-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' 'wasm-unsafe-eval' blob:; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; media-src 'self' data: blob:; connect-src 'self' data: blob: https://app.sweatstack.no http://localhost:* http://127.0.0.1:*; worker-src 'self' blob:; manifest-src 'self'; frame-src 'self' blob:; frame-ancestors 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; report-uri https://sweatstack.tools/v1/csp-report; report-to csp",
5
+ "Reporting-Endpoints": "csp=\"https://sweatstack.tools/v1/csp-report\"",
6
+ "Permissions-Policy": "bluetooth=(self), usb=(self), serial=(), hid=(), payment=(), publickey-credentials-get=(), publickey-credentials-create=(), identity-credentials-get=()",
7
+ "Cross-Origin-Resource-Policy": "same-origin",
8
+ "Cross-Origin-Opener-Policy": "same-origin",
9
+ "X-Content-Type-Options": "nosniff",
10
+ "Referrer-Policy": "no-referrer",
11
+ "X-DNS-Prefetch-Control": "off",
12
+ "Strict-Transport-Security": "max-age=63072000; includeSubDomains; preload"
13
+ },
14
+ "allowedOrigins": [
15
+ "https://app.sweatstack.no"
16
+ ],
17
+ "loopbackSources": [
18
+ "http://localhost:*",
19
+ "http://127.0.0.1:*"
20
+ ],
21
+ "inject": {
22
+ "head": "<script>\n(() => {\n const send = fetch.bind(globalThis);\n const blocked = function () {\n try {\n send(\"https://app.sweatstack.no/api/v1/tools/webrtc-blocked\", { method: \"POST\", mode: \"cors\", credentials: \"omit\", keepalive: true }).catch(() => {});\n } catch {}\n throw new DOMException(\"WebRTC is not available in SweatStack Tools\", \"NotSupportedError\");\n };\n for (const name of [\"RTCPeerConnection\", \"webkitRTCPeerConnection\"]) {\n Object.defineProperty(globalThis, name, { value: blocked, writable: false, configurable: false });\n }\n})();\n</script>",
23
+ "body": "<style>@media print { a[data-sweatstack-report] { display: none !important; } }</style><a data-sweatstack-report href=\"https://app.sweatstack.no/tools/{slug}/report\" target=\"_blank\" rel=\"noopener noreferrer\" style=\"all: initial; position: fixed; right: 12px; bottom: 0; z-index: 2147483647; padding: 3px 8px; font: 12px/1.4 system-ui, sans-serif; color: #444; background: #fff; border: 1px solid #ccc; border-bottom: 0; border-radius: 4px 4px 0 0; cursor: pointer\">Report</a>"
24
+ }
25
+ }
@@ -0,0 +1,155 @@
1
+ """The SweatStack Tools network policy, as https://sweatstack.tools/v1/policy serves it.
2
+
3
+ The CLI never carries its own version of the policy: it fetches the live one, falls back to the
4
+ copy cached from the last successful fetch, and only then to the copy built into this release.
5
+ So when the policy changes, every CLI picks it up without a release. The shape is in the Tools
6
+ app's contract (plans/shared/contract.md#network-policy in sweatstack-tools).
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import json
12
+ from dataclasses import dataclass
13
+ from importlib import resources
14
+ from pathlib import Path
15
+ from typing import Any, Literal, cast
16
+
17
+ import httpx
18
+ import platformdirs
19
+
20
+ from sweatstack_cli.config import get_settings
21
+
22
+ FETCH_TIMEOUT_SECONDS = 1.0
23
+ SUPPORTED_VERSION = 1
24
+
25
+ # Headers `tool serve` leaves out: nothing should report from a laptop, and localhost is not HTTPS.
26
+ LOCAL_OMITTED_HEADERS = frozenset({"reporting-endpoints", "strict-transport-security"})
27
+ LOCAL_OMITTED_DIRECTIVES = frozenset({"report-uri", "report-to"})
28
+
29
+ Source = Literal["live", "cached", "built-in"]
30
+
31
+ # The directives that govern loads: everything the deploy check looks for. (`connect-src` governs
32
+ # `fetch`, which the check does not scan, and is where the SweatStack API is allowed.)
33
+ LOAD_DIRECTIVES = (
34
+ "default-src",
35
+ "script-src",
36
+ "style-src",
37
+ "img-src",
38
+ "font-src",
39
+ "media-src",
40
+ "worker-src",
41
+ "manifest-src",
42
+ "frame-src",
43
+ )
44
+
45
+
46
+ @dataclass(frozen=True, slots=True)
47
+ class Policy:
48
+ """The network policy every tool is served with."""
49
+
50
+ headers: dict[str, str]
51
+ allowed_origins: tuple[str, ...]
52
+ loopback_sources: tuple[str, ...]
53
+ inject_head: str
54
+ inject_body: str
55
+ source: Source
56
+
57
+ def local_headers(self) -> dict[str, str]:
58
+ """The headers `tool serve` sends: production's, without reporting and HSTS."""
59
+ headers = {
60
+ name: value
61
+ for name, value in self.headers.items()
62
+ if name.lower() not in LOCAL_OMITTED_HEADERS
63
+ }
64
+ for name, value in headers.items():
65
+ if name.lower() == "content-security-policy":
66
+ directives = [d.strip() for d in value.split(";") if d.strip()]
67
+ headers[name] = "; ".join(
68
+ d for d in directives if d.split()[0].lower() not in LOCAL_OMITTED_DIRECTIVES
69
+ )
70
+ return headers
71
+
72
+ def load_origins(self) -> tuple[str, ...]:
73
+ """Other origins the policy lets a tool load from: none today. From its load directives,
74
+ so the deploy check and the policy cannot drift."""
75
+ csp = next(v for k, v in self.headers.items() if k.lower() == "content-security-policy")
76
+ origins: list[str] = []
77
+ for directive in csp.split(";"):
78
+ name, *sources = directive.split()
79
+ if name.lower() not in LOAD_DIRECTIVES:
80
+ continue
81
+ for source in sources:
82
+ if "://" in source and "*" not in source and source not in origins:
83
+ origins.append(source.rstrip("/"))
84
+ return tuple(origins)
85
+
86
+ def report_link(self, slug: str) -> str:
87
+ """The injected report link, for a tool's slug."""
88
+ return self.inject_body.replace("{slug}", slug)
89
+
90
+
91
+ class PolicyFormatError(ValueError):
92
+ """The policy did not have the shape the contract describes."""
93
+
94
+
95
+ def parse_policy(data: Any, source: Source) -> Policy:
96
+ """Check a policy document's shape. Anything unexpected is refused, never guessed at."""
97
+ if not isinstance(data, dict) or data.get("version") != SUPPORTED_VERSION:
98
+ raise PolicyFormatError("not a version 1 policy")
99
+ headers = data.get("headers")
100
+ inject = data.get("inject")
101
+ allowed = data.get("allowedOrigins")
102
+ loopback = data.get("loopbackSources")
103
+ if not isinstance(headers, dict) or not all(
104
+ isinstance(k, str) and isinstance(v, str) for k, v in headers.items()
105
+ ):
106
+ raise PolicyFormatError("headers must map names to values")
107
+ if not any(name.lower() == "content-security-policy" for name in headers):
108
+ raise PolicyFormatError("no Content-Security-Policy")
109
+ if not isinstance(inject, dict) or not all(
110
+ isinstance(inject.get(part), str) for part in ("head", "body")
111
+ ):
112
+ raise PolicyFormatError("inject must have head and body markup")
113
+ for name, value in (("allowedOrigins", allowed), ("loopbackSources", loopback)):
114
+ if not isinstance(value, list) or not all(isinstance(v, str) for v in value):
115
+ raise PolicyFormatError(f"{name} must be a list of strings")
116
+ return Policy(
117
+ headers=dict(headers),
118
+ allowed_origins=tuple(cast(list[str], allowed)),
119
+ loopback_sources=tuple(cast(list[str], loopback)),
120
+ inject_head=inject["head"],
121
+ inject_body=inject["body"],
122
+ source=source,
123
+ )
124
+
125
+
126
+ def cache_path() -> Path:
127
+ return Path(platformdirs.user_cache_dir("sweatstack")) / "tools-policy.json"
128
+
129
+
130
+ def load_policy(*, cache: Path | None = None) -> Policy:
131
+ """The live policy, else the cached one, else the one built into this release."""
132
+ cache = cache or cache_path()
133
+ url = f"{get_settings().tools_url}/v1/policy"
134
+ try:
135
+ response = httpx.get(url, timeout=FETCH_TIMEOUT_SECONDS)
136
+ response.raise_for_status()
137
+ data = response.json()
138
+ policy = parse_policy(data, "live")
139
+ except (httpx.HTTPError, ValueError):
140
+ pass
141
+ else:
142
+ try:
143
+ cache.parent.mkdir(parents=True, exist_ok=True)
144
+ cache.write_text(json.dumps(data), encoding="utf-8")
145
+ except OSError:
146
+ pass # A cache that cannot be written only costs the next offline run.
147
+ return policy
148
+
149
+ try:
150
+ return parse_policy(json.loads(cache.read_text(encoding="utf-8")), "cached")
151
+ except (OSError, ValueError):
152
+ pass
153
+
154
+ built_in = resources.files("sweatstack_cli.tools").joinpath("policy.json").read_text("utf-8")
155
+ return parse_policy(json.loads(built_in), "built-in")
@@ -0,0 +1,111 @@
1
+ """Rewrites a tool's HTML as the Tools app does, so `tool serve` shows the page production serves.
2
+
3
+ The same rules as the Worker (src/inject.ts in sweatstack-tools):
4
+
5
+ - The WebRTC block goes first inside `<head>`; without a `<head>`, before the element that follows
6
+ `<html>`, or before the first element; never before the doctype. With no element at all, at the
7
+ end. The report link goes at the end of the document.
8
+ - `<script type="speculationrules">` is removed, and so is a `<link>` whose `rel` includes
9
+ `preconnect`, `dns-prefetch` or `prerender`: they make the browser reach hosts the page names
10
+ with no check by the policy.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from html.parser import HTMLParser
16
+
17
+ ASCII_WHITESPACE = "\t\n\f\r "
18
+ SPECULATIVE_HINTS = frozenset({"preconnect", "dns-prefetch", "prerender"})
19
+
20
+
21
+ def is_speculation_rules(script_type: str | None) -> bool:
22
+ """The browser trims ASCII whitespace from a script's type and compares it in any case."""
23
+ return (
24
+ script_type is not None
25
+ and script_type.strip(ASCII_WHITESPACE).lower() == "speculationrules"
26
+ )
27
+
28
+
29
+ def is_speculative_hint(rel: str | None) -> bool:
30
+ """`rel` is a list of tokens, separated by ASCII whitespace, in any case."""
31
+ if rel is None:
32
+ return False
33
+ tokens = rel.lower().translate({ord(c): " " for c in ASCII_WHITESPACE}).split()
34
+ return any(token in SPECULATIVE_HINTS for token in tokens)
35
+
36
+
37
+ class _Scanner(HTMLParser):
38
+ """Finds where to place the WebRTC block, and which spans to remove, by character offset."""
39
+
40
+ def __init__(self, text: str) -> None:
41
+ super().__init__(convert_charrefs=False)
42
+ self.text = text
43
+ self._line_starts = [0]
44
+ for index, char in enumerate(text):
45
+ if char == "\n":
46
+ self._line_starts.append(index + 1)
47
+ # Where the WebRTC block goes, once the first element is seen.
48
+ self.insert_at: int | None = None
49
+ self.removals: list[tuple[int, int]] = []
50
+ self._rules_start: int | None = None
51
+
52
+ def _offset(self) -> int:
53
+ line, column = self.getpos()
54
+ return self._line_starts[line - 1] + column
55
+
56
+ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
57
+ self._element(tag, attrs, closes_itself=False)
58
+
59
+ def handle_startendtag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
60
+ self._element(tag, attrs, closes_itself=True)
61
+
62
+ def _element(
63
+ self, tag: str, attrs: list[tuple[str, str | None]], *, closes_itself: bool
64
+ ) -> None:
65
+ start = self._offset()
66
+ end = start + len(self.get_starttag_text() or "")
67
+ if self.insert_at is None and tag != "html":
68
+ self.insert_at = end if tag == "head" else start
69
+ values = dict(attrs)
70
+ if tag == "link" and is_speculative_hint(values.get("rel")):
71
+ self.removals.append((start, end))
72
+ elif tag == "script" and not closes_itself and is_speculation_rules(values.get("type")):
73
+ self._rules_start = start
74
+
75
+ def handle_endtag(self, tag: str) -> None:
76
+ if tag == "script" and self._rules_start is not None:
77
+ start = self._offset()
78
+ close = self.text.find(">", start)
79
+ end = len(self.text) if close == -1 else close + 1
80
+ self.removals.append((self._rules_start, end))
81
+ self._rules_start = None
82
+
83
+
84
+ def rewrite_html(text: str, *, head: str, body: str) -> str:
85
+ """The page as the Tools app serves it. `head` is the WebRTC block, `body` the report link."""
86
+ scanner = _Scanner(text)
87
+ scanner.feed(text)
88
+ scanner.close()
89
+ if scanner._rules_start is not None:
90
+ # Rules never closed run to the end of the document, as a browser would read them.
91
+ scanner.removals.append((scanner._rules_start, len(text)))
92
+
93
+ insert_at = scanner.insert_at
94
+ pieces: list[str] = []
95
+ position = 0
96
+ for start, end in sorted(scanner.removals):
97
+ if insert_at is not None and position <= insert_at <= start:
98
+ pieces.append(text[position:insert_at] + head)
99
+ position = insert_at
100
+ insert_at = None
101
+ pieces.append(text[position:start])
102
+ position = end
103
+ if insert_at is not None and insert_at >= position:
104
+ pieces.append(text[position:insert_at] + head)
105
+ position = insert_at
106
+ insert_at = None
107
+ pieces.append(text[position:])
108
+ if scanner.insert_at is None:
109
+ pieces.append(head)
110
+ pieces.append(body)
111
+ return "".join(pieces)
@@ -0,0 +1,148 @@
1
+ """`tool serve`: a tool's folder, served locally the way SweatStack Tools serves it.
2
+
3
+ Every response carries the production policy's headers, minus reporting and HSTS. HTML is rewritten
4
+ as in production. Paths resolve in production's order: the exact path, then `.html`, then a
5
+ directory's `index.html` (a directory without its slash is redirected to it), then `404.html`, then
6
+ a plain 404. Two serving rules stay out, because neither applies on a developer's own machine: the
7
+ gate, and the cross-site rule. See plans/developer-kit/cli.md#tool-serve in sweatstack-tools.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import json
13
+ from http import HTTPStatus
14
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
15
+ from importlib import resources
16
+ from pathlib import Path
17
+ from typing import Any
18
+ from urllib.parse import unquote, urlsplit
19
+
20
+ from sweatstack_cli.tools.policy import Policy
21
+ from sweatstack_cli.tools.rewrite import rewrite_html
22
+
23
+ _CONTENT_TYPES: dict[str, Any] = json.loads(
24
+ resources.files("sweatstack_cli.tools").joinpath("content_types.json").read_text("utf-8")
25
+ )
26
+
27
+
28
+ def content_type_of(path: str) -> str:
29
+ """The content type SweatStack Tools gives a file, from the contract's table."""
30
+ segment = path.rsplit("/", 1)[-1]
31
+ dot = segment.rfind(".")
32
+ if dot <= 0:
33
+ return str(_CONTENT_TYPES["default"])
34
+ table: dict[str, str] = _CONTENT_TYPES["byExtension"]
35
+ return table.get(segment[dot + 1 :].lower(), str(_CONTENT_TYPES["default"]))
36
+
37
+
38
+ def _is_html(content_type: str) -> bool:
39
+ return content_type.startswith("text/html")
40
+
41
+
42
+ class ToolRequestHandler(BaseHTTPRequestHandler):
43
+ """Serves one tool's folder. Configured by `make_server`."""
44
+
45
+ root: Path
46
+ policy: Policy
47
+ slug: str
48
+ server_version = "sweatstack-tool-serve"
49
+ sys_version = ""
50
+
51
+ # ---------------------------------------------------------------- responses
52
+
53
+ def _send(
54
+ self,
55
+ status: int,
56
+ body: bytes = b"",
57
+ *,
58
+ content_type: str = "text/plain; charset=utf-8",
59
+ extra: dict[str, str] | None = None,
60
+ ) -> None:
61
+ self.send_response(status)
62
+ for name, value in self.policy.local_headers().items():
63
+ self.send_header(name, value)
64
+ self.send_header("Cache-Control", "no-cache")
65
+ self.send_header("Content-Type", content_type)
66
+ self.send_header("Content-Length", str(len(body)))
67
+ for name, value in (extra or {}).items():
68
+ self.send_header(name, value)
69
+ self.end_headers()
70
+ if self.command != "HEAD":
71
+ self.wfile.write(body)
72
+
73
+ def _file(self, relative: str, status: int = HTTPStatus.OK) -> None:
74
+ file = self.root / relative
75
+ content_type = content_type_of(relative)
76
+ data = file.read_bytes()
77
+ if _is_html(content_type):
78
+ # Bytes that are not UTF-8 pass through unchanged, as they do in production.
79
+ text = data.decode("utf-8", errors="surrogateescape")
80
+ text = rewrite_html(
81
+ text, head=self.policy.inject_head, body=self.policy.report_link(self.slug)
82
+ )
83
+ data = text.encode("utf-8", errors="surrogateescape")
84
+ self._send(status, data, content_type=content_type)
85
+
86
+ # ---------------------------------------------------------------- resolution
87
+
88
+ def _exists(self, relative: str) -> bool:
89
+ """Whether `relative` is a file inside the tool's folder. Nothing outside it ever is."""
90
+ if not relative or "\x00" in relative:
91
+ return False
92
+ candidate = (self.root / relative).resolve()
93
+ return candidate.is_relative_to(self.root) and candidate.is_file()
94
+
95
+ def _serve(self) -> None:
96
+ parts = urlsplit(self.path)
97
+ try:
98
+ path = unquote(parts.path, errors="strict").removeprefix("/")
99
+ except UnicodeDecodeError:
100
+ self._send(HTTPStatus.BAD_REQUEST, b"Bad request.\n")
101
+ return
102
+ if path.startswith("_gate"):
103
+ self._send(HTTPStatus.NOT_FOUND, b"Not found.\n")
104
+ return
105
+
106
+ if path == "" or path.endswith("/"):
107
+ if self._exists(f"{path}index.html"):
108
+ self._file(f"{path}index.html")
109
+ return
110
+ else:
111
+ for candidate in (path, f"{path}.html"):
112
+ if self._exists(candidate):
113
+ self._file(candidate)
114
+ return
115
+ if self._exists(f"{path}/index.html"):
116
+ query = f"?{parts.query}" if parts.query else ""
117
+ self._send(
118
+ HTTPStatus.TEMPORARY_REDIRECT, extra={"Location": f"{parts.path}/{query}"}
119
+ )
120
+ return
121
+
122
+ if self._exists("404.html"):
123
+ self._file("404.html", HTTPStatus.NOT_FOUND)
124
+ else:
125
+ self._send(HTTPStatus.NOT_FOUND, b"Not found.\n")
126
+
127
+ def do_GET(self) -> None:
128
+ self._serve()
129
+
130
+ def do_HEAD(self) -> None:
131
+ self._serve()
132
+
133
+ def _refuse(self) -> None:
134
+ self._send(
135
+ HTTPStatus.METHOD_NOT_ALLOWED, b"Method not allowed.\n", extra={"Allow": "GET, HEAD"}
136
+ )
137
+
138
+ do_POST = do_PUT = do_DELETE = do_PATCH = do_OPTIONS = _refuse
139
+
140
+
141
+ def make_server(root: Path, policy: Policy, *, slug: str, port: int) -> ThreadingHTTPServer:
142
+ """A server for `root` on localhost. Call `serve_forever()` on it."""
143
+ handler = type(
144
+ "BoundToolRequestHandler",
145
+ (ToolRequestHandler,),
146
+ {"root": root.resolve(), "policy": policy, "slug": slug},
147
+ )
148
+ return ThreadingHTTPServer(("localhost", port), handler)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: sweatstack-cli
3
- Version: 0.15.0
3
+ Version: 0.16.0
4
4
  Summary: Command-line interface for SweatStack — the sports data platform for developers
5
5
  Project-URL: Homepage, https://sweatstack.no
6
6
  Project-URL: Documentation, https://docs.sweatstack.no
@@ -1,9 +1,9 @@
1
1
  sweatstack_cli/__init__.py,sha256=FalqkgMa69vADPpNY76Cztq5oiLgzRNVlhaR4itdZ78,465
2
- sweatstack_cli/config.py,sha256=cD4OXnVAKVagYazyk9Cw-WWx62tJPqcjQt1cknYyymM,706
2
+ sweatstack_cli/config.py,sha256=Gs2NEGpVy3_96jmGXJuuqLf3zH6MM8zgDMfsBOTqEGs,861
3
3
  sweatstack_cli/console.py,sha256=69KWAM7ksnYRTONMYTXGXN4LWRAQhRxcLXO0LcWGLqo,300
4
4
  sweatstack_cli/exceptions.py,sha256=Qzc1tbchk3Zhfin4Z08hQ1xpTK-dKqjkde2CXDpAY4s,2227
5
5
  sweatstack_cli/go_live.py,sha256=6-WjN9jyfLnbLXs0eAEX0NkwWeigf1A7BYVGG6W092w,1855
6
- sweatstack_cli/main.py,sha256=D2Yd2pPY_Uf_rgCAy_ayxiQaNTV1t8BtyYxMl8lvMKU,2350
6
+ sweatstack_cli/main.py,sha256=ZwS92BCGGhYB_Khc237lb4XzzBR5ay6vYJK_Vrn8S5k,2393
7
7
  sweatstack_cli/project.py,sha256=yUpkrEbPD2c0lu8IKw88aQrkFtksLHMjmgHlaqKVQUo,3660
8
8
  sweatstack_cli/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
9
  sweatstack_cli/version_check.py,sha256=yZ_qVe7tUBowUDAmMki-wyf5pSxlNDsoWKgTcSfesBA,2015
@@ -18,10 +18,18 @@ sweatstack_cli/commands/__init__.py,sha256=YCO8wYVvdGeoEI0p_oUm8ANLneuET4SHsKDji
18
18
  sweatstack_cli/commands/app.py,sha256=90AwUm5RhagE5tZww77Fk9B0vpZFlC59OlylGXTxWC0,9055
19
19
  sweatstack_cli/commands/auth.py,sha256=vmiYfkvrHIOyF0DTC6JI95dieu9uyaq6xvx9M9_grQE,4754
20
20
  sweatstack_cli/commands/page.py,sha256=G58KEsiCeq4hD0Z5ot2g9W6vBH6iZu-lCvdA1kzep-U,24612
21
+ sweatstack_cli/commands/tool.py,sha256=jZym2HkSSW0OiylbA0qwJm0bP0jvjtde5-nQrnsrnFg,5681
21
22
  sweatstack_cli/services/__init__.py,sha256=pFrcjumdHNRU8aa3bxplq4jhCivH2YGGqkGG8yIMv20,337
22
23
  sweatstack_cli/services/applications.py,sha256=VToXlk48kn2-39eTdDf_hSJUdZqkJjCFoDaqf6DpdGA,4428
23
24
  sweatstack_cli/templates/page/index.html,sha256=-PxqMi5eq4GbNGhjz4U9vD_oRReyngGPX2T0Rv1LL3M,20395
24
- sweatstack_cli-0.15.0.dist-info/METADATA,sha256=yez50FaXk3UnGbQ5ZF6DVEX5ouVQuXTtGb4CswxKMo0,5678
25
- sweatstack_cli-0.15.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
26
- sweatstack_cli-0.15.0.dist-info/entry_points.txt,sha256=rnH0EV1U9CQlmKQcJ7RmxGpNRTa9J-YdiIJZDYrAXb4,56
27
- sweatstack_cli-0.15.0.dist-info/RECORD,,
25
+ sweatstack_cli/tools/__init__.py,sha256=V2ermw4eTBgVVze_iScOEaG2p7NoKmVD146ISqLQqBk,93
26
+ sweatstack_cli/tools/check.py,sha256=MPAmp3fhrsFKXT8OXlYtCMiXhTHUu1imQwQXmnvNsbg,6995
27
+ sweatstack_cli/tools/content_types.json,sha256=PSW-HQpSYQR_AaIp-cWmVQqVmUx9FQY-_sZqka-DJOk,2052
28
+ sweatstack_cli/tools/policy.json,sha256=gzgls_sSMZZY60Rj0m2j63cW1RYRrlmZaUgeh2djXyU,2414
29
+ sweatstack_cli/tools/policy.py,sha256=9EEtZWRrWQXFROY-CvnZtW0ZspiMG4JX58283pg7Hz0,5989
30
+ sweatstack_cli/tools/rewrite.py,sha256=19GkK-aGmjoAPUrY0336g4Pjl_8Uu5IMHmY7d-vHs2I,4525
31
+ sweatstack_cli/tools/server.py,sha256=Sk6u9ltMPEktFdpuFGgrm9kj4z9IOFlrMV3ETMfc0YA,5578
32
+ sweatstack_cli-0.16.0.dist-info/METADATA,sha256=BJSw_9mkZMxfzeW0KAhhzBchBhUvOSLXi8YMIkle1sw,5678
33
+ sweatstack_cli-0.16.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
34
+ sweatstack_cli-0.16.0.dist-info/entry_points.txt,sha256=rnH0EV1U9CQlmKQcJ7RmxGpNRTa9J-YdiIJZDYrAXb4,56
35
+ sweatstack_cli-0.16.0.dist-info/RECORD,,