-
Notifications
You must be signed in to change notification settings - Fork 92
Bound remote archive extraction resources #509
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
Open
GOLDKUN
wants to merge
3
commits into
chaitin:main
Choose a base branch
from
GOLDKUN:security/limit-import-resource-usage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
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
Some comments aren't visible on the classic Files Changed page.
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,88 @@ | ||
| package packageimport | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "archive/zip" | ||
| "bytes" | ||
| "compress/gzip" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestCopyWithByteLimitRejectsOversizedContent(t *testing.T) { | ||
| var dst bytes.Buffer | ||
| err := copyWithByteLimit(&dst, strings.NewReader("oversized"), 4) | ||
| if err == nil || !strings.Contains(err.Error(), "byte limit") { | ||
| t.Fatalf("copy limit error = %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestTarExtractionEnforcesExpandedSizeLimit(t *testing.T) { | ||
| archivePath := filepath.Join(t.TempDir(), "package.tgz") | ||
| file, err := os.Create(archivePath) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| gz := gzip.NewWriter(file) | ||
| tarWriter := tar.NewWriter(gz) | ||
| body := []byte("0123456789") | ||
| if err := tarWriter.WriteHeader(&tar.Header{Name: "package/data", Mode: 0o600, Size: int64(len(body))}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := tarWriter.Write(body); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := tarWriter.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := gz.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := file.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| err = untarGzWithLimits(archivePath, t.TempDir(), archiveExtractionLimits{ | ||
| MaxFiles: 10, | ||
| MaxEntryBytes: 5, | ||
| MaxTotalBytes: 5, | ||
| }) | ||
| if err == nil || !strings.Contains(err.Error(), "byte limit") { | ||
| t.Fatalf("tar limit error = %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestZipExtractionEnforcesFileCountLimit(t *testing.T) { | ||
| archivePath := filepath.Join(t.TempDir(), "package.zip") | ||
| file, err := os.Create(archivePath) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| zipWriter := zip.NewWriter(file) | ||
| for _, name := range []string{"package/a", "package/b"} { | ||
| entry, err := zipWriter.Create(name) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := entry.Write(bytes.Repeat([]byte{'x'}, 1)); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| if err := zipWriter.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := file.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| err = unzipWithLimits(archivePath, t.TempDir(), archiveExtractionLimits{ | ||
| MaxFiles: 1, | ||
| MaxEntryBytes: 10, | ||
| MaxTotalBytes: 10, | ||
| }) | ||
| if err == nil || !strings.Contains(err.Error(), "file limit") { | ||
| t.Fatalf("zip limit error = %v", err) | ||
| } | ||
| } | ||
|
monkeyscan[bot] marked this conversation as resolved.
|
||
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.
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.
资源限制错误信息缺少包来源上下文,故障定位困难
本次变更新增的运行时资源限制错误路径与同函数内其他错误路径不一致:downloadRemoteArchive 中 ContentLength 预检失败返回 "download remote package %q: archive exceeds ..." 带 URL 上下文,但 copyWithByteLimit 运行时超限返回的 "content exceeds ... byte limit" 被直接透传,未包装 source;同样,prepareRemoteArchiveSource 中 unzip/untarGz 返回的 "archive exceeds ... file limit" / "archive exceeds ... expanded byte limit" / "archive entry ... exceeds ... byte limit" 也未包装任何归档来源。当导入失败触发资源限制时,用户/运维无法从错误信息判断是哪个远程包或上传包导致的,增加了安全事件与故障排查成本。
Problem code:
Recommendation:
在 downloadRemoteArchive 中用 fmt.Errorf("download remote package %q: %w", redactedRemoteArchiveSource(source), err) 包装 copyWithByteLimit 的错误;在 prepareRemoteArchiveSource 中对 unzip/untarGz 的错误统一包装归档来源(如 redactedRemoteArchiveSource(source)),使所有限制错误都带可定位的来源上下文。
Suggested diff: