huge rewrite

This commit is contained in:
2026-10-29 22:19:03 -07:00
parent 25358e9c17
commit e738ea158b
29 changed files with 2237 additions and 2060 deletions

View File

@@ -63,46 +63,30 @@ class Patcher {
const fullPath = path.join(this.appDataPath, dir);
if (fs.existsSync(fullPath)) {
try {
const trashPath = path.join(this.appDataPath, `.trash_${dir}_${Date.now()}`);
fs.renameSync(fullPath, trashPath);
fs.rmSync(trashPath, { recursive: true, force: true });
} catch (e) {
try {
fs.rmSync(fullPath, { recursive: true, force: true });
} catch (err2) { }
}
fs.rmSync(fullPath, { recursive: true, force: true });
} catch (e) { }
}
}
try {
const files = fs.readdirSync(this.appDataPath);
for (const file of files) {
if (file.startsWith('.trash_')) {
fs.rmSync(path.join(this.appDataPath, file), { recursive: true, force: true });
}
}
} catch (e) { }
}
extractZipPayload(zipPath) {
const AdmZip = require('adm-zip');
const zip = new AdmZip(zipPath);
const entries = zip.getEntries();
if (!entries || entries.length === 0) {
throw new Error('Downloaded archive is empty, invalid, or truncated.');
throw new Error('Archive is empty, invalid, or truncated.');
}
const targetDir = path.resolve(this.appDataPath);
for (const entry of entries) {
const entryPath = path.normalize(entry.entryName);
const resolvedPath = path.resolve(targetDir, entryPath);
if (!resolvedPath.startsWith(targetDir)) {
throw new Error(`Directory traversal attempt detected in archive: ${entry.entryName}`);
}
}
zip.extractAllTo(this.appDataPath, true);
const testPath = path.join(this.appDataPath, 'frontend', 'index.html');
if (!fs.existsSync(testPath)) {
const { execSync } = require('child_process');
if (process.platform === 'win32') {
execSync(`powershell.exe -NoProfile -NonInteractive -Command "Expand-Archive -Path '${zipPath}' -DestinationPath '${this.appDataPath}' -Force"`, { windowsHide: true });
} else {
execSync(`unzip -o "${zipPath}" -d "${this.appDataPath}"`);
}
}
}
async extractToStaging(zipPath, onFile) {
@@ -114,6 +98,16 @@ class Patcher {
if (entries.length === 0) {
throw new Error('Downloaded archive is empty, invalid, or truncated.');
}
const resolvedStaging = path.resolve(stagingDir);
for (const entry of entries) {
const entryPath = path.normalize(entry.entryName);
const resolvedPath = path.resolve(resolvedStaging, entryPath);
if (!resolvedPath.startsWith(resolvedStaging)) {
throw new Error(`Directory traversal attempt detected: ${entry.entryName}`);
}
}
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
zip.extractEntryTo(entry, stagingDir, true, true);
@@ -183,7 +177,6 @@ class Patcher {
if (file === 'node_modules' || file === '.git') continue;
this.processDirectory(fullPath, replacementList);
} else if (stat.isFile()) {
const ext = path.extname(fullPath).toLowerCase();
let content = fs.readFileSync(fullPath, 'utf8');
let modified = false;
@@ -212,4 +205,4 @@ class Patcher {
}
}
module.exports = Patcher;
module.exports = Patcher;