| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "reasonix/internal/config" |
| 15 | ) |
| 16 | |
| 17 | type attachmentSecurityReader func([]byte) (int, error) |
| 18 | |
| 19 | func (read attachmentSecurityReader) Read(p []byte) (int, error) { return read(p) } |
| 20 | |
| 21 | func attachmentStorageDirectoryLink(t *testing.T, target, link string) { |
| 22 | t.Helper() |
| 23 | if runtime.GOOS == "windows" { |
| 24 | if out, err := exec.Command("cmd", "/c", "mklink", "/J", link, target).CombinedOutput(); err != nil { |
| 25 | t.Fatalf("create directory junction: %v: %s", err, out) |
| 26 | } |
| 27 | return |
| 28 | } |
| 29 | if err := os.Symlink(target, link); err != nil { |
| 30 | if errors.Is(err, errors.ErrUnsupported) { |
| 31 | t.Skipf("symlinks unavailable: %v", err) |
| 32 | } |
| 33 | t.Fatalf("create directory symlink: %v", err) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestAttachmentStorageRejectsSymlinkedReasonixParent(t *testing.T) { |
| 38 | attachmentStorageDirectoryLink(t, t.TempDir(), filepath.Join(t.TempDir(), "probe")) |
| 39 | for _, transport := range []string{"stream", "legacy-bytes"} { |
| 40 | t.Run(transport, func(t *testing.T) { |
| 41 | root, outside := t.TempDir(), t.TempDir() |
| 42 | attachmentStorageDirectoryLink(t, outside, filepath.Join(root, ".reasonix")) |
| 43 | var path string |
| 44 | var err error |
| 45 | if transport == "stream" { |
| 46 | path, err = SaveAttachmentStreamInRoot(context.Background(), root, "app.apk", strings.NewReader("apk bytes")) |
| 47 | } else { |
| 48 | path, err = SaveAttachmentBytesInRoot(root, "app.apk", []byte("apk bytes")) |
| 49 | } |
| 50 | if err == nil || path != "" { |
| 51 | t.Errorf("symlinked workspace storage accepted: path=%q error=%v", path, err) |
| 52 | } |
| 53 | entries, readErr := os.ReadDir(outside) |
| 54 | if readErr != nil { |
| 55 | t.Fatal(readErr) |
| 56 | } |
| 57 | if len(entries) != 0 { |
| 58 | t.Errorf("attachment creation escaped the workspace: outside entries=%v", entries) |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestAttachmentStorageCleanupKeepsOriginalDirectoryOwner(t *testing.T) { |
| 65 | attachmentStorageDirectoryLink(t, t.TempDir(), filepath.Join(t.TempDir(), "probe")) |
| 66 | for _, failure := range []string{"read-error", "cancel", "stream-limit"} { |
| 67 | t.Run(failure, func(t *testing.T) { |
| 68 | root, outside := t.TempDir(), t.TempDir() |
| 69 | directory := filepath.Join(root, ".reasonix", "attachments") |
| 70 | retained := filepath.Join(root, ".reasonix", "retained-attachments") |
| 71 | cleanupDirectory := retained |
| 72 | ctx, cancel := context.WithCancel(context.Background()) |
| 73 | defer cancel() |
| 74 | readFailure := errors.New("attachment source failed") |
| 75 | wantErr := readFailure |
| 76 | if failure == "cancel" { |
| 77 | wantErr = context.Canceled |
| 78 | } |
| 79 | const victimContent = "outside file must survive upload cleanup" |
| 80 | var basename string |
| 81 | reads := 0 |
| 82 | reader := attachmentSecurityReader(func(p []byte) (int, error) { |
| 83 | reads++ |
| 84 | if reads == 1 { |
| 85 | return copy(p, "partially uploaded apk"), nil |
| 86 | } |
| 87 | if reads > 2 { |
| 88 | return 0, io.EOF |
| 89 | } |
| 90 | entries, err := os.ReadDir(directory) |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | var partial []os.DirEntry |
| 95 | for _, entry := range entries { |
| 96 | if !entry.IsDir() && filepath.Ext(entry.Name()) == ".apk" { |
| 97 | partial = append(partial, entry) |
| 98 | } |
| 99 | } |
| 100 | if len(partial) != 1 { |
| 101 | t.Fatalf("expected one opened partial APK, got %v", entries) |
| 102 | } |
| 103 | basename = partial[0].Name() |
| 104 | partialBytes, err := os.ReadFile(filepath.Join(directory, basename)) |
| 105 | if err != nil || string(partialBytes) != "partially uploaded apk" { |
| 106 | t.Fatalf("partial bytes must be saved before replacement: bytes=%q error=%v", partialBytes, err) |
| 107 | } |
| 108 | if err := os.WriteFile(filepath.Join(outside, basename), []byte(victimContent), 0o600); err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | if err := os.Rename(directory, retained); err != nil { |
| 112 | if runtime.GOOS != "windows" || !errors.Is(err, os.ErrPermission) { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | // Windows may prevent replacement while the upload handle is open. |
| 116 | cleanupDirectory = directory |
| 117 | t.Log("open upload handle refused directory replacement") |
| 118 | } else { |
| 119 | attachmentStorageDirectoryLink(t, outside, directory) |
| 120 | } |
| 121 | if failure == "cancel" { |
| 122 | cancel() |
| 123 | } |
| 124 | if failure == "stream-limit" { |
| 125 | return copy(p, "x"), nil |
| 126 | } |
| 127 | return 0, wantErr |
| 128 | }) |
| 129 | |
| 130 | limit := (*config.Config)(nil).AttachmentStreamLimit() |
| 131 | if failure == "stream-limit" { |
| 132 | limit = config.AttachmentLimit{Bytes: int64(len("partially uploaded apk")), Source: "user"} |
| 133 | } |
| 134 | path, err := SaveAttachmentStreamInRoot(ctx, root, "app.apk", reader, limit) |
| 135 | matches := errors.Is(err, wantErr) |
| 136 | if failure == "stream-limit" { |
| 137 | var limitErr *AttachmentStreamLimitError |
| 138 | matches = errors.As(err, &limitErr) && limitErr.LimitBytes == limit.Bytes && limitErr.LimitSource == "user" |
| 139 | } |
| 140 | if path != "" || !matches { |
| 141 | t.Errorf("failed upload result: path=%q error=%v; want %v", path, err, wantErr) |
| 142 | } |
| 143 | if reads != 2 || basename == "" { |
| 144 | t.Fatalf("replacement did not run after the file opened: reads=%d name=%q", reads, basename) |
| 145 | } |
| 146 | victim, err := os.ReadFile(filepath.Join(outside, basename)) |
| 147 | if err != nil || string(victim) != victimContent { |
| 148 | t.Errorf("cleanup touched an outside file: bytes=%q error=%v", victim, err) |
| 149 | } |
| 150 | if _, err := os.Lstat(filepath.Join(cleanupDirectory, basename)); !errors.Is(err, os.ErrNotExist) { |
| 151 | t.Errorf("cleanup did not remove the owned partial file: %v", err) |
| 152 | } |
| 153 | }) |
| 154 | } |
| 155 | } |
| 156 |