From 69573fdb4ae21c6e740ac0f397161018e7b5c4b1 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 8 Oct 2021 10:15:23 -0400 Subject: [PATCH] Add handling for invalid utf-8 Signed-off-by: Sebastian Malton --- src/main/catalog-sources/kubeconfig-sync.ts | 25 ++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/catalog-sources/kubeconfig-sync.ts b/src/main/catalog-sources/kubeconfig-sync.ts index 6ecc3ffafc..748eb129bd 100644 --- a/src/main/catalog-sources/kubeconfig-sync.ts +++ b/src/main/catalog-sources/kubeconfig-sync.ts @@ -54,6 +54,14 @@ const ignoreGlobs = [ matcher: globToRegExp(rawGlob), })); +/** + * This should be much larger than any kubeconfig text file + * + * Even if you have a cert-file, key-file, and client-cert files that is only + * 12kb of extra data (at 4096 bytes each) which allows for around 150 entries. + */ +const maxAllowedFileReadSize = 2 * 1024 * 1024; // 2 MiB + export class KubeconfigSyncManager extends Singleton { protected sources = observable.map, Disposer]>(); protected syncing = false; @@ -228,8 +236,6 @@ export function computeDiff(contents: string, source: RootSource, filePath: stri }); } -const maxAllowedFileReadSize = 16 * 1024 * 1024; // 16 MiB - function diffChangedConfig(filePath: string, source: RootSource, stats: fs.Stats): Disposer { logger.debug(`${logPrefix} file changed`, { filePath }); @@ -245,7 +251,8 @@ function diffChangedConfig(filePath: string, source: RootSource, stats: fs.Stats mode: fs.constants.O_RDONLY, }); const readStream: stream.Readable = fileReader; - const bufs: Buffer[] = []; + const decoder = new TextDecoder("utf-8", { fatal: true }); + let fileString = ""; let closed = false; const cleanup = () => { @@ -262,7 +269,15 @@ function diffChangedConfig(filePath: string, source: RootSource, stats: fs.Stats }; readStream - .on("data", chunk => bufs.push(chunk)) + .on("data", (chunk: Buffer) => { + try { + fileString += decoder.decode(chunk, { stream: true }); + } catch (error) { + logger.warn(`${logPrefix} skipping ${filePath}: ${error}`); + source.clear(); + cleanup(); + } + }) .on("close", () => cleanup()) .on("error", error => { cleanup(); @@ -270,7 +285,7 @@ function diffChangedConfig(filePath: string, source: RootSource, stats: fs.Stats }) .on("end", () => { if (!closed) { - computeDiff(Buffer.concat(bufs).toString("utf-8"), source, filePath); + computeDiff(fileString, source, filePath); } });