| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "errors" |
| 7 | "io" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | type interruptedAttachmentReader struct{} |
| 12 | |
| 13 | func (interruptedAttachmentReader) Read(p []byte) (int, error) { |
| 14 | copy(p, "partial") |
| 15 | return 7, errors.New("source interrupted") |
| 16 | } |
| 17 | |
| 18 | func TestWindowsAttachmentFramesRequireCompleteSource(t *testing.T) { |
| 19 | for _, input := range []io.Reader{bytes.NewReader([]byte("hello")), bytes.NewReader(nil), interruptedAttachmentReader{}} { |
| 20 | var wire bytes.Buffer |
| 21 | err := writeWindowsAttachmentFrames(&wire, input) |
| 22 | data := wire.Bytes() |
| 23 | var content bytes.Buffer |
| 24 | complete := false |
| 25 | for len(data) >= 4 { |
| 26 | n := int(binary.LittleEndian.Uint32(data[:4])) |
| 27 | data = data[4:] |
| 28 | if n == 0 { |
| 29 | complete = true |
| 30 | break |
| 31 | } |
| 32 | if n > 64<<10 || n > len(data) { |
| 33 | t.Fatal("invalid frame") |
| 34 | } |
| 35 | content.Write(data[:n]) |
| 36 | data = data[n:] |
| 37 | } |
| 38 | if complete != (err == nil) { |
| 39 | t.Fatal("an interrupted transfer must not contain a completion marker") |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestWindowsAttachmentPathConfinedToGeneratedDirectory(t *testing.T) { |
| 45 | valid := `C:\Users\CageLabSetup\Cage\Attachments\0123456789abcdef0123456789abcdef\中文 O'Brien.txt` |
| 46 | if !validWindowsAttachmentPath(valid, "中文 O'Brien.txt") { |
| 47 | t.Fatal("valid guest path rejected") |
| 48 | } |
| 49 | for _, path := range []string{`C:\Users\Cage\secret.txt`, `C:\Users\Cage\Cage\Attachments\..\secret.txt`, `\\server\Cage\Attachments\0123456789abcdef0123456789abcdef\中文 O'Brien.txt`, valid + ":stream", valid + "\n"} { |
| 50 | if validWindowsAttachmentPath(path, "中文 O'Brien.txt") { |
| 51 | t.Fatalf("unsafe response accepted: %q", path) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestWindowsChooserAttachmentPathRetainsNameInPrivateChild(t *testing.T) { |
| 57 | const batch = "0123456789abcdef0123456789abcdef" |
| 58 | const file = "fedcba9876543210fedcba9876543210" |
| 59 | base := `C:\Users\guest\Cage\Attachments\` + batch + `\` |
| 60 | if !validWindowsAttachmentPath(base+file+`\same.txt`, "same.txt") { |
| 61 | t.Fatal("fixed chooser file directory rejected") |
| 62 | } |
| 63 | for _, path := range []string{ |
| 64 | base + `ordinary\same.txt`, |
| 65 | base + file + `\` + file + `\same.txt`, |
| 66 | base + file + `\different.txt`, |
| 67 | base + `..\same.txt`, |
| 68 | base + file + `\same.txt:extra`, |
| 69 | } { |
| 70 | if validWindowsAttachmentPath(path, "same.txt") { |
| 71 | t.Fatalf("unconfined chooser attachment accepted: %q", path) |
| 72 | } |
| 73 | } |
| 74 | } |