Files
Netcatty/electron/plugins/sourceIdentity.cjs
T

45 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use strict";
const { createHash } = require("node:crypto");
const { isIP } = require("node:net");
const OFFICIAL_ORIGIN = "https://plugins.netcatty.app";
const OFFICIAL_SOURCE = Object.freeze({ id: "official", kind: "official", name: "Netcatty marketplace", origin: OFFICIAL_ORIGIN });
const LOCAL_SOURCE = Object.freeze({ id: "local", kind: "local", name: "Local import", origin: null });
const hash = (value) => createHash("sha256").update(value).digest("hex");
// Source identity is host-owned. Neither a manifest nor a catalog supplies kind/id.
function canonicalSourceOrigin(value) {
if (typeof value !== "string" || value.length > 2048 || /[\s\\%?#]/u.test(value)) throw new Error("Use a public HTTPS origin without credentials, query or fragment");
const url = new URL(value);
if (url.protocol !== "https:" || url.username || url.password || url.port
|| !["/", "/v1/snapshot.json"].includes(url.pathname)) throw new Error("Use an HTTPS origin or its /v1/snapshot.json endpoint");
const hostname = url.hostname.toLowerCase();
if (isIP(hostname) || hostname.includes(":") || !hostname.includes(".") || hostname.endsWith(".")
|| /(?:^|\.)(?:localhost|local|internal|invalid|test)$/u.test(hostname)) throw new Error("Market sources must use a public DNS hostname");
// Explicitly reject textual credentials/normalization tricks before canonicalizing case.
if (!/^https:\/\/[^/@]+(?:\/|\/v1\/snapshot\.json)?$/iu.test(value)) throw new Error("Invalid market source address");
return url.origin;
}
function thirdPartySource(name, address) {
if (typeof name !== "string" || !name.trim() || name.length > 100 || /[\u0000-\u001f\u007f]/u.test(name)) throw new Error("Source name must contain 1–100 printable characters");
const origin = canonicalSourceOrigin(address);
if (origin === OFFICIAL_ORIGIN) throw new Error("The official source is already built in");
return Object.freeze({ id: `third-party:${hash(origin)}`, kind: "third-party", name: name.trim(), origin });
}
function assertSource(source) {
if (!source || typeof source !== "object") throw new Error("Missing host source identity");
const trusted = source.id === "official" ? OFFICIAL_SOURCE : source.id === "local" ? LOCAL_SOURCE : thirdPartySource(source.name, source.origin);
if (["id", "kind", "origin"].some((key) => trusted[key] !== source[key])) throw new Error("Invalid host source identity");
return { ...trusted };
}
function privateOwner(source, pluginId) {
if (typeof pluginId !== "string" || !/^[a-z0-9][a-z0-9.-]{1,127}$/u.test(pluginId)) throw new Error("Invalid plugin identity");
return `source:${hash(assertSource(source).id)}:${pluginId}`;
}
module.exports = { OFFICIAL_SOURCE, LOCAL_SOURCE, canonicalSourceOrigin, thirdPartySource, assertSource, privateOwner };