mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-25 18:16:32 +00:00
fix(plugins): bound icon archive reads and reject prototype glyph lookups
This commit is contained in:
1 parent
6fad272afe
commit
4445c292ea
5 files changed
+52
-10
No files matched your search
@@ -504,7 +504,7 @@ export async function validatePluginDirectory(
|
||||
};
|
||||
}
|
||||
|
||||
function openZip(filePath: string): Promise<ZipFile> {
|
||||
function openZip(filePath: string, autoClose = true): Promise<ZipFile> {
|
||||
return new Promise((resolve, reject) => {
|
||||
yauzl.open(
|
||||
filePath,
|
||||
@@ -512,7 +512,7 @@ function openZip(filePath: string): Promise<ZipFile> {
|
||||
// AssertByteCountStream that rejects an overflowing chunk before it is
|
||||
// forwarded to readEntry(). Combined with the metadata limits below,
|
||||
// captured manifest contents cannot exceed PACKAGE_LIMITS.manifestBytes.
|
||||
{ lazyEntries: true, decodeStrings: true, strictFileNames: true, validateEntrySizes: true },
|
||||
{ lazyEntries: true, autoClose, decodeStrings: true, strictFileNames: true, validateEntrySizes: true },
|
||||
(error, zipFile) => {
|
||||
if (error || !zipFile) reject(error ?? new Error("Unable to open plugin archive"));
|
||||
else resolve(zipFile);
|
||||
@@ -659,9 +659,11 @@ async function inspectPluginPackage(
|
||||
if (archiveStats.size > PACKAGE_LIMITS.archiveBytes) {
|
||||
throw new Error(`Plugin archive exceeds ${PACKAGE_LIMITS.archiveBytes} bytes`);
|
||||
}
|
||||
const zipFile = await openZip(archivePath);
|
||||
const zipFile = await openZip(archivePath, false);
|
||||
try {
|
||||
const registry = new PackagePathRegistry();
|
||||
const entries = new Map<string, {
|
||||
zipEntry: Entry;
|
||||
contents?: Buffer;
|
||||
mode: number;
|
||||
sha256: string;
|
||||
@@ -711,13 +713,14 @@ async function inspectPluginPackage(
|
||||
const result = await readEntry(
|
||||
zipFile,
|
||||
entry,
|
||||
packagePath === "netcatty.plugin.json" || (/\.(png|webp)$/.test(packagePath) && entry.uncompressedSize <= PLUGIN_BRAND_ICON_MAX_BYTES),
|
||||
packagePath === "netcatty.plugin.json",
|
||||
extractionDirectory ? path.join(extractionDirectory, ...packagePath.split("/")) : undefined,
|
||||
);
|
||||
if (result.bytes !== entry.uncompressedSize || result.crc32 !== entry.crc32) {
|
||||
throw new Error(`ZIP entry integrity check failed: ${packagePath}`);
|
||||
}
|
||||
entries.set(packagePath, {
|
||||
zipEntry: entry,
|
||||
contents: result.contents,
|
||||
mode,
|
||||
sha256: result.sha256,
|
||||
@@ -760,10 +763,16 @@ async function inspectPluginPackage(
|
||||
throw new Error(`Manifest references a missing package file: ${requiredPath}`);
|
||||
}
|
||||
}
|
||||
for (const iconPath of packageIconPaths(manifest.icon)) {
|
||||
const contents = entries.get(iconPath)?.contents;
|
||||
if (!contents) throw new Error('Brand icon is missing or exceeds size limit');
|
||||
inspectPluginBrandIcon(contents, iconPath);
|
||||
// The manifest may follow its assets in ZIP order. Keep the same verified
|
||||
// descriptor open, then capture only the (at most two) declared brand images.
|
||||
// Non-logo images remain streamed regardless of extension or size.
|
||||
for (const iconPath of new Set(packageIconPaths(manifest.icon))) {
|
||||
const record = entries.get(iconPath);
|
||||
if (!record || record.size > PLUGIN_BRAND_ICON_MAX_BYTES) throw new Error('Brand icon is missing or exceeds size limit');
|
||||
const result = await readEntry(zipFile, record.zipEntry, true);
|
||||
if (!result.contents || result.sha256 !== record.sha256 || result.bytes !== record.size
|
||||
|| result.crc32 !== record.zipEntry.crc32) throw new Error('Brand icon changed during archive validation');
|
||||
inspectPluginBrandIcon(result.contents, iconPath);
|
||||
}
|
||||
for (const [companionPath, companion] of declaredCompanions) {
|
||||
const entry = entries.get(companionPath);
|
||||
@@ -789,6 +798,7 @@ async function inspectPluginPackage(
|
||||
})),
|
||||
);
|
||||
return { manifest, fileCount: entries.size, uncompressedBytes: totalBytes, contentSha256 };
|
||||
} finally { zipFile.close(); }
|
||||
}
|
||||
|
||||
export async function validatePluginPackage(
|
||||
|
||||
@@ -55,3 +55,25 @@ test('directory validation, packing and archive validation include both brand re
|
||||
await writeFile(path.join(dir, 'assets/logo-dark.png'), Buffer.alloc(256 * 1024 + 1));
|
||||
await assert.rejects(validatePluginDirectory(dir), /size limit/);
|
||||
});
|
||||
|
||||
test('undeclared PNG assets remain streamed even when their names sort before the manifest', async (t) => {
|
||||
const root = await mkdtemp(path.join(tmpdir(), 'netcatty-brand-stream-'));
|
||||
t.after(() => rm(root, { recursive: true, force: true }));
|
||||
const dir = path.join(root, 'plugin'); await mkdir(dir);
|
||||
const { icon: _icon, features: _features, ...plain } = manifest;
|
||||
await writeFile(path.join(dir, 'netcatty.plugin.json'), JSON.stringify({ ...plain, main: { browser: 'index.js' }, contributes: undefined, activationEvents: [] }));
|
||||
await writeFile(path.join(dir, 'index.js'), 'export function activate() {}');
|
||||
for (let i = 0; i < 16; i++) await writeFile(path.join(dir, `a-${i}.png`), Buffer.alloc(240 * 1024));
|
||||
const archive = path.join(root, 'no-brand.ncpkg'); await buildPluginPackage(dir, archive);
|
||||
// Instrument the explicit byte aggregation boundary, not RSS/GC timing.
|
||||
const originalConcat = Buffer.concat; let retainedImageBodies = 0;
|
||||
Buffer.concat = (list, totalLength) => {
|
||||
if (list.reduce((sum, item) => sum + item.length, 0) >= 240 * 1024) retainedImageBodies++;
|
||||
return originalConcat(list, totalLength);
|
||||
};
|
||||
try {
|
||||
const result = await validatePluginPackage(archive);
|
||||
assert.equal(result.fileCount, 18);
|
||||
assert.equal(retainedImageBodies, 0, 'unreferenced images must never be concatenated and retained');
|
||||
} finally { Buffer.concat = originalConcat; }
|
||||
});
|
||||
Reference in new issue
Block a user