Validate Audita report bounds with constant memory
This commit is contained in:
@@ -187,7 +187,13 @@ benchmark or a real audio workload is unnecessary to establish the complexity.
|
||||
|
||||
Priority: P3
|
||||
|
||||
Status: Pending consideration
|
||||
Status: Complete
|
||||
|
||||
Both Audita report boundaries now use a shared streaming bounded-file check
|
||||
with constant auxiliary memory. Existing regular-file/path guards and the
|
||||
central report limit remain intact. Focused file-operation, adapter, and stage
|
||||
tests passed, including below/at/above limits, invalid paths, optional reports,
|
||||
and preservation of the original subprocess failure.
|
||||
|
||||
Affected stages: 10 and 11, principally the new polish-stage report check.
|
||||
|
||||
@@ -263,8 +269,9 @@ scope. They are not findings or reasons to restore the earlier behavior.
|
||||
## Algorithmic review coverage
|
||||
|
||||
The follow-up review used Narratio `54ba768a28eee317985b0be4bb284d5f77ff00f1`
|
||||
and the same implementation and catalog revisions described above. Findings 1
|
||||
and 2 remain pending; Findings 3 and 4 are lower-priority refactoring opportunities.
|
||||
and the same implementation and catalog revisions described above. At review
|
||||
time, Findings 1 and 2 were pending and Findings 3 and 4 were lower-priority
|
||||
refactoring opportunities. Completion notes above record their resolution.
|
||||
No additional ordinary correctness bug was found in this pass.
|
||||
|
||||
| Stage | Algorithmic assessment |
|
||||
|
||||
@@ -190,7 +190,7 @@ func discoverReports(reportDir string) (ReportBundle, error) {
|
||||
if !info.Mode().IsRegular() {
|
||||
return reports, fmt.Errorf("audita report %q is not a regular file", path)
|
||||
}
|
||||
if _, err := fileops.ReadFileBounded(path, MaxReportOutputBytes); err != nil {
|
||||
if err := fileops.CheckFileBounded(path, MaxReportOutputBytes); err != nil {
|
||||
return reports, fmt.Errorf("audita report %q exceeds or cannot be read within %d-byte limit: %w", path, MaxReportOutputBytes, err)
|
||||
}
|
||||
*report.dest = path
|
||||
|
||||
@@ -8,6 +8,34 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CheckFileBounded verifies readability and the byte limit without retaining
|
||||
// contents. Callers own any additional path or regular-file requirements.
|
||||
func CheckFileBounded(path string, limit int64) error {
|
||||
if strings.TrimSpace(path) == "" {
|
||||
return fmt.Errorf("file path is required")
|
||||
}
|
||||
if limit < 0 {
|
||||
return fmt.Errorf("file read limit %d is invalid", limit)
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open file %q: %w", path, err)
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
readLimit := limit
|
||||
if limit < math.MaxInt64 {
|
||||
readLimit++
|
||||
}
|
||||
size, err := io.Copy(io.Discard, io.LimitReader(file, readLimit))
|
||||
if err != nil {
|
||||
return fmt.Errorf("read file %q: %w", path, err)
|
||||
}
|
||||
if size > limit {
|
||||
return fmt.Errorf("file %q exceeds %d-byte limit", path, limit)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadFileBounded reads at most limit bytes from path using ordinary workspace
|
||||
// path semantics. It does not return partial data when the limit is exceeded.
|
||||
func ReadFileBounded(path string, limit int64) ([]byte, error) {
|
||||
|
||||
@@ -8,6 +8,42 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCheckFileBounded(t *testing.T) {
|
||||
const limit int64 = 4
|
||||
for _, tc := range []struct {
|
||||
name, content string
|
||||
limit int64
|
||||
valid bool
|
||||
}{
|
||||
{"below", "123", limit, true},
|
||||
{"at", "1234", limit, true},
|
||||
{"above", "12345", limit, false},
|
||||
{"empty", "", 0, true},
|
||||
{"zero", "1", 0, false},
|
||||
{"maximum", "1234", math.MaxInt64, true},
|
||||
{"negative", "", -1, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "report")
|
||||
if err := os.WriteFile(path, []byte(tc.content), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := CheckFileBounded(path, tc.limit)
|
||||
if (err == nil) != tc.valid {
|
||||
t.Fatalf("check: %v, want valid=%t", err, tc.valid)
|
||||
}
|
||||
if tc.name == "above" && !strings.Contains(err.Error(), "4-byte limit") {
|
||||
t.Fatalf("missing limit: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, path := range []string{" ", t.TempDir(), filepath.Join(t.TempDir(), "missing")} {
|
||||
if err := CheckFileBounded(path, limit); err == nil {
|
||||
t.Fatalf("invalid path accepted: %q", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFileBoundedEnforcesLimitWithoutPartialContent(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
path := filepath.Join(root, "value.txt")
|
||||
|
||||
@@ -112,7 +112,7 @@ func (polishStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*St
|
||||
if err := requireRegularReadableFile(path, "audita report"); err != nil {
|
||||
return nil, fmt.Errorf("polish: %w", err)
|
||||
}
|
||||
if _, err := fileops.ReadFileBounded(path, audita.MaxReportOutputBytes); err != nil {
|
||||
if err := fileops.CheckFileBounded(path, audita.MaxReportOutputBytes); err != nil {
|
||||
return nil, fmt.Errorf("polish: read audita report %q: %w", path, err)
|
||||
}
|
||||
reportPaths = append(reportPaths, path)
|
||||
|
||||
Reference in New Issue
Block a user