From 2358c57542861a4d3129af08fac2a1f4a6d01911 Mon Sep 17 00:00:00 2001 From: Jan Keith Darunday Date: Thu, 17 Oct 2024 23:00:57 +0800 Subject: [PATCH] Add checks for invalid reg output --- lib/system-status.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/system-status.js b/lib/system-status.js index 4f00c52..682b6b4 100644 --- a/lib/system-status.js +++ b/lib/system-status.js @@ -210,10 +210,15 @@ customElements.define('system-status', class extends HTMLElement { const pathQuery = spawnSync('reg', ['query', 'HKCU\\Environment', '/v', 'Path']) if (pathQuery.status !== 0 && !pathQuery.stderr.toString().includes('unable to find')) { - throw new Error('Failed to query user PATH.') + throw new Error('Failed to query user PATH') } - const originalPath = pathQuery.stdout.toString()?.match(WIN_REGVAL_MATCHER)?.[1] + const pathMatches = pathQuery.stdout.toString()?.match(WIN_REGVAL_MATCHER) + if (!Array.isArray(pathMatches) || pathMatches.length !== 2) { + throw new Error('Failed to extract user PATH') + } + + const originalPath = pathMatches[1] if (!originalPath || !originalPath.includes(BIN)) { const newPath = originalPath ? `${BIN};${originalPath}` : BIN spawnSync('reg', ['add', 'HKCU\\Environment', '/v', 'Path', '/t', 'REG_SZ', '/d', newPath, '/f'])