summaryrefslogtreecommitdiffstats
path: root/roseh.moe.go
blob: 99d1bedf796a39866d8ed7236b0ea440e65c86a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"crypto/hmac"
	"crypto/rand"
	"crypto/sha512"
	"crypto/subtle"
	"embed"
	"encoding/base64"
	"encoding/hex"
	"flag"
	"fmt"
	"html/template"
	"io"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"time"

	"gitlab.com/rhogenson/roseh.moe/internal/pwhash"
)

var (
	port        = flag.Int("port", 42069, "port to listen on")
	secretsFile = flag.String("secrets", "secrets", "path to the secrets file")
	notepadFile = flag.String("notepad", "notepad", "path to save user notes")

	serverStartTime = time.Now()
)

var (
	notepadPassword     []byte
	notepadPasswordSalt []byte
	privateKey          []byte

	encryptionKeyMu sync.Mutex
	encryptionKey   []byte
)

func loadSecrets() error {
	secrets, err := os.ReadFile(*secretsFile)
	if err != nil {
		return err
	}
	for _, line := range bytes.Split(bytes.TrimSuffix(secrets, []byte("\n")), []byte("\n")) {
		if pw, ok := bytes.CutPrefix(line, []byte("notepad-password=")); ok {
			buf := make([]byte, hex.DecodedLen(len(pw)))
			if _, err := hex.Decode(buf, pw); err != nil {
				return err
			}
			notepadPasswordSalt, notepadPassword = buf[:pwhash.SaltLen], buf[pwhash.SaltLen:]
		} else if key, ok := bytes.CutPrefix(line, []byte("secret-key=")); ok {
			if hex.DecodedLen(len(key)) != sha512.Size256 {
				return fmt.Errorf("invalid HMAC-SHA512/256 key")
			}
			privateKey = make([]byte, hex.DecodedLen(len(key)))
			if _, err := hex.Decode(privateKey, key); err != nil {
				return err
			}
		}
	}
	return nil
}

var (
	//go:embed templates/404.html.template
	notFoundString   string
	notFoundTemplate = template.Must(template.New("notFound").Parse(notFoundString))
)

func notFound(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusNotFound)
	type args struct {
		Path string
	}
	if err := notFoundTemplate.Execute(w, args{Path: r.URL.Path}); err != nil {
		log.Printf("Warning: notFound: %s", err)
	}
}

var (
	//go:embed templates/outline.html.template
	outlineString   string
	outlineTemplate = template.Must(template.New("outline").Parse(outlineString))

	//go:embed templates/index.html.template
	indexString   string
	indexTemplate = template.Must(template.Must(outlineTemplate.Clone()).New("body").Parse(indexString)).Lookup("outline")
)

func index(w http.ResponseWriter, _ *http.Request) {
	if err := indexTemplate.Execute(w, nil); err != nil {
		log.Printf("Warning: index: %s", err)
	}
}

var (
	//go:embed templates/pong.html.template
	pongString   string
	pongTemplate = template.Must(template.New("pong").Parse(pongString))
)

func pong(w http.ResponseWriter, _ *http.Request) {
	if err := pongTemplate.Execute(w, nil); err != nil {
		log.Printf("Warning: pong: %s", err)
	}
}

//go:embed static
var staticFiles embed.FS

func serveStaticFile(w http.ResponseWriter, r *http.Request, path string) {
	f, err := staticFiles.Open(path)
	if err != nil {
		notFound(w, r)
		return
	}
	defer f.Close()
	http.ServeContent(w, r, path, serverStartTime, f.(io.ReadSeeker))
}

func static(w http.ResponseWriter, r *http.Request) {
	serveStaticFile(w, r, strings.TrimPrefix(r.URL.Path, "/"))
}

func favicon(w http.ResponseWriter, r *http.Request) {
	serveStaticFile(w, r, "static/favicon.ico")
}

const cookieExpiration = 400 * 24 * time.Hour

func attachCookie(w http.ResponseWriter) error {
	nowBytes, err := time.Now().MarshalBinary()
	if err != nil {
		return err
	}
	mac := hmac.New(sha512.New512_256, privateKey)
	mac.Write(nowBytes)
	http.SetCookie(w, &http.Cookie{
		Name:        "auth",
		Value:       base64.RawStdEncoding.EncodeToString(append(nowBytes, mac.Sum(nil)...)),
		Path:        "/notepad",
		Expires:     time.Now().Add(cookieExpiration),
		Secure:      true,
		HttpOnly:    true,
		SameSite:    http.SameSiteStrictMode,
		Partitioned: true,
	})
	return nil
}

func cookieAuth(w http.ResponseWriter, r *http.Request) (string, bool) {
	const csrfTokenLen = 32

	cookie, err := r.Cookie("auth")
	if err != nil {
		return "", false
	}
	bytes, err := base64.RawStdEncoding.DecodeString(cookie.Value)
	if err != nil {
		return "", false
	}
	mac := hmac.New(sha512.New512_256, privateKey)
	macSize := mac.Size()
	if len(bytes) < macSize {
		return "", false
	}
	msg, sig := bytes[:len(bytes)-macSize], bytes[len(bytes)-macSize:]
	mac.Write(msg)
	if !hmac.Equal(sig, mac.Sum(nil)) {
		return "", false
	}
	var t time.Time
	if err := t.UnmarshalBinary(msg); err != nil {
		return "", false
	}
	if time.Since(t) > cookieExpiration {
		return "", false
	}
	attachCookie(w)

	if csrfToken, err := r.Cookie("csrf-token"); err == nil {
		return csrfToken.Value, true
	}
	buf := make([]byte, csrfTokenLen)
	rand.Read(buf)
	csrfToken := base64.RawStdEncoding.EncodeToString(buf)
	http.SetCookie(w, &http.Cookie{
		Name:        "csrf-token",
		Value:       csrfToken,
		Path:        "/notepad",
		Secure:      true,
		HttpOnly:    true,
		SameSite:    http.SameSiteStrictMode,
		Partitioned: true,
	})
	return csrfToken, true
}

