feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,188 @@
|
||||
import crypto from 'node:crypto';
|
||||
import http from 'node:http';
|
||||
import https from 'node:https';
|
||||
|
||||
const panelURL = process.env.SEA_PANEL_URL?.trim();
|
||||
const panelAPIKey = process.env.SEA_PANEL_API_KEY?.trim();
|
||||
const consoleURL = process.env.SEA_CONSOLE_URL?.trim();
|
||||
const insecureTLS = process.env.SEA_PANEL_TLS_INSECURE === 'true';
|
||||
if (!panelURL || !panelAPIKey || !consoleURL) {
|
||||
console.error('SEA_PANEL_URL, SEA_PANEL_API_KEY and SEA_CONSOLE_URL are required.');
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const panelBase = new URL(panelURL);
|
||||
const consoleBase = new URL(consoleURL);
|
||||
if (!['http:', 'https:'].includes(panelBase.protocol) || panelBase.username || panelBase.password ||
|
||||
!['http:', 'https:'].includes(consoleBase.protocol) || consoleBase.username || consoleBase.password) {
|
||||
console.error('Panel and console URLs must be HTTP(S) URLs without embedded credentials.');
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const client = panelBase.protocol === 'https:' ? https : http;
|
||||
const agent = panelBase.protocol === 'https:' ? new https.Agent({ rejectUnauthorized: !insecureTLS }) : undefined;
|
||||
const md5 = (value) => crypto.createHash('md5').update(value).digest('hex');
|
||||
const canaryID = crypto.randomBytes(6).toString('hex');
|
||||
const marker = (scope) => `SEA_VHOST_CANARY_V1:${scope}:${canaryID}:${crypto.randomBytes(16).toString('hex')}`;
|
||||
const markers = {
|
||||
control: marker('control'), broker: marker('broker'), agent: marker('agent'), runtime: marker('runtime'),
|
||||
hidden: marker('hidden-web'), env: marker('extension-web'), manifest: marker('manifest-web'), public: marker('public-baseline'),
|
||||
};
|
||||
|
||||
function authFields() {
|
||||
const requestTime = Math.floor(Date.now() / 1000).toString();
|
||||
return { request_time: requestTime, request_token: md5(requestTime + md5(panelAPIKey)) };
|
||||
}
|
||||
|
||||
function formRequest(pathname, values) {
|
||||
const body = Buffer.from(new URLSearchParams({ ...values, ...authFields() }).toString());
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = client.request(new URL(pathname, panelBase), {
|
||||
method: 'POST', agent, timeout: 30_000,
|
||||
headers: { 'content-type': 'application/x-www-form-urlencoded', 'content-length': body.length },
|
||||
}, (response) => {
|
||||
let data = '';
|
||||
response.setEncoding('utf8');
|
||||
response.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
if (data.length > 2 * 1024 * 1024) request.destroy(new Error('panel response exceeded 2 MB'));
|
||||
});
|
||||
response.on('end', () => {
|
||||
if (response.statusCode < 200 || response.statusCode > 299) return reject(new Error(`panel returned HTTP ${response.statusCode}`));
|
||||
try { resolve(JSON.parse(data)); } catch { reject(new Error('panel returned non-JSON data')); }
|
||||
});
|
||||
});
|
||||
request.on('timeout', () => request.destroy(new Error('panel request timed out')));
|
||||
request.on('error', reject);
|
||||
request.end(body);
|
||||
});
|
||||
}
|
||||
|
||||
function requireSuccess(payload, operation) {
|
||||
if (!payload || (payload.status !== true && payload.status !== 'true')) throw new Error(`${operation} was rejected by the panel`);
|
||||
return payload;
|
||||
}
|
||||
|
||||
const q = (value) => `'${value.replaceAll("'", "'\\''")}'`;
|
||||
const shell = [
|
||||
'set -euo pipefail',
|
||||
'umask 077',
|
||||
`canary_id=${q(canaryID)}`,
|
||||
`control_marker=${q(markers.control)}`,
|
||||
`broker_marker=${q(markers.broker)}`,
|
||||
`agent_marker=${q(markers.agent)}`,
|
||||
`runtime_marker=${q(markers.runtime)}`,
|
||||
`hidden_marker=${q(markers.hidden)}`,
|
||||
`env_marker=${q(markers.env)}`,
|
||||
`manifest_marker=${q(markers.manifest)}`,
|
||||
`public_marker=${q(markers.public)}`,
|
||||
"host_dir=\"$(find /www/wwwroot/seavHost/vHost -xdev -mindepth 1 -maxdepth 1 -type d -name 'sv??????????' -print -quit)\"",
|
||||
'test -n "$host_dir"',
|
||||
'host_name="${host_dir##*/}"',
|
||||
'case "$host_name" in sv??????????) ;; *) exit 1 ;; esac',
|
||||
'site_conf="$(find /www/server/panel/vhost/nginx -maxdepth 1 -type f -name "$host_name*.conf" -print -quit)"',
|
||||
'test -n "$site_conf"',
|
||||
'site_domain="$(basename "$site_conf" .conf)"',
|
||||
'site_root="$(awk \'$1 == "root" { value=$2; sub(/;$/, "", value); print value; exit }\' "$site_conf")"',
|
||||
'test -n "$site_root"; site_root="$(realpath -e "$site_root")"',
|
||||
'case "$site_root/" in "$host_dir/"*) ;; *) printf \'site root escaped managed host\\n\'; exit 1 ;; esac',
|
||||
'control_dir=/var/lib/sea-vhost/security-canaries',
|
||||
'broker_dir=/var/lib/sea-vhost-panel/security-canaries',
|
||||
'agent_dir=/var/lib/sea-vhost-agent/security-canaries',
|
||||
'runtime_dir="$host_dir/.sea-runtime/security-canaries"',
|
||||
'web_dir="$site_root/.sea-vhost-canaries"',
|
||||
'install -d -o root -g sea-vhost-control -m 0750 "$control_dir"',
|
||||
'install -d -o sea-vhost-panel -g sea-vhost-panel-control -m 0700 "$broker_dir"',
|
||||
'install -d -o root -g root -m 0700 "$agent_dir"',
|
||||
'install -d -o "$host_name" -g "$host_name" -m 0700 "$runtime_dir"',
|
||||
'install -d -o "$host_name" -g "$host_name" -m 0750 "$web_dir"',
|
||||
'control_path="$control_dir/control-$canary_id.txt"',
|
||||
'broker_path="$broker_dir/broker-$canary_id.txt"',
|
||||
'agent_path="$agent_dir/agent-$canary_id.txt"',
|
||||
'runtime_path="$runtime_dir/runtime-$canary_id.txt"',
|
||||
'hidden_path="$web_dir/hidden-$canary_id.txt"',
|
||||
'env_path="$site_root/sea-vhost-canary-$canary_id.env"',
|
||||
'manifest_path="$site_root/requirements-sea-vhost-$canary_id.txt"',
|
||||
'public_path="$site_root/sea-vhost-canary-public-$canary_id.txt"',
|
||||
'link_path="$site_root/sea-vhost-canary-link-$canary_id"',
|
||||
'registry_path="$control_dir/registry-$canary_id.tsv"',
|
||||
'printf \'%s\\n\' "$control_marker" | install -o root -g sea-vhost-control -m 0640 /dev/stdin "$control_path"',
|
||||
'printf \'%s\\n\' "$broker_marker" | install -o sea-vhost-panel -g sea-vhost-panel-control -m 0600 /dev/stdin "$broker_path"',
|
||||
'printf \'%s\\n\' "$agent_marker" | install -o root -g root -m 0600 /dev/stdin "$agent_path"',
|
||||
'printf \'%s\\n\' "$runtime_marker" | install -o "$host_name" -g "$host_name" -m 0600 /dev/stdin "$runtime_path"',
|
||||
'printf \'%s\\n\' "$hidden_marker" | install -o "$host_name" -g "$host_name" -m 0640 /dev/stdin "$hidden_path"',
|
||||
'printf \'%s\\n\' "$env_marker" | install -o "$host_name" -g "$host_name" -m 0640 /dev/stdin "$env_path"',
|
||||
'printf \'%s\\n\' "$manifest_marker" | install -o "$host_name" -g "$host_name" -m 0640 /dev/stdin "$manifest_path"',
|
||||
'printf \'%s\\n\' "$public_marker" | install -o "$host_name" -g "$host_name" -m 0640 /dev/stdin "$public_path"',
|
||||
'ln -s "$runtime_path" "$link_path"',
|
||||
'setfacl -m u:www:r-x "$web_dir"; setfacl -m u:www:r-- "$hidden_path" "$env_path" "$manifest_path" "$public_path"',
|
||||
'printf \'scope\\tpath\\tmarker\\ncontrol\\t%s\\t%s\\nbroker\\t%s\\t%s\\nagent\\t%s\\t%s\\nruntime\\t%s\\t%s\\nhidden_web\\t%s\\t%s\\nextension_web\\t%s\\t%s\\nmanifest_web\\t%s\\t%s\\npublic_baseline\\t%s\\t%s\\nsymlink_probe\\t%s\\t%s\\n\' "$control_path" "$control_marker" "$broker_path" "$broker_marker" "$agent_path" "$agent_marker" "$runtime_path" "$runtime_marker" "$hidden_path" "$hidden_marker" "$env_path" "$env_marker" "$manifest_path" "$manifest_marker" "$public_path" "$public_marker" "$link_path" "$runtime_marker" | install -o root -g root -m 0600 /dev/stdin "$registry_path"',
|
||||
'expect_readable() { runuser -u "$1" -- grep -Fqx -- "$2" "$3" 2>/dev/null; }',
|
||||
'expect_denied() { if runuser -u "$1" -- grep -Fqx -- "$2" "$3" 2>/dev/null; then printf \'unexpected read: %s -> %s\\n\' "$1" "$3"; exit 1; fi; }',
|
||||
'expect_readable sea-vhost "$control_marker" "$control_path"',
|
||||
'expect_readable "$host_name" "$runtime_marker" "$runtime_path"',
|
||||
'expect_readable www "$public_marker" "$public_path"',
|
||||
'expect_readable www "$hidden_marker" "$hidden_path"',
|
||||
'for account in www nobody "$host_name"; do expect_denied "$account" "$control_marker" "$control_path"; expect_denied "$account" "$broker_marker" "$broker_path"; expect_denied "$account" "$agent_marker" "$agent_path"; done',
|
||||
'expect_denied sea-vhost "$broker_marker" "$broker_path"; expect_denied sea-vhost "$agent_marker" "$agent_path"',
|
||||
'expect_denied www "$runtime_marker" "$runtime_path"; expect_denied nobody "$runtime_marker" "$runtime_path"',
|
||||
'response_body="$(mktemp)"; trap \'rm -f "$response_body"\' EXIT',
|
||||
'public_status="$(curl --silent --show-error --path-as-is --max-time 8 --output "$response_body" --write-out \'%{http_code}\' --header "Host: $site_domain" "http://127.0.0.1/sea-vhost-canary-public-$canary_id.txt")"',
|
||||
'test "$public_status" = 200; grep -Fqx -- "$public_marker" "$response_body"',
|
||||
'probe_web_denied() { local path="$1" marker="$2" status; status="$(curl --silent --show-error --path-as-is --max-time 8 --output "$response_body" --write-out \'%{http_code}\' --header "Host: $site_domain" "http://127.0.0.1$path")"; case "$status" in 403|404) ;; *) printf \'unexpected HTTP status %s for %s\\n\' "$status" "$path"; exit 1 ;; esac; if grep -Fq -- "$marker" "$response_body"; then printf \'canary leaked over HTTP: %s\\n\' "$path"; exit 1; fi; }',
|
||||
'probe_web_denied "/.sea-vhost-canaries/hidden-$canary_id.txt" "$hidden_marker"',
|
||||
'probe_web_denied "/sea-vhost-canary-$canary_id.env" "$env_marker"',
|
||||
'probe_web_denied "/requirements-sea-vhost-$canary_id.txt" "$manifest_marker"',
|
||||
'probe_web_denied "/.sea-runtime/security-canaries/runtime-$canary_id.txt" "$runtime_marker"',
|
||||
'probe_web_denied "/sea-vhost-canary-link-$canary_id" "$runtime_marker"',
|
||||
'agent_status="$(curl --silent --output /dev/null --write-out \'%{http_code}\' --unix-socket /run/sea-vhost/agent.sock --request POST --data \'{}\' http://localhost/v1/prepare)"',
|
||||
'broker_status="$(curl --silent --output /dev/null --write-out \'%{http_code}\' --unix-socket /run/sea-vhost-panel/broker.sock --request POST --data \'{}\' http://localhost/v1/operations)"',
|
||||
'test "$agent_status" = 401; test "$broker_status" = 401',
|
||||
'printf \'canary_id=%s host=%s site=%s public_baseline=%s hidden_http=denied extension_http=denied manifest_http=denied runtime_http=denied symlink_http=denied os_boundaries=denied internal_sockets=401 registry=%s\\n\' "$canary_id" "$host_name" "$site_domain" "$public_status" "$registry_path"',
|
||||
"printf '%s\\n' 'Sea vHost canary verification: success'",
|
||||
].join('\n');
|
||||
|
||||
const taskName = `Sea vHost canary verify ${canaryID}`;
|
||||
let taskID;
|
||||
try {
|
||||
const task = requireSuccess(await formRequest('/crontab', {
|
||||
action: 'AddCrontab', name: taskName, type: 'month', where1: String((new Date().getUTCDate() + 6) % 28 + 1), where_hour: '0', where_minute: '0', user: 'root', urladdress: 'http://', save_local: '0',
|
||||
sType: 'toShell', sBody: shell, sName: 'Temporary Sea vHost security canary verification', save: '0', backupTo: 'localhost',
|
||||
}), 'temporary canary task creation');
|
||||
taskID = Number(task.id);
|
||||
if (!Number.isSafeInteger(taskID) || taskID <= 0) throw new Error('panel returned an invalid task id');
|
||||
await formRequest('/crontab', { action: 'StartTask', id: String(taskID) });
|
||||
let log = '';
|
||||
for (let attempt = 0; attempt < 90; attempt += 1) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
const result = await formRequest('/crontab', { action: 'GetLogs', id: String(taskID) });
|
||||
log = typeof result?.msg === 'string' ? result.msg : typeof result === 'string' ? result : log;
|
||||
if (log.includes('Sea vHost canary verification: success') || log.includes('★[')) break;
|
||||
}
|
||||
if (!log.includes('Sea vHost canary verification: success')) throw new Error(`canary verification did not complete:\n${log.trim()}`);
|
||||
if (Object.values(markers).some((value) => log.includes(value))) throw new Error('canary marker was written to the task log');
|
||||
|
||||
const unauthenticated = await fetch(new URL(`/api/v1/projects/1/files?path=.sea-runtime/security-canaries/runtime-${canaryID}.txt`, consoleBase), {
|
||||
redirect: 'manual', signal: AbortSignal.timeout(10_000),
|
||||
});
|
||||
if (unauthenticated.status !== 401 || (await unauthenticated.text()).includes(canaryID)) throw new Error('unauthenticated file API boundary failed');
|
||||
const crossOrigin = await fetch(new URL('/api/v1/projects/1/domains', consoleBase), {
|
||||
method: 'POST', redirect: 'manual', signal: AbortSignal.timeout(10_000),
|
||||
headers: { Origin: 'https://attacker.invalid', 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ domain: 'canary.invalid' }),
|
||||
});
|
||||
if (crossOrigin.status !== 403 || (await crossOrigin.text()).includes(canaryID)) throw new Error('cross-origin API boundary failed');
|
||||
const consoleStatic = await fetch(new URL(`/.sea-vhost-canaries/${canaryID}.txt`, consoleBase), {
|
||||
redirect: 'manual', signal: AbortSignal.timeout(10_000),
|
||||
});
|
||||
if ((await consoleStatic.text()).includes(canaryID)) throw new Error('host canary appeared on the management console');
|
||||
|
||||
const summary = log.split(/\r?\n/).find((line) => line.startsWith('canary_id='));
|
||||
console.log(summary ?? `canary_id=${canaryID}`);
|
||||
console.log('external_api_unauthenticated=401 external_api_cross_origin=403 console_canary_leak=no');
|
||||
console.log('Sea VHost canary verification: success');
|
||||
} finally {
|
||||
if (taskID) {
|
||||
try { await formRequest('/crontab', { action: 'DelCrontab', id: String(taskID) }); } catch { /* best effort */ }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user