Skip to content

Commit

Permalink
Cache: validating data as its being read more carefully, for extra sa…
Browse files Browse the repository at this point in the history
…fety
  • Loading branch information
fabiospampinato committed Nov 30, 2023
1 parent 85ccff6 commit 09ec088
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import { getCachePath, isObject, sha1hex, sha1base64 } from "./utils.js";
import { getCachePath, isArray, isBoolean, isObject, isString, sha1hex, sha1base64 } from "./utils.js";
import type Logger from "./logger.js";
import type { Options } from "./types.js";

Expand All @@ -23,7 +23,6 @@ type FileData = {

//TODO: Maybe remember thrown errors also, if they are under a certain size
//TODO: Use some kind of relative path as the file key, if in CI enviornments parts of the path can be kinda random, or if the cache file is committed
//TODO: Validate cached values as they are being read, for better safety

class Cache {
private version: string;
Expand Down Expand Up @@ -77,8 +76,9 @@ class Cache {
const save = this.set.bind(this, filePath, filePathHash);
try {
const file = this.store[this.version]?.files?.[filePathHash];
if (!file) return { save };
if (!file || !isArray(file) || file.length !== 2) return { save };
const [hash, formatted] = file;
if (!isString(hash) || !isBoolean(formatted)) return { save };
const content = fs.readFileSync(filePath);
const fileHash = sha1base64(content);
if (hash !== fileHash) return { content, save };
Expand Down

0 comments on commit 09ec088

Please sign in to comment.