| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | type attachmentPatternReader struct { |
| 14 | remaining int64 |
| 15 | maxRead int |
| 16 | } |
| 17 | |
| 18 | func (r *attachmentPatternReader) Read(p []byte) (int, error) { |
| 19 | if len(p) > r.maxRead { |
| 20 | r.maxRead = len(p) |
| 21 | } |
| 22 | if r.remaining == 0 { |
| 23 | return 0, io.EOF |
| 24 | } |
| 25 | n := int(min(int64(len(p)), r.remaining)) |
| 26 | for i := range p[:n] { |
| 27 | p[i] = byte(i % 251) |
| 28 | } |
| 29 | r.remaining -= int64(n) |
| 30 | return n, nil |
| 31 | } |
| 32 | |
| 33 | func TestSaveAttachmentStreamLargeFileUsesBoundedReads(t *testing.T) { |
| 34 | root := t.TempDir() |
| 35 | const size = 80 << 20 |
| 36 | reader := &attachmentPatternReader{remaining: size} |
| 37 | path, err := SaveAttachmentStreamInRoot(context.Background(), root, "base.apk", reader) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | info, err := os.Stat(filepath.Join(root, filepath.FromSlash(path))) |
| 42 | if err != nil || info.Size() != size || !strings.HasSuffix(path, ".apk") { |
| 43 | t.Fatalf("file=%v err=%v path=%s", info, err, path) |
| 44 | } |
| 45 | if reader.maxRead > 64<<10 { |
| 46 | t.Fatalf("unbounded read: %d", reader.maxRead) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | type failingAttachmentReader struct{ cancel context.CancelFunc } |
| 51 | |
| 52 | func (r *failingAttachmentReader) Read(p []byte) (int, error) { |
| 53 | if r.cancel != nil { |
| 54 | r.cancel() |
| 55 | return copy(p, "partial"), nil |
| 56 | } |
| 57 | return 0, io.ErrUnexpectedEOF |
| 58 | } |
| 59 | |
| 60 | func TestSaveAttachmentStreamCleansInterruptedWrites(t *testing.T) { |
| 61 | for _, cancelled := range []bool{false, true} { |
| 62 | t.Run(map[bool]string{false: "read failure", true: "cancelled"}[cancelled], func(t *testing.T) { |
| 63 | root := t.TempDir() |
| 64 | ctx, cancel := context.WithCancel(context.Background()) |
| 65 | defer cancel() |
| 66 | reader := &failingAttachmentReader{} |
| 67 | want := io.ErrUnexpectedEOF |
| 68 | if cancelled { |
| 69 | reader.cancel = cancel |
| 70 | want = context.Canceled |
| 71 | } |
| 72 | if _, err := SaveAttachmentStreamInRoot(ctx, root, "bad.apk", reader); !errors.Is(err, want) { |
| 73 | t.Fatalf("err=%v", err) |
| 74 | } |
| 75 | entries, err := os.ReadDir(filepath.Join(root, ".reasonix", "attachments")) |
| 76 | if err != nil || len(entries) != 0 { |
| 77 | t.Fatalf("partial files=%v err=%v", entries, err) |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestSaveAttachmentStreamRejectsEmptyFile(t *testing.T) { |
| 84 | root := t.TempDir() |
| 85 | if _, err := SaveAttachmentStreamInRoot(context.Background(), root, "empty.apk", strings.NewReader("")); err == nil { |
| 86 | t.Fatal("accepted empty file") |
| 87 | } |
| 88 | entries, err := os.ReadDir(filepath.Join(root, ".reasonix", "attachments")) |
| 89 | if err != nil || len(entries) != 0 { |
| 90 | t.Fatalf("files=%v err=%v", entries, err) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestExternalFileReferenceDoesNotReadOrAuthorizeParent(t *testing.T) { |
| 95 | root := t.TempDir() |
| 96 | path := filepath.Join(t.TempDir(), "large app.apk") |
| 97 | f, err := os.Create(path) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | if err := f.Truncate(120 << 20); err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | if err := f.Close(); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | c := newOwnedTestController(t, Options{WorkspaceRoot: root}) |
| 108 | token, display, err := c.RegisterExternalFileRef(path) |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | realPath, err := filepath.EvalSymlinks(path) |
| 113 | if err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | if display != filepath.ToSlash(realPath) { |
| 117 | t.Fatalf("display=%q", display) |
| 118 | } |
| 119 | block, errs := c.ResolveRefs(context.Background(), "inspect @"+EscapeRefPath(token)) |
| 120 | if len(errs) != 0 || !strings.Contains(block, "binary file") || len(block) > 4096 { |
| 121 | t.Fatalf("block=%q errs=%v", block, errs) |
| 122 | } |
| 123 | for _, sub := range []string{"/../secret", "/other.apk", "/.."} { |
| 124 | if _, ok := c.externalFolderRef(token + sub); ok { |
| 125 | t.Fatalf("file reference authorized subpath: %s", sub) |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 |