Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,90 @@
|
||||
"use strict";
|
||||
|
||||
const AdmZip = require("adm-zip");
|
||||
const path = require("path");
|
||||
|
||||
const MAX_ENTRY_BYTES = 8 * 1024 * 1024;
|
||||
const MAX_FILES = 32;
|
||||
const MAX_PAYLOAD_BYTES = 20 * 1024 * 1024;
|
||||
|
||||
function filePayloadBytes(filename, size) {
|
||||
return Buffer.byteLength(JSON.stringify({ path: filename, contentBase64: "" }), "utf8") + Math.ceil(size / 3) * 4 + 1;
|
||||
}
|
||||
|
||||
function extractNodeUpdateFilesFromRelease(targetRelease, zipBuffer, { isAllowedNodeReleasePath, getUpdateEntryDataCapped } = {}) {
|
||||
if (typeof isAllowedNodeReleasePath !== "function" || typeof getUpdateEntryDataCapped !== "function") {
|
||||
throw new TypeError("Node update archive validation dependencies are required");
|
||||
}
|
||||
const entries = new AdmZip(zipBuffer).getEntries();
|
||||
if (!entries.length) {
|
||||
throw Object.assign(new Error("node update archive is empty"), { statusCode: 502 });
|
||||
}
|
||||
let prefix = "";
|
||||
for (const entry of entries) {
|
||||
const name = entry.entryName || "";
|
||||
if (name.includes("/")) {
|
||||
prefix = name.split("/")[0] + "/";
|
||||
break;
|
||||
}
|
||||
}
|
||||
const candidates = [];
|
||||
let skipped = 0;
|
||||
const envelopeBytes = Buffer.byteLength(JSON.stringify({ version: targetRelease?.tag_name || "", files: [] }), "utf8") + 128;
|
||||
let declaredPayloadBytes = envelopeBytes;
|
||||
const rejectPayload = () => Object.assign(new Error("node update archive exceeds the payload size budget"), { code: "EUPDATEPAYLOADTOOLARGE", statusCode: 502 });
|
||||
for (const entry of entries) {
|
||||
if (entry.isDirectory) continue;
|
||||
const fullEntryPath = String(entry.entryName || "");
|
||||
const relativePath = prefix && fullEntryPath.startsWith(prefix)
|
||||
? fullEntryPath.slice(prefix.length)
|
||||
: fullEntryPath;
|
||||
const rawRelative = relativePath.replace(/\\/g, "/");
|
||||
if (rawRelative.includes("/") || rawRelative.includes("..")) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
const normalized = path.posix.normalize(rawRelative);
|
||||
if (!isAllowedNodeReleasePath(normalized)) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
const declaredSize = Number(entry.header?.size);
|
||||
if (!Number.isSafeInteger(declaredSize) || declaredSize < 0 || declaredSize > MAX_ENTRY_BYTES) {
|
||||
throw Object.assign(new Error("archive entry exceeds the allowed size"), { code: "EUPDATEENTRYTOOLARGE", statusCode: 502 });
|
||||
}
|
||||
declaredPayloadBytes += filePayloadBytes(normalized, declaredSize);
|
||||
candidates.push({ entry, normalized });
|
||||
// adpanel:node-update-aggregate-budget - reject file sets the daemon
|
||||
// cannot accept before allocating their decompressed or base64 payloads.
|
||||
if (candidates.length > MAX_FILES || declaredPayloadBytes > MAX_PAYLOAD_BYTES) throw rejectPayload();
|
||||
}
|
||||
const files = [];
|
||||
let actualPayloadBytes = envelopeBytes;
|
||||
for (const { entry, normalized } of candidates) {
|
||||
// adpanel:node-update-inflate-budget - enforce the accepted entry size
|
||||
// while inflating; a post-inflate length check cannot prevent an OOM.
|
||||
let data;
|
||||
try {
|
||||
data = getUpdateEntryDataCapped(entry, MAX_ENTRY_BYTES);
|
||||
} catch (error) {
|
||||
error.statusCode = 502;
|
||||
throw error;
|
||||
}
|
||||
if (!data || data.length === 0) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
if (data.length > MAX_ENTRY_BYTES) {
|
||||
throw Object.assign(new Error("archive entry exceeds the allowed size"), { code: "EUPDATEENTRYTOOLARGE", statusCode: 502 });
|
||||
}
|
||||
actualPayloadBytes += filePayloadBytes(normalized, data.length);
|
||||
if (actualPayloadBytes > MAX_PAYLOAD_BYTES) throw rejectPayload();
|
||||
files.push({ path: normalized, contentBase64: data.toString("base64") });
|
||||
}
|
||||
if (!files.some((file) => file.path === "main.go")) {
|
||||
throw Object.assign(new Error("node release does not contain main.go"), { statusCode: 502 });
|
||||
}
|
||||
return { files, filesSkipped: skipped, sourceTag: targetRelease?.tag_name || "" };
|
||||
}
|
||||
|
||||
module.exports = { extractNodeUpdateFilesFromRelease };
|
||||
Reference in new issue
Block a user