Repository navigation
Open only viewable attachments, and mark every download #560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| //go:build darwin | ||
|
|
||
| package attachments | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| // markDownloaded sets the quarantine flag a browser sets on a download, so | ||
| // macOS asks before opening a file that can run a program. | ||
| func markDownloaded(path string) error { | ||
| value := fmt.Sprintf("0081;%08x;HEY CLI;", time.Now().Unix()) | ||
| return unix.Setxattr(path, "com.apple.quarantine", []byte(value), 0) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //go:build !darwin && !windows | ||
|
|
||
| package attachments | ||
|
|
||
| // markDownloaded does nothing where the system keeps no record of where a | ||
| // file came from. Saved attachments are still never executable. | ||
| func markDownloaded(string) error { | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| //go:build windows | ||
|
|
||
| package attachments | ||
|
|
||
| import "os" | ||
|
|
||
| // markDownloaded records that the file came from the internet, as a browser | ||
| // does, so Windows warns before running it. Volumes without alternate data | ||
| // streams, such as FAT drives, can't carry the mark; the file is saved anyway, | ||
| // as a browser would save it. | ||
| func markDownloaded(path string) error { | ||
| _ = os.WriteFile(path+":Zone.Identifier", []byte("[ZoneTransfer]\r\nZoneId=3\r\n"), 0o600) | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| //go:build darwin | ||
|
|
||
| package attachments | ||
|
|
||
| import ( | ||
| "context" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| func TestSaveSetsQuarantineOnDownloadedAttachment(t *testing.T) { | ||
| destination := filepath.Join(t.TempDir(), "Invoice.terminal") | ||
| if _, err := Save(context.Background(), writeDownload("<plist/>"), destination, "/rails/active_storage/blobs/invoice", false); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| value := make([]byte, 256) | ||
| size, err := unix.Getxattr(destination, "com.apple.quarantine", value) | ||
| if err != nil { | ||
| t.Fatalf("saved attachment has no quarantine flag: %v", err) | ||
| } | ||
| flag := string(value[:size]) | ||
| if !strings.HasPrefix(flag, "0081;") || !strings.HasSuffix(flag, ";HEY CLI;") { | ||
| t.Errorf("quarantine flag = %q", value[:size]) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //go:build unix | ||
|
|
||
| package attachments | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestSavedAttachmentIsNeverExecutable(t *testing.T) { | ||
| destination := filepath.Join(t.TempDir(), "install.sh") | ||
| if _, err := Save(context.Background(), writeDownload("#!/bin/sh\n"), destination, "/rails/active_storage/blobs/install", false); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| info, err := os.Stat(destination) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if info.Mode().Perm()&0o111 != 0 { | ||
| t.Errorf("saved attachment mode = %v, want no execute bits", info.Mode().Perm()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package attachments | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // viewableExtensions are the kinds of file the system hands to a document, | ||
| // image or media viewer. Anything else might be run rather than shown: a | ||
| // Terminal profile, a script, a shortcut, an installer. The list is closed on | ||
| // purpose, because the kinds of file an operating system will run keep growing. | ||
| var viewableExtensions = map[string]bool{ | ||
| // Documents and text | ||
| ".pdf": true, ".txt": true, ".md": true, ".csv": true, ".rtf": true, | ||
| ".docx": true, ".xlsx": true, ".pptx": true, | ||
| ".odt": true, ".ods": true, ".odp": true, | ||
| ".pages": true, ".numbers": true, ".key": true, | ||
| ".ics": true, | ||
|
|
||
| // Images | ||
| ".png": true, ".jpg": true, ".jpeg": true, ".gif": true, ".webp": true, | ||
| ".heic": true, ".heif": true, ".bmp": true, ".tif": true, ".tiff": true, | ||
|
|
||
| // Audio and video | ||
| ".mp3": true, ".m4a": true, ".wav": true, ".aac": true, ".flac": true, ".ogg": true, | ||
| ".mp4": true, ".m4v": true, ".mov": true, ".webm": true, | ||
| } | ||
|
|
||
| // viewableContentTypes are the media types, as a content sniffer names them, | ||
| // of the same documents, images and media. Some systems pick the application | ||
| // for a file from what is in it rather than from its name, so a Windows | ||
| // program called report.pdf reaches whatever runs Windows programs. Both | ||
| // xdg-mime backends' spellings are here: file(1)'s and shared-mime-info's. | ||
| var viewableContentTypes = map[string]bool{ | ||
| // Documents and text | ||
| "application/pdf": true, "text/plain": true, "text/markdown": true, "text/x-markdown": true, | ||
| "text/csv": true, "application/rtf": true, "text/rtf": true, "text/calendar": true, | ||
| "application/vnd.openxmlformats-officedocument.wordprocessingml.document": true, | ||
| "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": true, | ||
| "application/vnd.openxmlformats-officedocument.presentationml.presentation": true, | ||
| "application/vnd.oasis.opendocument.text": true, | ||
| "application/vnd.oasis.opendocument.spreadsheet": true, | ||
| "application/vnd.oasis.opendocument.presentation": true, | ||
| "application/vnd.apple.pages": true, "application/vnd.apple.numbers": true, "application/vnd.apple.keynote": true, | ||
|
|
||
| // Images | ||
| "image/png": true, "image/jpeg": true, "image/gif": true, "image/webp": true, | ||
| "image/heic": true, "image/heif": true, "image/bmp": true, "image/x-ms-bmp": true, "image/tiff": true, | ||
|
|
||
| // Audio and video | ||
| "audio/mpeg": true, "audio/mp4": true, "audio/x-m4a": true, "audio/wav": true, "audio/x-wav": true, | ||
| "audio/vnd.wave": true, "audio/aac": true, "audio/x-aac": true, "audio/x-hx-aac-adts": true, | ||
| "audio/flac": true, "audio/x-flac": true, "audio/ogg": true, "audio/x-vorbis+ogg": true, | ||
| "audio/x-opus+ogg": true, "application/ogg": true, | ||
| "video/mp4": true, "video/x-m4v": true, "video/quicktime": true, "video/webm": true, | ||
| } | ||
|
|
||
| // zipDocumentExtensions are documents stored as zip archives. A sniffer that | ||
| // cannot tell one from a plain archive calls it application/zip, which then | ||
| // opens in an archive manager rather than anything that runs it. | ||
| var zipDocumentExtensions = map[string]bool{ | ||
| ".docx": true, ".xlsx": true, ".pptx": true, | ||
| ".odt": true, ".ods": true, ".odp": true, | ||
| ".pages": true, ".numbers": true, ".key": true, | ||
| } | ||
|
|
||
| // OpensInViewer reports whether an attachment called filename is a kind of | ||
| // file the system shows in a viewer, so it is safe to open directly. Other | ||
| // attachments can still be saved and opened deliberately. | ||
| func OpensInViewer(filename string) bool { | ||
| return viewableExtensions[portableExtension(filename)] | ||
| } | ||
|
|
||
| // ContentOpensInViewer reports whether a file called filename whose contents | ||
| // a sniffer named contentType is still a document, image or media file, so an | ||
| // opener that chooses by contents shows it rather than runs it. | ||
| func ContentOpensInViewer(filename, contentType string) bool { | ||
| if !OpensInViewer(filename) { | ||
| return false | ||
| } | ||
| contentType = strings.ToLower(strings.TrimSpace(contentType)) | ||
| if contentType == "application/zip" { | ||
| return zipDocumentExtensions[portableExtension(filename)] | ||
| } | ||
| return viewableContentTypes[contentType] | ||
| } | ||
|
|
||
| func portableExtension(filename string) string { | ||
| portable, err := PortableFilename(filename) | ||
| if err != nil { | ||
| return "" | ||
| } | ||
| return strings.ToLower(filepath.Ext(portable)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package attachments | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestOpensInViewerOnlyForDocumentsImagesAndMedia(t *testing.T) { | ||
| for filename, want := range map[string]bool{ | ||
| "agenda.pdf": true, | ||
| "Chart.PNG": true, | ||
| "minutes.docx": true, | ||
| "invite.ics": true, | ||
| "recording.m4a": true, | ||
| "../../notes.txt": true, | ||
| "Invoice.terminal": false, | ||
| "setup.command": false, | ||
| "report.pdf.command": false, | ||
| "report.pdf.terminal": false, | ||
| "Invoice.terminal. ": false, | ||
| "Shortcut.webloc": false, | ||
| "Server.inetloc": false, | ||
| "Folder.fileloc": false, | ||
| "Script.scpt": false, | ||
| "Installer.pkg": false, | ||
| "Disk.dmg": false, | ||
| "launcher.desktop": false, | ||
| "install.sh": false, | ||
| "tool.AppImage": false, | ||
| "invoice.exe": false, | ||
| "invoice.bat": false, | ||
| "invoice.cmd": false, | ||
| "invoice.lnk": false, | ||
| "invoice.url": false, | ||
| "invoice.hta": false, | ||
| "invoice.js": false, | ||
| "invoice.vbs": false, | ||
| "invoice.ps1": false, | ||
| "invoice.msi": false, | ||
| "macro.docm": false, | ||
| "page.html": false, | ||
| "drawing.svg": false, | ||
| "archive.zip": false, | ||
| "README": false, | ||
| "": false, | ||
| "..": false, | ||
| } { | ||
| if got := OpensInViewer(filename); got != want { | ||
| t.Errorf("OpensInViewer(%q) = %v, want %v", filename, got, want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestContentOpensInViewerOnlyWhenTheContentsAgree(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| filename, contentType string | ||
| want bool | ||
| }{ | ||
| {"agenda.pdf", "application/pdf", true}, | ||
| {"notes.md", "text/plain", true}, | ||
| {"Chart.PNG", " Image/PNG ", true}, | ||
| {"photo.jpg", "image/png", true}, | ||
| {"minutes.docx", "application/zip", true}, | ||
| {"budget.ods", "application/zip", true}, | ||
| {"agenda.pdf", "application/vnd.microsoft.portable-executable", false}, | ||
| {"agenda.md", "application/x-ms-dos-executable", false}, | ||
| {"notes.txt", "application/x-shellscript", false}, | ||
| {"notes.txt", "text/x-script.python", false}, | ||
| {"photo.jpg", "application/zip", false}, | ||
| {"photo.png", "image/svg+xml", false}, | ||
| {"photo.png", "application/x-desktop", false}, | ||
| {"photo.png", "application/octet-stream", false}, | ||
| {"photo.png", "", false}, | ||
| {"Invoice.terminal", "text/plain", false}, | ||
| {"report.pdf.command", "application/pdf", false}, | ||
| } { | ||
| if got := ContentOpensInViewer(tc.filename, tc.contentType); got != tc.want { | ||
| t.Errorf("ContentOpensInViewer(%q, %q) = %v, want %v", tc.filename, tc.contentType, got, tc.want) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.