var (
	//go:embed templates/login.html.template
	loginString   string
	loginTemplate = template.Must(template.Must(outlineTemplate.Clone()).New("body").Parse(loginString)).Lookup("outline")
)

type loginTemplateArgs struct {
	Error    bool
	Redirect string
}

func login(w http.ResponseWriter, r *http.Request) {
	key, pwHash, err := pwhash.Hash(r.FormValue("password"), notepadPasswordSalt)
	if err != nil {
		http.Error(w, fmt.Sprintf("Unable to hash password: %s", err), http.StatusInternalServerError)
		return
	}
	if subtle.ConstantTimeCompare(pwHash[:], notepadPassword) == 0 {
		if err := loginTemplate.Execute(w, loginTemplateArgs{Error: true, Redirect: r.FormValue("redirect")}); err != nil {
			log.Printf("Warning: login: %s", err)
		}
		return
	}
	encryptionKeyMu.Lock()
	if encryptionKey == nil {
		encryptionKey = key
	}
	encryptionKeyMu.Unlock()
	attachCookie(w)
	http.Redirect(w, r, r.FormValue("redirect"), http.StatusSeeOther)
}

func readNotepad(key []byte) (string, error) {
	encrypted, err := os.ReadFile(*notepadFile)
	if err != nil {
		return "", err
	}
	block, err := aes.NewCipher(key)
	if err != nil {
		return "", err
	}
	aead, err := cipher.NewGCMWithRandomNonce(block)
	if err != nil {
		return "", err
	}
	decrypted, err := aead.Open(nil, nil, encrypted, nil)
	if err != nil {
		return "", err
	}
	return string(decrypted), nil
}

var (
	//go:embed templates/note.html.template
	notepadString   string
	notepadTemplate = template.Must(template.Must(outlineTemplate.Clone()).New("body").Parse(notepadString)).Lookup("outline")
)

type notepadTemplateArgs struct {
	Content   string
	CSRFToken string
}

func notepad(w http.ResponseWriter, r *http.Request) {
	csrfToken, ok := cookieAuth(w, r)
	if !ok {
		if err := loginTemplate.Execute(w, loginTemplateArgs{Redirect: "/notepad"}); err != nil {
			log.Printf("Warning: login: %s", err)
		}
		return
	}
	encryptionKeyMu.Lock()
	key := encryptionKey
	encryptionKeyMu.Unlock()
	if key == nil {
		if err := loginTemplate.Execute(w, loginTemplateArgs{Redirect: "/notepad"}); err != nil {
			log.Printf("Warning: login: %s", err)
		}
		return
	}
	currentContent, err := readNotepad(key)
	if err != nil {
		currentContent = fmt.Sprintf("Error reading notepad file: %s", err)
	}
	if err := notepadTemplate.Execute(w, notepadTemplateArgs{Content: currentContent, CSRFToken: csrfToken}); err != nil {
		log.Printf("Warning: notepad: %s", err)
	}
}

func saveNote(w http.ResponseWriter, r *http.Request) error {
	if csrfToken, err := r.Cookie("csrf-token"); err != nil || subtle.ConstantTimeCompare([]byte(csrfToken.Value), []byte(r.FormValue("csrf-token"))) == 0 {
		return fmt.Errorf("bad CSRF token")
	}
	if _, ok := cookieAuth(w, r); !ok {
		return fmt.Errorf("not logged in")
	}
	encryptionKeyMu.Lock()
	key := encryptionKey
	encryptionKeyMu.Unlock()
	if key == nil {
		return fmt.Errorf("not logged in")
	}
	block, err := aes.NewCipher(key)
	if err != nil {
		return err
	}
	aead, err := cipher.NewGCMWithRandomNonce(block)
	if err != nil {
		return err
	}
	f, err := os.CreateTemp(filepath.Dir(*notepadFile), "notepad")
	if err != nil {
		return err
	}
	defer f.Close()
	if _, err = f.Write(aead.Seal(nil, nil, []byte(r.FormValue("content")), nil)); err != nil {
		return err
	}
	if err := f.Close(); err != nil {
		return err
	}
	return os.Rename(f.Name(), *notepadFile)
}

func autosave(w http.ResponseWriter, r *http.Request) {
	msg := "Saved ✓"
	if err := saveNote(w, r); err != nil {
		msg = fmt.Sprintf("Failed to save: %s", err)
	}
	if err := notepadTemplate.ExecuteTemplate(w, "saveIndicator", msg); err != nil {
		log.Printf("Warning: autosave: %s", err)
	}
}

func main() {
	flag.Parse()

	if err := loadSecrets(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	http.HandleFunc("GET /pong", pong)
	http.HandleFunc("POST /login", login)
	http.HandleFunc("GET /notepad", notepad)
	http.HandleFunc("POST /notepad/autosave", autosave)
	http.HandleFunc("GET /static/", static)
	http.HandleFunc("GET /favicon.ico", favicon)
	http.HandleFunc("GET /{$}", index)
	http.HandleFunc("GET /", notFound)

	addr := fmt.Sprintf(":%d", *port)
	log.Printf("Listening on %q", addr)
	log.Fatal(http.ListenAndServe(addr, nil))
}