Skip to content

Commit 3890b5f

Browse files
committed
lose hardcoded blob key
might want to load from environment instead to make the blobs survive restart
1 parent 64ae963 commit 3890b5f

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

serve/blob_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ func TestBlob(t *testing.T) {
99
UserID: 1234,
1010
InlineMessageID: "I'm a rather less regular ID than will be in there.",
1111
}
12+
k := genKey()
1213

13-
if bb, err := decode(encode(b)); err != nil {
14+
if bb, err := decode(encode(b, k), k); err != nil {
1415
t.Fatal(err)
1516
} else if b != bb {
1617
t.Errorf("have %v, want %v", bb, b)
1718
}
1819
}
1920

2021
func TestBlobShort(t *testing.T) {
21-
_, err := decode("short")
22+
k := genKey()
23+
_, err := decode("short", k)
2224
if err == nil {
2325
t.Error("expected error")
2426
}

serve/main.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ var (
4242
func main() {
4343
flag.Parse()
4444

45+
blobKey := genKey()
46+
4547
actions := make(chan BotAction)
4648

4749
if *bot {
4850
var callbacks []CallbackHandler
4951
for _, g := range games {
50-
callbacks = append(callbacks, handleGame(g, *baseURL))
52+
callbacks = append(callbacks, handleGame(g, *baseURL, blobKey))
5153
}
5254
for _, g := range multigames {
5355
callbacks = append(callbacks, handleMultiGame(g, *baseURL))
@@ -56,18 +58,18 @@ func main() {
5658
}
5759

5860
log.Printf("listening on %s...\n", *listen)
59-
log.Fatal(http.ListenAndServe(*listen, mux(actions, *static)))
61+
log.Fatal(http.ListenAndServe(*listen, mux(actions, *static, blobKey)))
6062
}
6163

62-
func mux(actions chan<- BotAction, static string) *httprouter.Router {
64+
func mux(actions chan<- BotAction, static string, blobKey [32]byte) *httprouter.Router {
6365
r := httprouter.New()
6466
if static != "" {
6567
r.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
6668
http.ServeFile(w, r, static+"index.html")
6769
})
6870
r.ServeFiles("/static/*filepath", http.Dir(static))
6971
}
70-
r.GET("/api/win", winHandler(actions))
72+
r.GET("/api/win", winHandler(actions, blobKey))
7173
r.GET("/api/join", multiHandler(newRooms()))
7274
return r
7375
}
@@ -177,11 +179,7 @@ type Blob struct {
177179
InlineMessageID string `json:"iid,omitempty"`
178180
}
179181

180-
var (
181-
key = [32]byte{0x49, 0xf3, 0xae, 0x3f, 0x82, 0x26, 0x72, 0x6d, 0xf4, 0x5c, 0xf4, 0x3c, 0x36, 0x66, 0x12, 0xdf, 0x8a, 0xc1, 0x2b, 0xe9, 0x94, 0x87, 0x92, 0x47, 0x8e, 0xfa, 0xcf, 0xb9, 0xcc, 0x77, 0xf7, 0x3d}
182-
)
183-
184-
func encode(b Blob) string {
182+
func encode(b Blob, key [32]byte) string {
185183
js, err := json.Marshal(b)
186184
if err != nil {
187185
panic(err)
@@ -197,7 +195,7 @@ func encode(b Blob) string {
197195
return base64.RawURLEncoding.EncodeToString(bs)
198196
}
199197

200-
func decode(s string) (Blob, error) {
198+
func decode(s string, key [32]byte) (Blob, error) {
201199
bs, err := base64.RawURLEncoding.DecodeString(s)
202200
if err != nil {
203201
return Blob{}, err
@@ -214,7 +212,16 @@ func decode(s string) (Blob, error) {
214212
return b, json.Unmarshal(js, &b)
215213
}
216214

217-
func handleGame(shortname, u string) CallbackHandler {
215+
func genKey() [32]byte {
216+
var key [32]byte
217+
_, err := rand.Read(key[:])
218+
if err != nil {
219+
panic(err)
220+
}
221+
return key
222+
}
223+
224+
func handleGame(shortname, u string, key [32]byte) CallbackHandler {
218225
return func(q *tgbotapi.CallbackQuery) *tgbotapi.CallbackConfig {
219226
if g := q.GameShortName; g != shortname {
220227
return nil
@@ -231,7 +238,7 @@ func handleGame(shortname, u string) CallbackHandler {
231238
b.MessageID = msg.MessageID
232239
b.ChatID = msg.Chat.ID
233240
}
234-
key := encode(b)
241+
key := encode(b, key)
235242
var v = url.Values{}
236243
v.Add("game", shortname)
237244
v.Add("scored", "1")
@@ -282,7 +289,7 @@ func sendScore(blob Blob, score int) BotAction {
282289
}
283290
}
284291

285-
func winHandler(actions chan<- BotAction) httprouter.Handle {
292+
func winHandler(actions chan<- BotAction, blobKey [32]byte) httprouter.Handle {
286293
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
287294
key := r.FormValue("key")
288295
if key == "" {
@@ -294,7 +301,7 @@ func winHandler(actions chan<- BotAction) httprouter.Handle {
294301
http.Error(w, "missing/bad parameter `score`", http.StatusBadRequest)
295302
return
296303
}
297-
blob, err := decode(key)
304+
blob, err := decode(key, blobKey)
298305
if err != nil {
299306
log.Printf("decoding blob %q: %s", key, err)
300307
http.Error(w, "bad key", http.StatusBadRequest)

0 commit comments

Comments
 (0)