mirror of
https://gitlab.com/vyla-entertainment/home.git
synced 2026-11-17 07:37:24 -05:00
huge rewrite
This commit is contained in:
53
patcher.js
53
patcher.js
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user