sweatstack-cli 0.15.0__tar.gz → 0.16.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.
Files changed (81) hide show
  1. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/CHANGELOG.md +18 -0
  2. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/Makefile +8 -1
  3. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/PKG-INFO +1 -1
  4. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/pyproject.toml +1 -1
  5. sweatstack_cli-0.16.0/src/sweatstack_cli/commands/tool.py +153 -0
  6. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/config.py +3 -0
  7. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/main.py +2 -1
  8. sweatstack_cli-0.16.0/src/sweatstack_cli/tools/__init__.py +1 -0
  9. sweatstack_cli-0.16.0/src/sweatstack_cli/tools/check.py +184 -0
  10. sweatstack_cli-0.16.0/src/sweatstack_cli/tools/content_types.json +55 -0
  11. sweatstack_cli-0.16.0/src/sweatstack_cli/tools/policy.json +25 -0
  12. sweatstack_cli-0.16.0/src/sweatstack_cli/tools/policy.py +155 -0
  13. sweatstack_cli-0.16.0/src/sweatstack_cli/tools/rewrite.py +111 -0
  14. sweatstack_cli-0.16.0/src/sweatstack_cli/tools/server.py +148 -0
  15. sweatstack_cli-0.16.0/tests/e2e/test_tool_serve.py +104 -0
  16. sweatstack_cli-0.16.0/tests/test_tools/__init__.py +0 -0
  17. sweatstack_cli-0.16.0/tests/test_tools/test_check.py +181 -0
  18. sweatstack_cli-0.16.0/tests/test_tools/test_policy.py +163 -0
  19. sweatstack_cli-0.16.0/tests/test_tools/test_rewrite.py +133 -0
  20. sweatstack_cli-0.16.0/tests/test_tools/test_server.py +195 -0
  21. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/uv.lock +1 -1
  22. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/.claude/settings.local.json +0 -0
  23. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/.claude/skills/sweatstack/SKILL.md +0 -0
  24. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/.claude/skills/sweatstack/api.md +0 -0
  25. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/.claude/skills/sweatstack/auth.md +0 -0
  26. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/.claude/skills/sweatstack/cli.md +0 -0
  27. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/.claude/skills/sweatstack/pyscript.md +0 -0
  28. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/.claude/skills/sweatstack/webapp.md +0 -0
  29. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/.gitignore +0 -0
  30. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/.python-version +0 -0
  31. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/CREATE_PRIVATE_APP.md +0 -0
  32. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/DEVELOPMENT.md +0 -0
  33. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/PLAN.md +0 -0
  34. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/README.md +0 -0
  35. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/plans/001-page-init-command.md +0 -0
  36. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/__init__.py +0 -0
  37. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/api/__init__.py +0 -0
  38. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/api/client.py +0 -0
  39. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/auth/__init__.py +0 -0
  40. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/auth/callback_server.py +0 -0
  41. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/auth/jwt.py +0 -0
  42. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/auth/pkce.py +0 -0
  43. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/auth/tokens.py +0 -0
  44. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/commands/__init__.py +0 -0
  45. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/commands/app.py +0 -0
  46. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/commands/auth.py +0 -0
  47. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/commands/page.py +0 -0
  48. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/console.py +0 -0
  49. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/exceptions.py +0 -0
  50. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/go_live.py +0 -0
  51. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/project.py +0 -0
  52. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/py.typed +0 -0
  53. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/services/__init__.py +0 -0
  54. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/services/applications.py +0 -0
  55. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/templates/page/index.html +0 -0
  56. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/src/sweatstack_cli/version_check.py +0 -0
  57. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/sweatstack.toml +0 -0
  58. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/__init__.py +0 -0
  59. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/conftest.py +0 -0
  60. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/e2e/__init__.py +0 -0
  61. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/e2e/test_page_template.py +0 -0
  62. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_api/__init__.py +0 -0
  63. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_api/test_client.py +0 -0
  64. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_auth/__init__.py +0 -0
  65. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_auth/test_authenticator.py +0 -0
  66. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_auth/test_authorize_url.py +0 -0
  67. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_auth/test_callback_server.py +0 -0
  68. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_auth/test_jwt.py +0 -0
  69. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_auth/test_pkce.py +0 -0
  70. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_auth/test_tokens.py +0 -0
  71. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_commands/__init__.py +0 -0
  72. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_commands/test_app.py +0 -0
  73. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_commands/test_auth.py +0 -0
  74. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_commands/test_cli.py +0 -0
  75. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_commands/test_page_deploy.py +0 -0
  76. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_commands/test_page_init.py +0 -0
  77. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_project.py +0 -0
  78. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_services/__init__.py +0 -0
  79. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_services/test_applications.py +0 -0
  80. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_version.py +0 -0
  81. {sweatstack_cli-0.15.0 → sweatstack_cli-0.16.0}/tests/test_version_check.py +0 -0
