Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions internal/web/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,27 @@
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="theme-color" content="#f6f5f2" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#191a1c" media="(prefers-color-scheme: dark)">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="Heating">
<meta name="apple-mobile-web-app-status-bar-style" content="default">
<!-- Installable to the home screen, but with no service worker: nothing is
cached offline, so the page always shows the live state. -->
<!-- Installable to the home screen through the manifest, which browsers read
instead of the old apple-mobile-web-app-* tags. There is no service worker:
nothing is cached offline, so the page always shows the live state. -->
<link rel="manifest" href="manifest.webmanifest">
<link rel="icon" href="icon-192.png" type="image/png">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<title>Heating</title>
<style>
/* Each color is light, then dark, following the phone's setting. */
:root {
color-scheme: light dark;
--bg: #f6f5f2; --card: #ffffff; --text: #1d1f21; --muted: #666a70;
--line: #dddcd7; --button: #eceae5; --accent: #b4490f; --error: #a4161a;
--good: #1a7f37; --bad: #c62828;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #191a1c; --card: #24262a; --text: #eeeeec; --muted: #a2a6ac;
--line: #383a3f; --button: #33363b; --accent: #f08a4b; --error: #ff8a8a;
--good: #3fb950; --bad: #ff6b6b;
}
--bg: light-dark(#f6f5f2, #191a1c);
--card: light-dark(#ffffff, #24262a);
--text: light-dark(#1d1f21, #eeeeec);
--muted: light-dark(#666a70, #a2a6ac);
--line: light-dark(#dddcd7, #383a3f);
--button: light-dark(#eceae5, #33363b);
--accent: light-dark(#b4490f, #f08a4b);
--error: light-dark(#a4161a, #ff8a8a);
--good: light-dark(#1a7f37, #3fb950);
--bad: light-dark(#c62828, #ff6b6b);
}
* { box-sizing: border-box; }
body {
Expand Down Expand Up @@ -91,6 +89,7 @@
<h1>Heating</h1>
<div id="status" role="status">Connecting…</div>
</header>
<main>
<div id="away">
<div id="away-banner" hidden>
<p id="away-text"></p>
Expand All @@ -114,7 +113,8 @@ <h2 id="away-title">Away</h2>
</form>
</dialog>
<div id="rooms"></div>
<p id="error" role="status"></p>
<p id="error" role="alert"></p>
</main>
<script>
const roomsEl = document.getElementById('rooms');
const errorEl = document.getElementById('error');
Expand All @@ -138,6 +138,7 @@ <h2 id="away-title">Away</h2>
const airingMinutes = [5, 15, 30, 60];
// Thermostats report at least about every 25 minutes.
const staleAfterMs = 2 * 3600 * 1000;
const refreshTimeoutMs = 10000;

const clock = iso => new Date(iso).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hourCycle: 'h23' });
const dayAndClock = iso => new Date(iso).toLocaleString([], {
Expand Down Expand Up @@ -320,7 +321,8 @@ <h2 id="away-title">Away</h2>
async function refresh() {
const generation = ++refreshGeneration;
try {
const res = await fetch('api/rooms');
// A hung request would hold back every refresh after it.
const res = await fetch('api/rooms', { signal: AbortSignal.timeout(refreshTimeoutMs) });
if (!res.ok) throw new Error(res.statusText);
const next = await res.json();
if (sending > 0 || generation !== refreshGeneration) return;
Expand Down
1 change: 1 addition & 0 deletions internal/web/static/manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "/",
"name": "Heating",
"short_name": "Heating",
"description": "Adjust the heating per room on the home network.",
Expand Down
13 changes: 12 additions & 1 deletion internal/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,18 @@ func Handler(rooms Rooms, broker health.Checker, allowedHosts []string) http.Han
crossOrigin.SetDenyHandler(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
writeError(w, http.StatusForbidden, "request from another website")
}))
return checkHost(allowedHosts, crossOrigin.Handler(mux))
return checkHost(allowedHosts, noFraming(crossOrigin.Handler(mux)))
}

// noFraming forbids other pages to embed this one. Requests from a framed page
// are same-origin, so without this another website could frame the page and
// trick a tap on its buttons.
func noFraming(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Security-Policy", "frame-ancestors 'none'")
w.Header().Set("X-Frame-Options", "DENY")
next.ServeHTTP(w, r)
})
}

// checkHost refuses unknown host names. With DNS rebinding another website's
Expand Down
15 changes: 14 additions & 1 deletion internal/web/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ func TestPageIsServed(t *testing.T) {
}
}

func TestPageCantBeFramed(t *testing.T) {
for _, path := range []string{"/", "/api/rooms"} {
rec := get(t, path)
if got := rec.Header().Get("Content-Security-Policy"); got != "frame-ancestors 'none'" {
t.Errorf("%s: Content-Security-Policy = %q", path, got)
}
if got := rec.Header().Get("X-Frame-Options"); got != "DENY" {
t.Errorf("%s: X-Frame-Options = %q", path, got)
}
}
}

func TestHomeScreenFilesAreServed(t *testing.T) {
page := get(t, "/").Body.String()
for _, link := range []string{`rel="manifest" href="manifest.webmanifest"`, `rel="apple-touch-icon" href="apple-touch-icon.png"`} {
Expand All @@ -123,6 +135,7 @@ func TestHomeScreenFilesAreServed(t *testing.T) {
t.Errorf("Content-Type = %q", got)
}
var manifest struct {
ID string `json:"id"`
StartURL string `json:"start_url"`
Display string `json:"display"`
Icons []struct {
Expand All @@ -133,7 +146,7 @@ func TestHomeScreenFilesAreServed(t *testing.T) {
if err := json.Unmarshal(rec.Body.Bytes(), &manifest); err != nil {
t.Fatal(err)
}
if manifest.StartURL != "/" || manifest.Display != "standalone" {
if manifest.ID != "/" || manifest.StartURL != "/" || manifest.Display != "standalone" {
t.Errorf("manifest = %+v", manifest)
}

Expand Down