oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "net/http"
5
6 "amadan.net/rastrillo/rastrillo"
7
8 // The pack for rastrillo's one language here: from rastrillo v0.29.0
9 // a language with no pack linked is refused at start (Serve and
10 // Handler both), and the sign-in door's words come from it.
11 _ "amadan.net/rastrillo/rastrillo/locales/en"
12)
13
14// ContentSecurityPolicy is the policy Sheets serves under standalone.
15// It is rastrillo's default policy with one change: style-src keeps
16// 'unsafe-inline', which was rastrillo's own default until the bump to
17// fb806a2.
18//
19// The new default (style-src 'self' 'unsafe-hashes' plus pow's honeypot
20// hash) refuses every style attribute, and Sheets' documents are drawn
21// with them:
22//
23// - the scriptless table and public-link views paint each cell's fill
24// and text colour as custom properties on the cell (table.html,
25// tablepaint.go). The colours are the document's own, unbounded, so
26// they cannot be moved into sheets.css, and a per-document
27// stylesheet would be a new route and cache for every view;
28// - the Format menu's fill and text palettes draw each swatch's
29// colour the same way (sheet.html);
30// - the vendored Jspreadsheet and jsuites write style attributes of
31// their own (setAttribute("style", …) and style="…" in markup),
32// which this app does not patch: it is vendored as shipped.
33//
34// Hashing them is not possible (the values are per document), and the
35// other directives are unchanged, so scripts stay same-origin only and
36// framing, base and form targets stay locked. Home mode serves
37// connect.FormPolicy, which carries the same style-src.
38// TestProductionCSPIsTheDefaultBarStyles keeps every other directive in
39// step with rastrillo's default.
40const ContentSecurityPolicy = "default-src 'self'; style-src 'self' 'unsafe-inline'; " +
41 "img-src 'self' data:; frame-ancestors 'none'; base-uri 'self'; form-action 'self'"
42
43// ServeOptions completes opts with what Sheets serves: its router, its
44// error page and its CSP. main passes the result to rastrillo.Serve,
45// and the CSP tests pass it to rastrillo.Handler, so the policy they
46// check is the one production runs under.
47func (a *App) ServeOptions(opts rastrillo.Options) rastrillo.Options {
48 // rastrillo.Options.Mux wants a *http.ServeMux; the app's own
49 // router is a chi.Router (an http.Handler), so it mounts under one.
50 mux := http.NewServeMux()
51 mux.Handle("/", a.Router())
52 opts.Mux = mux
53 // A panic outside the router's signed-in group (where the app's own
54 // recoverer renders the viewer's chrome) is the same error page, not
55 // rastrillo's plain text (spec §3.3).
56 opts.ErrorPage = a.ErrorPage
57 opts.CSP = ContentSecurityPolicy
58 return opts
59}
60