-
Notifications
You must be signed in to change notification settings - Fork 0
Always include license and readme files in packages #7
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,172 @@ | ||||||||||||||||||
| package packager | ||||||||||||||||||
|
|
||||||||||||||||||
| import ( | ||||||||||||||||||
| "archive/zip" | ||||||||||||||||||
| "io" | ||||||||||||||||||
| "os" | ||||||||||||||||||
| "path/filepath" | ||||||||||||||||||
| "strings" | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| // legalFilePatterns are base names (case-insensitive) of files that should always | ||||||||||||||||||
| // be included in the package root, regardless of exclude filters. | ||||||||||||||||||
| var legalFilePatterns = []string{ | ||||||||||||||||||
| "license", | ||||||||||||||||||
| "licence", | ||||||||||||||||||
| "license.md", | ||||||||||||||||||
| "licence.md", | ||||||||||||||||||
| "license.txt", | ||||||||||||||||||
| "licence.txt", | ||||||||||||||||||
| "license.rst", | ||||||||||||||||||
| "licence.rst", | ||||||||||||||||||
| "readme", | ||||||||||||||||||
| "readme.md", | ||||||||||||||||||
| "readme.txt", | ||||||||||||||||||
| "readme.rst", | ||||||||||||||||||
| "notice", | ||||||||||||||||||
| "notice.md", | ||||||||||||||||||
| "notice.txt", | ||||||||||||||||||
| "third-party-notices", | ||||||||||||||||||
| "third-party-notices.md", | ||||||||||||||||||
| "third-party-notices.txt", | ||||||||||||||||||
| "thirdpartynotices", | ||||||||||||||||||
| "thirdpartynotices.txt", | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // isLegalFile returns true if the filename (base name only) matches a known | ||||||||||||||||||
| // license/readme/notice pattern. | ||||||||||||||||||
| func isLegalFile(name string) bool { | ||||||||||||||||||
| lower := strings.ToLower(name) | ||||||||||||||||||
| for _, pattern := range legalFilePatterns { | ||||||||||||||||||
| if lower == pattern { | ||||||||||||||||||
| return true | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| return false | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // CopyLegalFiles copies license, readme, and notice files from srcDir (top-level only) | ||||||||||||||||||
| // to destDir, regardless of any exclude patterns. Files already present in destDir are | ||||||||||||||||||
| // not overwritten. | ||||||||||||||||||
| func CopyLegalFiles(srcDir, destDir string) error { | ||||||||||||||||||
| entries, err := os.ReadDir(srcDir) | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| return err | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| for _, entry := range entries { | ||||||||||||||||||
| if entry.IsDir() { | ||||||||||||||||||
| continue | ||||||||||||||||||
| } | ||||||||||||||||||
| if !isLegalFile(entry.Name()) { | ||||||||||||||||||
| continue | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| destPath := filepath.Join(destDir, entry.Name()) | ||||||||||||||||||
| // Don't overwrite if already present (e.g., copied by CopyFiltered) | ||||||||||||||||||
| if _, err := os.Stat(destPath); err == nil { | ||||||||||||||||||
| continue | ||||||||||||||||||
|
Comment on lines
+67
to
+68
|
||||||||||||||||||
| if _, err := os.Stat(destPath); err == nil { | |
| continue | |
| if _, err := os.Stat(destPath); err == nil { | |
| // File already exists at destPath; skip copying. | |
| continue | |
| } else if !os.IsNotExist(err) { | |
| // An unexpected error occurred while checking destPath; surface it. | |
| return err |
Copilot
AI
Mar 28, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ExtractLegalFilesFromZip joins destDir with f.Name without a zip-slip/path traversal guard. The current root-level check only looks for /, so names like ..\\LICENSE could escape destDir on Windows. Add a robust validation (e.g., reject any path separators, clean and verify the final path stays within destDir, similar to extractZip), before creating the output file.
Copilot
AI
Mar 28, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as CopyLegalFiles: the os.Stat overwrite check treats any error as “not present”. If Stat fails for reasons other than IsNotExist, return that error rather than continuing to create/truncate the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
legalFilePatternsdoesn’t fully match the filenames/extensions promised in the PR description (e.g.,NOTICE.rst,ThirdPartyNotices.md,ThirdPartyNotices.rst, andthird-party-notices.rstare missing). This will cause some legal files to be skipped. Expand the pattern list (or generate patterns programmatically) so all documented variants are recognized consistently.