From 76a383b829ba82564e5ef74aeb9ae18ba4248948 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Wed, 10 Aug 2022 14:00:31 +0530 Subject: [PATCH 1/2] hash cache key to prevent illegal filename --- main/utils/cache.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/main/utils/cache.ts b/main/utils/cache.ts index 863fc4e23..98c6f4235 100644 --- a/main/utils/cache.ts +++ b/main/utils/cache.ts @@ -1,6 +1,7 @@ import { ipcRenderer } from 'electron/renderer'; import path from 'path'; import { readFile, writeFile, existsSync, mkdir } from 'promise-fs'; +import crypto from 'crypto'; const CACHE_DIR = 'ente'; @@ -22,7 +23,7 @@ class DiskCache { constructor(private cacheBucketDir: string) {} async put(cacheKey: string, response: Response): Promise { - const cachePath = path.join(this.cacheBucketDir, cacheKey); + const cachePath = makeAssetCachePath(this.cacheBucketDir, cacheKey); await writeFile( cachePath, new Uint8Array(await response.arrayBuffer()) @@ -30,7 +31,7 @@ class DiskCache { } async match(cacheKey: string): Promise { - const cachePath = path.join(this.cacheBucketDir, cacheKey); + const cachePath = makeAssetCachePath(this.cacheBucketDir, cacheKey); if (existsSync(cachePath)) { return new Response(await readFile(cachePath)); } else { @@ -38,3 +39,11 @@ class DiskCache { } } } + +function makeAssetCachePath(cacheDir: string, cacheKey: string) { + const cacheKeyHash = crypto + .createHash('sha256') + .update(cacheKey) + .digest('hex'); + return path.join(cacheDir, cacheKeyHash); +} From 421efaaa6c54f035cb4c7f441f2f5478109606f4 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Wed, 10 Aug 2022 14:03:03 +0530 Subject: [PATCH 2/2] add comment --- main/utils/cache.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/main/utils/cache.ts b/main/utils/cache.ts index 98c6f4235..c115cdd2e 100644 --- a/main/utils/cache.ts +++ b/main/utils/cache.ts @@ -41,6 +41,7 @@ class DiskCache { } function makeAssetCachePath(cacheDir: string, cacheKey: string) { + // hashing the key to prevent illegal filenames const cacheKeyHash = crypto .createHash('sha256') .update(cacheKey)