Skip to content

Commit d8462ad

Browse files
committed
JS: Add a file size limit to extractor
1 parent bc47646 commit d8462ad

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ public class AutoBuild {
222222
private boolean installDependencies = false;
223223
private final VirtualSourceRoot virtualSourceRoot;
224224
private ExtractorState state;
225+
private final long maximumFileSizeInMegabytes;
225226

226227
/** The default timeout when installing dependencies, in milliseconds. */
227228
public static final int INSTALL_DEPENDENCIES_DEFAULT_TIMEOUT = 10 * 60 * 1000; // 10 minutes
@@ -236,6 +237,7 @@ public AutoBuild() {
236237
this.defaultEncoding = getEnvVar("LGTM_INDEX_DEFAULT_ENCODING");
237238
this.installDependencies = Boolean.valueOf(getEnvVar("LGTM_INDEX_TYPESCRIPT_INSTALL_DEPS"));
238239
this.virtualSourceRoot = makeVirtualSourceRoot();
240+
this.maximumFileSizeInMegabytes = EnvironmentVariables.getMegabyteCountFromPrefixedEnv("MAX_FILE_SIZE", 10);
239241
setupFileTypes();
240242
setupXmlMode();
241243
setupMatchers();
@@ -446,8 +448,8 @@ private boolean addPathPattern(Set<Path> patterns, Path base, String pattern) {
446448
}
447449

448450
/**
449-
* Returns whether the autobuilder has seen code.
450-
* This is overridden in tests.
451+
* Returns whether the autobuilder has seen code.
452+
* This is overridden in tests.
451453
*/
452454
protected boolean hasSeenCode() {
453455
return seenCode;
@@ -741,12 +743,12 @@ private CompletableFuture<?> extractSource() throws IOException {
741743
dependencyInstallationResult = this.preparePackagesAndDependencies(filesToExtract);
742744
}
743745
Set<Path> extractedFiles = new LinkedHashSet<>();
744-
746+
745747
// Extract HTML files as they may contain TypeScript
746748
CompletableFuture<?> htmlFuture = extractFiles(
747749
filesToExtract, extractedFiles, extractors,
748750
f -> extractors.fileType(f) == FileType.HTML);
749-
751+
750752
htmlFuture.join(); // Wait for HTML extraction to be finished.
751753

752754
// extract TypeScript projects and files
@@ -1229,6 +1231,11 @@ private void doExtract(FileExtractor extractor, Path file, ExtractorState state)
12291231
warn("Skipping " + file + ", which does not exist.");
12301232
return;
12311233
}
1234+
long fileSize = f.length();
1235+
if (fileSize > 1_000_000L * this.maximumFileSizeInMegabytes) {
1236+
warn("Skipping " + file + " because it is too large (" + StringUtil.printFloat(fileSize / 1_000_000.0) + " MB). The limit is " + this.maximumFileSizeInMegabytes + " MB.");
1237+
return;
1238+
}
12321239

12331240
try {
12341241
long start = logBeginProcess("Extracting " + file);

0 commit comments

Comments
 (0)