mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
feat(engine): operators can add networks the render must treat as non-public (#7790)
* feat(engine): operators can add networks the render must treat as non-public * style(engine): format the non-public ranges change
This commit is contained in:
@@ -200,6 +200,11 @@ address a name resolves to and connects to the address it checked. A refused req
|
||||
fails like any other unreachable asset. Leave it unset for local renders that load
|
||||
assets from your own machine or network.
|
||||
|
||||
If your network routes a range that is not a private one, such as a cluster's pod range,
|
||||
add it with `PRODUCER_NON_PUBLIC_CIDRS`, a comma-separated list like `192.160.0.0/16`. Asset
|
||||
downloads and the render browser then refuse those addresses too. A malformed entry
|
||||
refuses every address rather than being skipped, and logs a warning.
|
||||
|
||||
## Related topics
|
||||
|
||||
<CardGroup cols={2}>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { ipv4HostClass, isBlockedNetworkHost } from "./networkPolicy.js";
|
||||
|
||||
describe("special-use network list", () => {
|
||||
@@ -27,3 +27,27 @@ describe("IPv4 host class", () => {
|
||||
["100.128.0.1", "public"],
|
||||
])("%s is %s", (ip, hostClass) => expect(ipv4HostClass(ip)).toBe(hostClass));
|
||||
});
|
||||
|
||||
describe("operator ranges", () => {
|
||||
afterEach(() => vi.unstubAllEnvs());
|
||||
|
||||
it("adds the operator's networks to the list and classes them as private", () => {
|
||||
expect(isBlockedNetworkHost("192.160.83.14")).toBe(false);
|
||||
vi.stubEnv("PRODUCER_NON_PUBLIC_CIDRS", "192.160.0.0/16, 2600:1f00::/24");
|
||||
expect(isBlockedNetworkHost("192.160.83.14")).toBe(true);
|
||||
expect(ipv4HostClass("192.160.83.14")).toBe("private");
|
||||
expect(isBlockedNetworkHost("2600:1f00::1")).toBe(true);
|
||||
expect(isBlockedNetworkHost("192.161.0.1")).toBe(false);
|
||||
});
|
||||
|
||||
it.each(["192.160.0.0", "192.160.0.0/33", "not-a-network/8"])(
|
||||
"refuses every address under a malformed setting %s",
|
||||
(range) => {
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
vi.stubEnv("PRODUCER_NON_PUBLIC_CIDRS", range);
|
||||
expect(isBlockedNetworkHost("8.8.8.8")).toBe(true);
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringContaining("PRODUCER_NON_PUBLIC_CIDRS"));
|
||||
warn.mockRestore();
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -34,7 +34,53 @@ for (const [network, prefix, hostClass] of [
|
||||
|
||||
export function ipv4HostClass(address: string): NonPublicIPv4Class | "public" {
|
||||
for (const [hostClass, list] of IPV4_CLASSES) if (list.check(address, "ipv4")) return hostClass;
|
||||
return "public";
|
||||
return inOperatorRange(address, "ipv4") ? "private" : "public";
|
||||
}
|
||||
|
||||
const OPERATOR_RANGES_ENV = "PRODUCER_NON_PUBLIC_CIDRS";
|
||||
let operatorRangesFor: string | undefined;
|
||||
let operatorRangeList: BlockList | "malformed" = new BlockList();
|
||||
|
||||
function operatorRanges(): BlockList | "malformed" {
|
||||
const raw = process.env[OPERATOR_RANGES_ENV] ?? "";
|
||||
if (raw === operatorRangesFor) return operatorRangeList;
|
||||
operatorRangesFor = raw;
|
||||
operatorRangeList = parseRanges(raw);
|
||||
if (operatorRangeList === "malformed") {
|
||||
console.warn(
|
||||
`[hyperframes] ${OPERATOR_RANGES_ENV}="${raw}" is not a list like 192.160.0.0/16; every address is refused.`,
|
||||
);
|
||||
}
|
||||
return operatorRangeList;
|
||||
}
|
||||
|
||||
function parseRanges(raw: string): BlockList | "malformed" {
|
||||
const list = new BlockList();
|
||||
for (const range of raw
|
||||
.split(",")
|
||||
.map((part) => part.trim())
|
||||
.filter(Boolean)) {
|
||||
const [network = "", bits] = range.split("/");
|
||||
const family = isIP(network);
|
||||
const prefix = Number(bits);
|
||||
const max = family === 4 ? 32 : 128;
|
||||
if (
|
||||
family === 0 ||
|
||||
bits === undefined ||
|
||||
!Number.isInteger(prefix) ||
|
||||
prefix < 0 ||
|
||||
prefix > max
|
||||
) {
|
||||
return "malformed";
|
||||
}
|
||||
list.addSubnet(network, prefix, family === 4 ? "ipv4" : "ipv6");
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
function inOperatorRange(address: string, family: "ipv4" | "ipv6"): boolean {
|
||||
const ranges = operatorRanges();
|
||||
return ranges === "malformed" || ranges.check(address, family);
|
||||
}
|
||||
|
||||
const NON_PUBLIC_IPV6_ADDRESSES = new BlockList();
|
||||
@@ -66,6 +112,7 @@ export function isBlockedNetworkHost(hostname: string): boolean {
|
||||
return true;
|
||||
const addressType = isIP(h);
|
||||
if (addressType === 0) return false;
|
||||
if (inOperatorRange(h, addressType === 4 ? "ipv4" : "ipv6")) return true;
|
||||
return addressType === 4
|
||||
? NON_PUBLIC_IPV4_ADDRESSES.check(h, "ipv4")
|
||||
: NON_PUBLIC_IPV6_ADDRESSES.check(h, "ipv6");
|
||||
|
||||
Reference in New Issue
Block a user