| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/base64" |
| 6 | "encoding/binary" |
| 7 | "encoding/json" |
| 8 | "hash/crc32" |
| 9 | "image" |
| 10 | "image/png" |
| 11 | "io" |
| 12 | "net/http" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
| 17 | func testPNG(t *testing.T) string { |
| 18 | t.Helper() |
| 19 | var data bytes.Buffer |
| 20 | if err := png.Encode(&data, image.NewNRGBA(image.Rect(0, 0, 2, 2))); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | return base64.StdEncoding.EncodeToString(data.Bytes()) |
| 24 | } |
| 25 | |
| 26 | func TestInputImageValidation(t *testing.T) { |
| 27 | valid := testPNG(t) |
| 28 | if _, err := decodeInputImage(valid); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | for _, input := range []string{"", "!", base64.StdEncoding.EncodeToString([]byte("not PNG")), valid[:len(valid)-16], strings.Repeat("A", base64.StdEncoding.EncodedLen(maxImageBytes)+4)} { |
| 32 | if _, err := decodeInputImage(input); err == nil { |
| 33 | t.Fatal("invalid image accepted") |
| 34 | } |
| 35 | } |
| 36 | oversized, _ := base64.StdEncoding.DecodeString(valid) |
| 37 | binary.BigEndian.PutUint32(oversized[16:20], 40_000_001) |
| 38 | binary.BigEndian.PutUint32(oversized[29:33], crc32.ChecksumIEEE(oversized[12:29])) |
| 39 | if _, err := decodeInputImage(base64.StdEncoding.EncodeToString(oversized)); err == nil { |
| 40 | t.Fatal("oversized dimensions accepted") |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestImageInputAuthenticationAndDestination(t *testing.T) { |
| 45 | s := testServer(t) |
| 46 | s.ready = true |
| 47 | s.credentials.Token = "guest-test-token" |
| 48 | calls := 0 |
| 49 | s.browserControl = browserClientForTest(t, s, browserTransport(func(r *http.Request) (*http.Response, error) { |
| 50 | calls++ |
| 51 | if r.URL.Path != "/paste-image" || r.Header.Get("Authorization") != "Bearer guest-test-token" { |
| 52 | t.Fatal("wrong destination or credentials") |
| 53 | } |
| 54 | var input map[string]string |
| 55 | if err := json.NewDecoder(r.Body).Decode(&input); err != nil || input["id"] != "tab-1" || input["png"] != testPNG(t) { |
| 56 | t.Fatal("image or target changed", err) |
| 57 | } |
| 58 | return &http.Response{StatusCode: 200, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(`{"ok":true}`))}, nil |
| 59 | })) |
| 60 | body := `{"target":"browser","id":"tab-1","png":"` + testPNG(t) + `"}` |
| 61 | path := "/api/input/image" |
| 62 | if w := request(s, "POST", path, s.origin, body, false); w.Code != 401 { |
| 63 | t.Fatal(w.Code) |
| 64 | } |
| 65 | if w := request(s, "POST", path, "https://example.com", body, true); w.Code != 403 { |
| 66 | t.Fatal(w.Code) |
| 67 | } |
| 68 | for _, bad := range []string{`{"target":"browser","png":"` + testPNG(t) + `"}`, `{"target":"host","id":"tab-1"}`, `{"target":"terminal","id":"missing","png":"` + testPNG(t) + `"}`, `{"target":"browser","id":"tab-1","png":"broken"}`} { |
| 69 | if w := request(s, "POST", path, s.origin, bad, true); w.Code < 400 { |
| 70 | t.Fatal("invalid image accepted") |
| 71 | } |
| 72 | } |
| 73 | if calls != 0 { |
| 74 | t.Fatal("invalid input reached guest") |
| 75 | } |
| 76 | if w := request(s, "POST", path, s.origin, body, true); w.Code != 200 || calls != 1 { |
| 77 | t.Fatal(w.Code, w.Body.String()) |
| 78 | } |
| 79 | s.ready = false |
| 80 | if w := request(s, "POST", path, s.origin, body, true); w.Code != 409 || calls != 1 { |
| 81 | t.Fatal("offline input accepted") |
| 82 | } |
| 83 | s.ready = true |
| 84 | s.account.lock() |
| 85 | if w := request(s, "POST", path, s.origin, body, true); w.Code != 423 || calls != 1 { |
| 86 | t.Fatal("locked account accepted an image") |
| 87 | } |
| 88 | } |