Skip to content

Commit

Permalink
feat: ✨ respect maximum file size of 4MB in Snyk Code [ROAD-700] (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch authored Mar 8, 2022
1 parent 2aa8c08 commit ddc968b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion code/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func (b *BundleImpl) addToBundleDocuments(files map[sglsp.DocumentURI]sglsp.Text
}
for uri, doc := range files {
if extensions[filepath.Ext(string(uri))] {
if (b.bundleDocuments[uri] == File{} && len(doc.Text) > 0) {
const maxFileSize = 1024*4096 - 1000 // 4MB, -1000 just to be safe
if (b.bundleDocuments[uri] == File{} && len(doc.Text) > 0 && len(doc.Text) < maxFileSize) {
b.bundleDocuments[uri] = File{
Hash: util.Hash(doc.Text),
Content: doc.Text,
Expand Down
20 changes: 20 additions & 0 deletions code/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ func Test_createBundleFromSource_shouldAddDocumentToBundle(t *testing.T) {
assert.Equal(t, firstBundleFile, b.bundleDocuments[firstDoc.URI])
}

func Test_createBundleFromSource_shouldNotAddDocumentToBundleIfTooBig(t *testing.T) {
b := BundleImpl{Backend: &FakeBackendService{BundleHash: "test-bundle-Hash"}}
b.bundleHash = "test-Hash"
b.bundleDocuments = map[lsp.DocumentURI]File{}
registeredDocuments := map[lsp.DocumentURI]lsp.TextDocumentItem{}

var bigFileContent []byte
fileSize := 4096*1024 + 1000
for i := 0; i < fileSize; i++ {
bigFileContent = append(bigFileContent, byte(i))
}
bundleDoc := secondDoc
bundleDoc.Text = string(bigFileContent)
registeredDocuments[bundleDoc.URI] = bundleDoc

b.addToBundleDocuments(registeredDocuments)
assert.Empty(t, b.missingFiles)
assert.Empty(t, b.bundleDocuments)
}

func Test_extendBundleFromSource_shouldAddDocumentToBundle(t *testing.T) {
b := BundleImpl{Backend: &FakeBackendService{BundleHash: "test-bundle-Hash"}}
b.bundleHash = "test-Hash"
Expand Down

0 comments on commit ddc968b

Please sign in to comment.