Iptables restrictions bugfix: #1

This commit is contained in:
2026-09-27 15:40:05 +01:00
parent dbae73a746
commit 65b321f73b
@@ -79,16 +79,58 @@ function chunkPorts({ ports }: { ports: string[] }) {
return chunks; return chunks;
} }
// function acceptLines({
// chain,
// source,
// destination,
// protocol,
// ports,
// }: {
// chain: string;
// source: string;
// destination?: string;
// protocol?: string;
// ports: string[];
// }) {
// const lines: string[] = [];
// const protocols = protocolsForRule({
// protocol,
// has_ports: Boolean(ports[0]),
// });
// const port_chunks = ports[0] ? chunkPorts({ ports }) : [[]];
// for (let p = 0; p < protocols.length; p++) {
// const proto = protocols[p];
// for (let c = 0; c < port_chunks.length; c++) {
// const chunk = port_chunks[c] || [];
// lines.push(
// joinArgs([
// `iptables -A ${chain}`,
// `-s ${source}`,
// destination ? `-d ${destination}` : undefined,
// proto ? `-p ${proto}` : undefined,
// proto ? portMatch({ ports: chunk }) : undefined,
// `-j ACCEPT`,
// ]),
// );
// }
// }
// return lines;
// }
function acceptLines({ function acceptLines({
chain, forward_chain,
input_chain,
source, source,
destination,
protocol, protocol,
ports, ports,
}: { }: {
chain: string; forward_chain: string;
input_chain: string;
source: string; source: string;
destination?: string;
protocol?: string; protocol?: string;
ports: string[]; ports: string[];
}) { }) {
@@ -104,14 +146,28 @@ function acceptLines({
for (let c = 0; c < port_chunks.length; c++) { for (let c = 0; c < port_chunks.length; c++) {
const chunk = port_chunks[c] || []; const chunk = port_chunks[c] || [];
const port_match_str = proto
? portMatch({ ports: chunk })
: undefined;
// 1. FORWARD chain: Allows routed traffic (including Docker bridges/DNAT)
lines.push( lines.push(
joinArgs([ joinArgs([
`iptables -A ${chain}`, `iptables -A ${forward_chain}`,
`-s ${source}`, `-s ${source}`,
destination ? `-d ${destination}` : undefined,
proto ? `-p ${proto}` : undefined, proto ? `-p ${proto}` : undefined,
proto ? portMatch({ ports: chunk }) : undefined, port_match_str,
`-j ACCEPT`,
]),
);
// 2. INPUT chain: Allows traffic bound directly for host OS services
lines.push(
joinArgs([
`iptables -A ${input_chain}`,
`-s ${source}`,
proto ? `-p ${proto}` : undefined,
port_match_str,
`-j ACCEPT`, `-j ACCEPT`,
]), ]),
); );
@@ -128,11 +184,7 @@ export default function buildHostIptablesScripts({
host_subnet, host_subnet,
clients, clients,
}: Params) { }: Params) {
if ( if (!Number.isSafeInteger(host_id) || host_id < 0 || host_id > 999999999) {
!Number.isSafeInteger(host_id) ||
host_id < 0 ||
host_id > 999999999
) {
return { return {
success: false, success: false,
msg: `Invalid host id`, msg: `Invalid host id`,
@@ -164,9 +216,7 @@ export default function buildHostIptablesScripts({
const { forward, input } = deriveIptablesChainNames({ host_id }); const { forward, input } = deriveIptablesChainNames({ host_id });
const masquerade_match = scoped_subnet const masquerade_match = scoped_subnet ? `-s ${scoped_subnet} ` : ``;
? `-s ${scoped_subnet} -o ${target_interface}`
: `-o ${target_interface}`;
const accept_lines: string[] = []; const accept_lines: string[] = [];
const host_clients = clients || []; const host_clients = clients || [];
@@ -212,19 +262,28 @@ export default function buildHostIptablesScripts({
}; };
} }
// if (rule.rule_type == "all") {
// if (scoped_subnet) {
// accept_lines.push(
// `iptables -A ${forward} -s ${source} -d ${scoped_subnet} -j ACCEPT`,
// `iptables -A ${forward} -s ${source} -o ${target_interface} -j ACCEPT`,
// `iptables -A ${input} -s ${source} -j ACCEPT`,
// );
// } else {
// accept_lines.push(
// `iptables -A ${forward} -s ${source} -j ACCEPT`,
// `iptables -A ${input} -s ${source} -j ACCEPT`,
// );
// }
// continue;
// }
if (rule.rule_type == "all") { if (rule.rule_type == "all") {
if (scoped_subnet) {
accept_lines.push(
`iptables -A ${forward} -s ${source} -d ${scoped_subnet} -j ACCEPT`,
`iptables -A ${forward} -s ${source} -o ${target_interface} -j ACCEPT`,
`iptables -A ${input} -s ${source} -j ACCEPT`,
);
} else {
accept_lines.push( accept_lines.push(
`iptables -A ${forward} -s ${source} -j ACCEPT`, `iptables -A ${forward} -s ${source} -j ACCEPT`,
`iptables -A ${input} -s ${source} -j ACCEPT`, `iptables -A ${input} -s ${source} -j ACCEPT`,
); );
}
continue; continue;
} }
@@ -241,11 +300,21 @@ export default function buildHostIptablesScripts({
}; };
} }
// accept_lines.push(
// ...acceptLines({
// chain: forward,
// source,
// destination,
// protocol: rule.protocol || "any",
// ports: parsed_ports.ports,
// }),
// );
accept_lines.push( accept_lines.push(
...acceptLines({ ...acceptLines({
chain: forward, forward_chain: forward,
input_chain: input,
source, source,
destination,
protocol: rule.protocol || "any", protocol: rule.protocol || "any",
ports: parsed_ports.ports, ports: parsed_ports.ports,
}), }),