Skip to content

Commit 9512e8c

Browse files
ElrondTheSentinel454
authored andcommitted
fix(seed): run psql with -X so psqlrc cannot retarget after the local guard
psql -c no longer implies -X, so ~/.psqlrc or PSQLRC (exportable from .env) ran after the guard and could \connect to another host. Pass -X on both the local and Docker psql paths and assert the control invocation carries it. Signed-off-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
1 parent bfa0417 commit 9512e8c

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

‎crates/buzz-db/tests/observability_source.rs‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,7 +2488,11 @@ fn admin_seed_refuses_remote_database_before_invoking_psql() {
24882488
)
24892489
.expect("copy script without developer .env");
24902490
let psql = root.join("psql");
2491-
std::fs::write(&psql, "#!/bin/sh\ntouch \"$SEED_PSQL_WITNESS\"\nexit 1\n").expect("stub psql");
2491+
std::fs::write(
2492+
&psql,
2493+
"#!/bin/sh\nprintf '%s\\n' \"$@\" > \"$SEED_PSQL_WITNESS\"\nexit 1\n",
2494+
)
2495+
.expect("stub psql");
24922496
std::fs::set_permissions(&psql, std::fs::Permissions::from_mode(0o755))
24932497
.expect("executable stub");
24942498
let witness = root.join("psql-was-invoked");
@@ -2503,15 +2507,16 @@ fn admin_seed_refuses_remote_database_before_invoking_psql() {
25032507
.env("SEED_PSQL_WITNESS", &witness)
25042508
.output()
25052509
.expect("run local seed guard");
2506-
let invoked = witness.exists();
2507-
if invoked {
2510+
let args = std::fs::read_to_string(&witness).ok();
2511+
if args.is_some() {
25082512
std::fs::remove_file(&witness).expect("clear witness");
25092513
}
2510-
(output, invoked)
2514+
(output, args)
25112515
};
25122516
// Control: a local host with a plain database name reaches psql, so the
25132517
// refusals below come from the guard rather than an unrunnable fixture.
2514-
let (_, control_invoked) = run("localhost", "", "", "buzz");
2518+
// It must also pass -X so a psqlrc cannot \connect after the guard.
2519+
let (_, control_args) = run("localhost", "", "", "buzz");
25152520
let cases = [
25162521
("remote host", "shared-db.example.invalid", "", "", ""),
25172522
("address override", "localhost", "192.0.2.1", "", ""),
@@ -2567,11 +2572,15 @@ fn admin_seed_refuses_remote_database_before_invoking_psql() {
25672572
),
25682573
];
25692574
let results = cases.map(|(name, host, address, service, database)| {
2570-
let (output, invoked) = run(host, address, service, database);
2571-
(name, output, invoked)
2575+
let (output, args) = run(host, address, service, database);
2576+
(name, output, args.is_some())
25722577
});
25732578
std::fs::remove_dir_all(root).expect("remove scratch fixture");
2574-
assert!(control_invoked, "plain local target did not reach psql");
2579+
let control_args = control_args.expect("plain local target did not reach psql");
2580+
assert!(
2581+
control_args.lines().any(|arg| arg == "-X"),
2582+
"psql invoked without -X: {control_args:?}"
2583+
);
25752584
for (name, output, invoked) in results {
25762585
assert!(!invoked, "{name} reached psql");
25772586
assert!(!output.status.success(), "{name} accepted");

‎scripts/seed-admin-dashboard.sh‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ if [[ ! "${PGDATABASE}" =~ ^[A-Za-z0-9_-]+$ ]]; then
3838
exit 1
3939
fi
4040

41+
# -X skips psqlrc, which runs after this guard and could \connect elsewhere.
4142
if command -v psql >/dev/null 2>&1; then
4243
run_psql() {
43-
PGPASSWORD="${PGPASSWORD}" psql -h "${PGHOST}" -p "${PGPORT}" \
44+
PGPASSWORD="${PGPASSWORD}" psql -X -h "${PGHOST}" -p "${PGPORT}" \
4445
-U "${PGUSER}" -d "${PGDATABASE}" "$@"
4546
}
4647
elif docker exec buzz-postgres psql --version >/dev/null 2>&1; then
4748
run_psql() {
4849
docker exec -i -e PGPASSWORD="${PGPASSWORD}" buzz-postgres \
49-
psql -U "${PGUSER}" -d "${PGDATABASE}" "$@"
50+
psql -X -U "${PGUSER}" -d "${PGDATABASE}" "$@"
5051
}
5152
else
5253
echo "error: neither psql nor buzz-postgres docker psql is available" >&2

0 commit comments

Comments
 (0)