Skip to content

Commit 365d53d

Browse files
chrisleckblakeembrey
authored andcommitted
Validate cache file contents before use (#394)
1 parent b99b6bc commit 365d53d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,10 @@ function readThrough (
489489

490490
try {
491491
const output = getFile(outputPath)
492-
cache.outputs[fileName] = output
493-
return output
492+
if (isValidCacheContent(output)) {
493+
cache.outputs[fileName] = output
494+
return output
495+
}
494496
} catch (err) {/* Ignore. */}
495497

496498
const [value, sourceMap] = compile(code, fileName, lineOffset)
@@ -536,6 +538,14 @@ function getCacheName (sourceCode: string, fileName: string) {
536538
.digest('hex')
537539
}
538540

541+
/**
542+
* Ensure the given cached content is valid by sniffing for a base64 encoded '}'
543+
* at the end of the content, which should exist if there is a valid sourceMap present.
544+
*/
545+
function isValidCacheContent (content: string) {
546+
return /(?:9|0=|Q==)$/.test(content.slice(-3))
547+
}
548+
539549
/**
540550
* Create a hash of the current configuration.
541551
*/

0 commit comments

Comments
 (0)