fix: enforce token file size limit before read#166
Conversation
Reject oversized token files before reading regular files into memory so a misconfigured --token-file cannot bypass the 1 MiB guard and trigger excessive allocation. Signed-off-by: Rodrigo Sampaio Vaz <rvaz@nvidia.com>
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
cmd/fleetint/security_test.go (1)
137-152: Consider adding an explicit> maxTokenSizetest case.This test covers the boundary (
== maxTokenSize) well. Adding amaxTokenSize+1case would make the oversized contract unambiguous and guard against future comparator changes.Suggested extension
func TestResolveToken_RejectsOversizedTokenFile(t *testing.T) { const maxTokenSize = 1 << 20 @@ require.NoError(t, os.Truncate(tmpFile, maxTokenSize)) @@ assert.Contains(t, err.Error(), "exceeds maximum size") + + tmpFile2 := filepath.Join(t.TempDir(), "token-plus-one") + file2, err := os.Create(tmpFile2) + require.NoError(t, err) + require.NoError(t, file2.Close()) + require.NoError(t, os.Truncate(tmpFile2, maxTokenSize+1)) + + err = app.Run([]string{"fleetint", "enroll", "--endpoint", "https://example.com", "--token-file", tmpFile2}) + require.Error(t, err) + assert.Contains(t, err.Error(), "exceeds maximum size") }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@cmd/fleetint/security_test.go` around lines 137 - 152, Add a second assertion case that explicitly tests a file larger than the allowed size: in TestResolveToken_RejectsOversizedTokenFile (and using the existing maxTokenSize, tmpFile and app.Run setup) create/truncate the tmpFile to maxTokenSize+1 bytes and call app.Run with the same arguments, then require an error and assert the error message contains "exceeds maximum size" to ensure the > maxTokenSize case fails as expected.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@cmd/fleetint/security_test.go`:
- Around line 137-152: Add a second assertion case that explicitly tests a file
larger than the allowed size: in TestResolveToken_RejectsOversizedTokenFile (and
using the existing maxTokenSize, tmpFile and app.Run setup) create/truncate the
tmpFile to maxTokenSize+1 bytes and call app.Run with the same arguments, then
require an error and assert the error message contains "exceeds maximum size" to
ensure the > maxTokenSize case fails as expected.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: a2750104-408a-4e4c-9a32-493ea7c74380
📒 Files selected for processing (2)
cmd/fleetint/enroll.gocmd/fleetint/security_test.go
Signed-off-by: Rodrigo Sampaio Vaz <rvaz@nvidia.com>
Signed-off-by: Rodrigo Sampaio Vaz <rvaz@nvidia.com>
Reject oversized token files before reading regular files into memory so a misconfigured --token-file cannot bypass the 1 MiB guard and trigger excessive allocation.
Description
Checklist
Summary by CodeRabbit
Bug Fixes
Tests