@@ -4,6 +4,24 @@ All notable changes to this project are documented here. The format is based on
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
5
5
  adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.16.0] - 2026-11-20
8
+
9
+ ### Added
10
+
11
+ - `sweatstack tool serve [DIR]` serves a tool locally the way SweatStack Tools serves it:
12
+ every response carries the production network policy, without reporting and HSTS; HTML
13
+ gets the same WebRTC block and report link, and loses speculation rules and preconnect
14
+ or DNS prefetch hints, as in production; paths resolve in production's order, files get
15
+ the Tools contract's content types, and nothing outside the folder is served. A request
16
+ the policy blocks shows in the browser console with its file and line.
17
+ - `sweatstack tool check [DIR]` reports every reference in a tool's folder that loads from
18
+ another origin and would be blocked on SweatStack Tools, with file, line and kind, and
19
+ exits with 1 so an agent treats it as a failure to fix. It also lists what SweatStack
20
+ Tools removes. Links, licence comments and XML namespaces are never reported.
21
+ - Both read the policy from `https://sweatstack.tools/v1/policy`, with a one-second
22
+ timeout, and fall back to the copy cached from the last fetch, then to the copy in this
23
+ release (`make refresh-policy`). `SWEATSTACK_TOOLS_URL` points them elsewhere.
24
+
7
25
  ## [0.15.0] - 2026-11-19
8
26
 
9
27
  ### Fixed
@@ -1,4 +1,4 @@
1
- .PHONY: help install dev test test-e2e lint format typecheck check clean build publish
1
+ .PHONY: help install dev test test-e2e lint format typecheck check clean build publish refresh-policy
2
2
 
3
3
  help:
4
4
  @echo "SweatStack CLI Development Commands"
@@ -11,6 +11,7 @@ help:
11
11
  @echo " make format Format code"
12
12
  @echo " make typecheck Run type checker"
13
13
  @echo " make check Run all checks (lint, format, typecheck, test)"
14
+ @echo " make refresh-policy Copy the live SweatStack Tools policy into the package"
14
15
  @echo " make clean Remove build artifacts"
15
16
  @echo " make build Build distribution packages"
16
17
  @echo " make publish Publish to PyPI"
@@ -47,6 +48,12 @@ clean:
47
48
  find . -type d -name .mypy_cache -exec rm -rf {} + 2>/dev/null || true
48
49
  find . -type d -name .ruff_cache -exec rm -rf {} + 2>/dev/null || true
49
50
 
51
+ # The copy of the SweatStack Tools policy this release falls back to when sweatstack.tools is
52
+ # unreachable and nothing is cached. Run before a release, and commit the result.
53
+ refresh-policy:
54
+ curl -fsS https://sweatstack.tools/v1/policy -o src/sweatstack_cli/tools/policy.json
55
+ uv run python -c "import json; from sweatstack_cli.tools.policy import parse_policy; parse_policy(json.load(open('src/sweatstack_cli/tools/policy.json')), 'built-in')"
56
+
50
57
  build:
51
58
  rm -rf dist
52
59
  uv build
@@ -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,6 +1,6 @@
1
1
  [project]
2
2
  name = "sweatstack-cli"
3
- version = "0.15.0"
3
+ version = "0.16.0"
4
4
  description = "Command-line interface for SweatStack — the sports data platform for developers"
5
5
  readme = "README.md"
6
6
  authors = [{ name = "Aart Goossens", email = "aart@sweatstack.no" }]
@@ -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)
@@ -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
  )
@@ -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
+ }