1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add handling for invalid utf-8

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-10-08 10:15:23 -04:00
parent 2808565161
commit 69573fdb4a

View File

@ -54,6 +54,14 @@ const ignoreGlobs = [
matcher: globToRegExp(rawGlob), 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 { export class KubeconfigSyncManager extends Singleton {
protected sources = observable.map<string, [IComputedValue<CatalogEntity[]>, Disposer]>(); protected sources = observable.map<string, [IComputedValue<CatalogEntity[]>, Disposer]>();
protected syncing = false; 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 { function diffChangedConfig(filePath: string, source: RootSource, stats: fs.Stats): Disposer {
logger.debug(`${logPrefix} file changed`, { filePath }); logger.debug(`${logPrefix} file changed`, { filePath });
@ -245,7 +251,8 @@ function diffChangedConfig(filePath: string, source: RootSource, stats: fs.Stats
mode: fs.constants.O_RDONLY, mode: fs.constants.O_RDONLY,
}); });
const readStream: stream.Readable = fileReader; const readStream: stream.Readable = fileReader;
const bufs: Buffer[] = []; const decoder = new TextDecoder("utf-8", { fatal: true });
let fileString = "";
let closed = false; let closed = false;
const cleanup = () => { const cleanup = () => {
@ -262,7 +269,15 @@ function diffChangedConfig(filePath: string, source: RootSource, stats: fs.Stats
}; };
readStream 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("close", () => cleanup())
.on("error", error => { .on("error", error => {
cleanup(); cleanup();
@ -270,7 +285,7 @@ function diffChangedConfig(filePath: string, source: RootSource, stats: fs.Stats
}) })
.on("end", () => { .on("end", () => {
if (!closed) { if (!closed) {
computeDiff(Buffer.concat(bufs).toString("utf-8"), source, filePath); computeDiff(fileString, source, filePath);
} }
}); });