diff --git a/tools/privacy-scan.ts b/tools/privacy-scan.ts index 5c9f53df..7cffca53 100644 --- a/tools/privacy-scan.ts +++ b/tools/privacy-scan.ts @@ -222,18 +222,27 @@ export function scanPath(path: string, terms: Term[], shown = basename(path), re report.files++; report.bytes += data.length; // the file's own bytes (names, uncompressed parts), then what is inside a container - report.hits.push(...scanBuffer(shown, data, terms)); + const own = scanBuffer(shown, data, terms); const kind = kindOf(path, data.subarray(0, 1 << 20)); - if (kind !== 'file' && kind !== 'dir') { - let u: { dir: string; cleanup: () => void } | null = null; - try { - u = unpack(path, kind); - scanPath(u.dir, terms, `${shown}!`, report); - } catch (e) { - report.errors.push((e as Error).message); - } finally { - u?.cleanup(); - } + if (kind === 'file' || kind === 'dir') { report.hits.push(...own); return report; } + let u: { dir: string; cleanup: () => void }; + try { + u = unpack(path, kind); + } catch (e) { + report.hits.push(...own); + report.errors.push((e as Error).message); + return report; + } + try { + // An upstream path seen only in part in the compressed bytes (the rest is a back-reference) is checked in full + // in the unpacked file, so the partial match in the container's own bytes is left to that check. + report.hits.push(...own.filter(h => !(h.encoding === 'utf8' && h.what.startsWith('home folder') + && UPSTREAM_PATHS.some(p => data.toString('latin1', h.offset, h.offset + 64).startsWith(p.split('/').slice(0, 3).join('/') + '/'))))); + scanPath(u.dir, terms, `${shown}!`, report); + } catch (e) { + report.errors.push((e as Error).message); + } finally { + u.cleanup(); } return report; }