Files
mtusi2026lab7/student-tests/privileges.ts

159 lines
4.4 KiB
TypeScript

import { readFile, writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import {
local,
root,
run,
privateWrite,
requireThat,
compose,
} from "../scripts/runtime";
import { decrypt } from "../scripts/operator-crypto";
import { containerID, containerIP } from "../scripts/runtime-inspect";
import { tlsProbe } from "./transport";
import { images } from "../scripts/topology";
function resp(parts: string[]) {
return (
"*" +
parts.length +
"\r\n" +
parts.map((p) => "$" + Buffer.byteLength(p) + "\r\n" + p + "\r\n").join("")
);
}
export async function auditWorkerPrivileges(password: string) {
const secrets = decrypt<any>(
await readFile(resolve(local, "operator/services.enc"), "utf8"),
password,
);
const env = resolve(local, "operator/pg-worker.env");
await privateWrite(
env,
`PGHOST=127.0.0.1\nPGPORT=15432\nPGDATABASE=messenger\nPGUSER=worker\nPGSSLMODE=disable\nPGGSSENCMODE=disable\n`,
);
const namespace = await containerID("worker-gateway");
const sql = async (query: string, okay = true) => {
const result = await run(
[
"docker",
"run",
"--pull=never",
"--rm",
"--user",
`${process.getuid?.()}:${process.getgid?.()}`,
"--network",
"container:" + namespace,
"--env-file",
env,
"--entrypoint",
"psql",
images.postgres,
"-X",
"-A",
"-t",
"-v",
"ON_ERROR_STOP=1",
"-c",
query,
],
{ allowFailure: !okay },
);
if (!okay)
requireThat(
result.code !== 0 && /permission denied/.test(result.err),
"WORKER_SQL_DENIAL_NOT_PERMISSION",
);
return result;
};
await sql("SELECT count(*) FROM attachments");
await sql("SELECT count(*) FROM attachment_jobs");
for (const table of ["users", "sessions", "chats", "messages"])
await sql("SELECT * FROM " + table + " LIMIT 1", false);
await sql("CREATE TABLE forbidden_worker_schema(id int)", false);
await sql("CREATE ROLE forbidden_worker_role LOGIN", false);
const workerFile = resolve(local, "rendered/worker/s3.json");
const s3 = await run([
"docker",
"run",
"--pull=never",
"--rm",
"--user",
`${process.getuid?.()}:${process.getgid?.()}`,
"--network",
"container:" + namespace,
"--mount",
`type=bind,source=${workerFile},target=/run/worker/s3.json,readonly`,
"--entrypoint",
"/s3-control",
"mtuci-vp7-security-app:local",
"-action",
"prove-worker",
"-config",
"/run/worker/s3.json",
]);
requireThat(
s3.out.trim() ===
"PASS_WORKER_S3_ALLOWED_VARIANT_DENIED_PREFIX_ADMIN_ANONYMOUS",
"WORKER_S3_SCOPE_FAILED",
);
// Authenticate and execute each command in a fresh TLS connection, so queued replies cannot mask an ACL result.
const brokerIP = await containerIP("broker-gateway");
const broker = async (parts: string[]) => {
const input = resp(["AUTH", "worker", secrets.brokerWorker]) + resp(parts);
const result = await tlsProbe(
brokerIP,
6379,
"broker-gateway",
"worker",
input,
"operator",
(data) => data.split("\r\n").filter(Boolean).length >= 2,
);
return result.data;
};
const ping = await broker(["PING"]);
requireThat(ping.includes("+PONG"), "WORKER_BROKER_POSITIVE_FAILED");
for (const command of [
["GET", "forbidden:scope"],
["FLUSHALL"],
["CONFIG", "GET", "*"],
["ACL", "LIST"],
]) {
const result = await broker(command);
requireThat(result.includes("NOPERM"), "WORKER_BROKER_ACL_NOT_DENIED");
}
await writeFile(
resolve(root, "evidence/security/worker-privileges.json"),
JSON.stringify(
{
database: {
allowed: ["attachments", "attachment_jobs"],
denied: [
"users",
"sessions",
"chats",
"messages",
"CREATE TABLE",
"CREATE ROLE",
],
},
s3: {
variant_put_get: true,
forbidden_prefix_denied: true,
bucket_admin_denied: true,
anonymous_object_denied: true,
},
broker: {
ping: true,
forbidden_key_denied: true,
flushall_denied: true,
config_denied: true,
acl_denied: true,
},
},
null,
2,
) + "\n",
);
console.log("PASS_COMPROMISED_WORKER_DB_S3_QUEUE_SCOPE");
}