| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "time" |
| 6 | |
| 7 | "amadan.net/rastrillo/rastrillo" |
| 8 | |
| 9 | // The pack for rastrillo's one language here: from rastrillo v0.29.0 |
| 10 | // a language with no pack linked is refused at start (Serve and |
| 11 | // Handler both), and the sign-in door's words come from it. |
| 12 | _ "amadan.net/rastrillo/rastrillo/locales/en" |
| 13 | ) |
| 14 | |
| 15 | // ContentSecurityPolicy is the policy Calendar serves standalone. It is |
| 16 | // rastrillo's default policy with one change: style-src keeps |
| 17 | // 'unsafe-inline', which was rastrillo's own default until the bump to |
| 18 | // fb806a2. |
| 19 | // |
| 20 | // The new default (style-src 'self' 'unsafe-hashes' plus pow's honeypot |
| 21 | // hash) refuses every style attribute, and Calendar draws its time with |
| 22 | // them: |
| 23 | // |
| 24 | // - the day and week grids place each event by its start and length |
| 25 | // (top, height, and the packer's left and width, in percent of the |
| 26 | // day: day.html, week.html, pack.go), and each half-hour slot link |
| 27 | // the same way. They are per event and continuous, so they cannot |
| 28 | // be classes in calendar.css; without them every chip and slot |
| 29 | // collapses to the top of its column; |
| 30 | // - every chip, and each calendar's swatch on the Calendars page, |
| 31 | // carries its calendar's hue as --cal-hue (_chip.html, |
| 32 | // calendars.html). The hue is per calendar (personalHue), and |
| 33 | // calendar.css mixes every colour from it. |
| 34 | // |
| 35 | // Hashing them is not possible (the values are per event), and the other |
| 36 | // directives are unchanged, so scripts stay same-origin only and framing, |
| 37 | // base and form targets stay locked. The constant layout that used to be |
| 38 | // style attributes is in calendar.css now. Home mode serves |
| 39 | // connect.FormPolicy, which carries the same style-src. |
| 40 | // TestProductionCSPIsTheDefaultBarStyles keeps every other directive in |
| 41 | // step with rastrillo's default. |
| 42 | const ContentSecurityPolicy = "default-src 'self'; style-src 'self' 'unsafe-inline'; " + |
| 43 | "img-src 'self' data:; frame-ancestors 'none'; base-uri 'self'; form-action 'self'" |
| 44 | |
| 45 | // ServeOptions completes opts with what Calendar serves: its router, its |
| 46 | // error page and its CSP. main passes the result to rastrillo.Serve, and |
| 47 | // the CSP tests pass it to rastrillo.Handler, so the policy they check is |
| 48 | // the one production runs under. |
| 49 | func (a *App) ServeOptions(opts rastrillo.Options) rastrillo.Options { |
| 50 | opts.Mux = a.Handler() |
| 51 | // The same page a handler's own error path renders, so a panic and |
| 52 | // a handled failure look identical to whoever hits one. |
| 53 | opts.ErrorPage = ErrorPage |
| 54 | opts.CSP = ContentSecurityPolicy |
| 55 | return opts |
| 56 | } |
| 57 | |
| 58 | // containsHTML reports whether an Accept header asks for HTML (notFound). |
| 59 | func containsHTML(accept string) bool { |
| 60 | for i := 0; i+9 <= len(accept); i++ { |
| 61 | if accept[i:i+9] == "text/html" { |
| 62 | return true |
| 63 | } |
| 64 | } |
| 65 | return false |
| 66 | } |
| 67 | |
| 68 | // NextDue answers the platform's scheduled-wake poll: the next moment |
| 69 | // this instance has work. rastrillo mounts it as GET /api/next-due, |
| 70 | // outside app middleware, and the activator asks before hibernating. |
| 71 | func (a *App) NextDue() time.Time { |
| 72 | return a.scheduled.probe() |
| 73 | } |
| 74 | |
| 75 | // Handler is the app's router, for rastrillo.Options.Mux and for |
| 76 | // tests. It is typed *http.ServeMux rather than http.Handler because |
| 77 | // Options.Mux is typed that way. |
| 78 | func (a *App) Handler() *http.ServeMux { return a.mux } |
| 79 | |