server

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildPolicy added in v0.1.0

func BuildPolicy(scriptHashes []string) string

BuildPolicy assembles the orchestrator's Content-Security-Policy directive string. scriptHashes are the 'sha256-...' tokens for every inline <script> block in the UI templates (see InlineScriptHashes) — each one must be present in script-src or the corresponding inline script is blocked by the browser.

'unsafe-eval' is required by Tailwind's Play CDN, which compiles CSS at runtime, and by htmx's evaluation of hx-on attribute bodies. It permits eval/new Function but does NOT re-permit inline <script> blocks or inline event-handler attributes — those still require the hashes above — so the protection that matters (blocking an injected <img onerror=...> payload) is retained.

style-src keeps 'unsafe-inline' because Tailwind injects <style> elements at runtime; style-only injection is a markedly lower-severity class than script execution.

connect-src includes Iconify's icon-data API and its two documented fallback hosts: the <script> tag is loaded from code.iconify.design, but icon SVGs are fetched at runtime from api.iconify.design (falling back to api.simplesvg.com / api.unisvg.com) — omitting them renders every icon blank.

img-src allows any HTTPS host ("https:"), not a targeted allowlist, for GitHub-hosted issue images. Fix round 2 tried the targeted approach — user-images.githubusercontent.com plus a path-scoped github.com/user-attachments/ — but the format GitHub writes into issue bodies today, github.com/user-attachments/assets/<uuid>, 302-redirects to a github-production-user-asset-*.s3.amazonaws.com host. Per CSP3, every hop of a redirect is matched again, and past the first hop only scheme and host are checked (the path is skipped) — so that S3 host, which matches nothing in a targeted list, gets the image blocked at the redirect. The targeted approach produced exactly the broken images it was meant to prevent. Hardcoding the S3 bucket host was rejected: it's a GitHub implementation detail that can change without notice, silently re-breaking images again.

The cost is real and is accepted deliberately: an <img> in rendered issue content fires on view, so a maliciously authored issue body could use it as a tracking pixel — leaking the viewing operator's IP, user agent, and the timing of internal review to whoever authored the issue. This is judged acceptable because script-src already blocks execution and DOMPurify sanitizes the markup before it's ever rendered, so the residual risk is a tracking pixel, not data exfiltration: an injected image's URL is fixed at authoring time and cannot carry any information the attacker didn't already have. Broken images are, empirically, the failure most likely to get the whole policy switched off — which would forfeit the script-execution protection that actually matters — so a slightly permissive img-src is the better trade.

func CSPMiddleware added in v0.1.0

func CSPMiddleware(policy, mode string) func(http.Handler) http.Handler

CSPMiddleware returns middleware that sets the Content-Security-Policy header (or, in report-only mode, Content-Security-Policy-Report-Only) before calling next. mode == "off" is a pure pass-through that sets neither header — an explicit escape hatch for a deployment where the policy breaks something the fix can't reach in time.

func InlineScriptHashes added in v0.1.0

func InlineScriptHashes(fsys fs.FS) ([]string, error)

InlineScriptHashes walks fsys recursively for *.html files (including a partials/ subdirectory) and returns a sorted, de-duplicated list of CSP script-src source tokens ("'sha256-<base64>'"), one per distinct inline <script> body found. Scripts carrying a `src` attribute are skipped — they load external code, and hashing their (empty) inline body would be meaningless.

fsys must be the filesystem the app actually renders from — in production that is ui.TemplateFS(), the //go:embed'd filesystem baked into the binary, not a path read from disk. Hashing a disk copy is wrong even where the files happen to exist: a stale or locally-modified working copy yields a policy that authorizes bytes nobody serves, or blocks bytes that are served, and a container image that ships only the compiled binary (as this project's does) has no disk copy at all. Taking an fs.FS makes the hashes correct by construction, because they come from the same bytes the renderer parses, and callers that do want a disk tree (e.g. scratch test fixtures) can still supply one via os.DirFS.

The hash is computed over the exact bytes the browser receives, not the source bytes between '>' and '</script>': html/template elides JavaScript comments while rendering (escape.go's stateJSLineCmt drops a "//..." line comment entirely; stateJSBlockCmt collapses a "/* */" block comment to a single space or newline), so an inline script containing a comment is served with bytes that differ from its source — hashing the source would compute a token the browser never matches, silently blocking the script. Each body is round-tripped through html/template in its own <script> context (see renderScriptBody) before hashing, reproducing the exact rendered bytes.

An error is returned — never a nil/empty slice — if fsys cannot be walked, if a script body cannot be rendered, if a script body contains a template action (see extractInlineScriptHashes), or if the number of hashes extracted from a file doesn't match its number of qualifying opening tags. Swallowing any of these and returning fewer hashes than exist would silently produce a policy that blocks that script; the caller (see Server.Routes) is expected to fall back to CSP mode "off" rather than ship that.

Types

type Server

type Server struct {
	DB           *gorm.DB
	Hub          *ws.Hub
	Broker       *sse.Broker
	SecureCookie bool
	BaseURL      string
	// Sync triggers an immediate manual GitHub ingest pass. Left nil when
	// GitHub sync is not running (no repos enabled, or the token is
	// missing) — the default install; api.Handlers responds 503 in that
	// case rather than panicking. Set from cmd/orchestrator/main.go only
	// when the worker was actually started, so a nil *ghsync.Worker is
	// never boxed into this interface (see main.go for why that matters).
	Sync api.SyncTrigger
	// ManualSyncCooldown is the minimum gap between manual syncs of one repo.
	ManualSyncCooldown time.Duration
	// CSPMode controls the Content-Security-Policy header: "enforce"
	// (default), "report-only", or "off". Set from config.CSPConfig.Mode in
	// cmd/orchestrator/main.go, which validates it; an unset or otherwise
	// unrecognized value is treated as "enforce" by CSPMiddleware.
	CSPMode string
	// GitHubTokenEnv is the name of the environment variable the GitHub token
	// is read from, passed through to the handlers so a refusal can name the
	// variable this deployment actually uses.
	GitHubTokenEnv string

	// Wait holds the supervisor's usage-limit wait caps.
	Wait api.WaitPolicy

	// GitHubDefaultLabel is the trigger label a newly registered repository
	// starts with (config.GitHubConfig.TriggerLabel).
	GitHubDefaultLabel string
}

Server holds the shared dependencies used by all HTTP handlers.

func New

func New(gdb *gorm.DB, hub *ws.Hub, broker *sse.Broker, secureCookie bool, baseURL string) *Server

New creates a Server with the given dependencies. baseURL is the orchestrator's externally reachable base URL, passed through to API handlers that link back to tickets (e.g. GitHub milestone comments).

func (*Server) Routes

func (s *Server) Routes() http.Handler

Routes returns the full HTTP mux with all handler groups registered.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL