feat(safety): add production environment safeguards
This commit is contained in:
1 parent
be66d6f46b
commit
1be66fb115
80 files changed
+4220
-234
No files matched your search
@@ -0,0 +1,119 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { executeWithProductionSqlGuard } from "../productionExecutionGuard";
|
||||
import { useProductionSafetyStore } from "@/stores/productionSafetyStore";
|
||||
import type { ConnectionConfig } from "@/types/database";
|
||||
|
||||
function connection(overrides: Partial<ConnectionConfig> = {}): ConnectionConfig {
|
||||
return {
|
||||
id: "conn-1",
|
||||
name: "Operations",
|
||||
db_type: "mysql",
|
||||
host: "db.internal",
|
||||
port: 3306,
|
||||
username: "operator",
|
||||
password: "",
|
||||
production_databases: ["prod_app"],
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("production SQL execution guard", () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
});
|
||||
|
||||
it("waits for confirmation before executing production SQL", async () => {
|
||||
const execute = vi.fn().mockResolvedValue("done");
|
||||
const pendingExecution = executeWithProductionSqlGuard({
|
||||
connection: connection(),
|
||||
database: "prod_app",
|
||||
sql: "DELETE FROM users WHERE id = 1",
|
||||
source: "Schema diff",
|
||||
execute,
|
||||
});
|
||||
|
||||
await Promise.resolve();
|
||||
const store = useProductionSafetyStore();
|
||||
expect(store.pending).toMatchObject({
|
||||
sql: "DELETE FROM users WHERE id = 1",
|
||||
database: "prod_app",
|
||||
productionDatabases: ["prod_app"],
|
||||
source: "Schema diff",
|
||||
});
|
||||
expect(execute).not.toHaveBeenCalled();
|
||||
|
||||
store.confirm();
|
||||
await expect(pendingExecution).resolves.toBe("done");
|
||||
expect(execute).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("cancels production SQL without executing the callback", async () => {
|
||||
const execute = vi.fn().mockResolvedValue("done");
|
||||
const pendingExecution = executeWithProductionSqlGuard({
|
||||
connection: connection(),
|
||||
database: "prod_app",
|
||||
sql: "TRUNCATE TABLE users",
|
||||
source: "Object tree",
|
||||
execute,
|
||||
});
|
||||
|
||||
await Promise.resolve();
|
||||
useProductionSafetyStore().cancel();
|
||||
|
||||
await expect(pendingExecution).resolves.toBeUndefined();
|
||||
expect(execute).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("queues concurrent production confirmations", async () => {
|
||||
const firstExecute = vi.fn().mockResolvedValue("first");
|
||||
const secondExecute = vi.fn().mockResolvedValue("second");
|
||||
const firstSql = "DELETE FROM prod_app.users WHERE id = 1";
|
||||
const secondSql = "DELETE FROM prod_app.audit_log WHERE id = 2";
|
||||
const firstExecution = executeWithProductionSqlGuard({
|
||||
connection: connection(),
|
||||
database: "staging",
|
||||
sql: firstSql,
|
||||
source: "Schema diff",
|
||||
execute: firstExecute,
|
||||
});
|
||||
const secondExecution = executeWithProductionSqlGuard({
|
||||
connection: connection(),
|
||||
database: "staging",
|
||||
sql: secondSql,
|
||||
source: "Data compare",
|
||||
execute: secondExecute,
|
||||
});
|
||||
|
||||
await Promise.resolve();
|
||||
const store = useProductionSafetyStore();
|
||||
expect(store.pending?.sql).toBe(firstSql);
|
||||
expect(firstExecute).not.toHaveBeenCalled();
|
||||
expect(secondExecute).not.toHaveBeenCalled();
|
||||
|
||||
store.confirm();
|
||||
await expect(firstExecution).resolves.toBe("first");
|
||||
expect(firstExecute).toHaveBeenCalledTimes(1);
|
||||
expect(store.pending?.sql).toBe(secondSql);
|
||||
|
||||
store.cancel();
|
||||
await expect(secondExecution).resolves.toBeUndefined();
|
||||
expect(secondExecute).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("executes non-production SQL immediately", async () => {
|
||||
const execute = vi.fn().mockResolvedValue("done");
|
||||
await expect(
|
||||
executeWithProductionSqlGuard({
|
||||
connection: connection(),
|
||||
database: "staging",
|
||||
sql: "UPDATE staging.users SET active = 1 WHERE id = 1",
|
||||
source: "Data compare",
|
||||
execute,
|
||||
}),
|
||||
).resolves.toBe("done");
|
||||
|
||||
expect(useProductionSafetyStore().pending).toBeUndefined();
|
||||
expect(execute).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,117 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { assessProductionSql, isProductionMutation, productionContextForDatabase } from "../productionSafety";
|
||||
import type { ConnectionConfig, DatabaseType } from "@/types/database";
|
||||
|
||||
interface ProductionSafetyCorpusCase {
|
||||
name: string;
|
||||
dialect: DatabaseType;
|
||||
productionDatabases: string[];
|
||||
activeDatabase: string;
|
||||
sql: string;
|
||||
active: boolean;
|
||||
isMutation: boolean;
|
||||
databases: string[];
|
||||
}
|
||||
|
||||
const productionSafetyCorpus = JSON.parse(readFileSync(new URL("../../../../../../tests/fixtures/production-safety-corpus.json", import.meta.url), "utf8")) as ProductionSafetyCorpusCase[];
|
||||
|
||||
function connection(overrides: Partial<ConnectionConfig> = {}): ConnectionConfig {
|
||||
return {
|
||||
id: "conn-1",
|
||||
name: "Operations",
|
||||
db_type: "mysql",
|
||||
host: "db.internal",
|
||||
port: 3306,
|
||||
username: "readonly",
|
||||
password: "",
|
||||
production_databases: ["prod_app"],
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("production SQL safety", () => {
|
||||
it("marks an explicitly production connection regardless of database", () => {
|
||||
expect(productionContextForDatabase(connection({ is_production: true }), "scratch").active).toBe(true);
|
||||
});
|
||||
|
||||
it("marks only configured production databases for multi-database connections", () => {
|
||||
expect(productionContextForDatabase(connection(), "PROD_APP").active).toBe(true);
|
||||
expect(productionContextForDatabase(connection(), "staging").active).toBe(false);
|
||||
});
|
||||
|
||||
it("detects a write after a USE production switch despite comments", () => {
|
||||
const assessment = assessProductionSql("-- install\nUSE `prod_app`; /* migration */ DELETE FROM users", connection(), "staging");
|
||||
expect(assessment).toMatchObject({ active: true, isMutation: true, databases: ["prod_app"] });
|
||||
});
|
||||
|
||||
it("detects qualified production targets in multi-statement SQL", () => {
|
||||
const assessment = assessProductionSql("SELECT ';' AS literal; DELETE FROM `prod_app`.`orders`; UPDATE staging.users SET active = 1", connection(), "staging");
|
||||
expect(assessment).toMatchObject({ active: true, isMutation: true, databases: ["prod_app"] });
|
||||
});
|
||||
|
||||
it("detects production database DDL without a selected production database", () => {
|
||||
expect(assessProductionSql("DROP DATABASE IF EXISTS prod_app", connection(), "staging")).toMatchObject({ active: true, isMutation: true, databases: ["prod_app"] });
|
||||
});
|
||||
|
||||
it("detects production writes hidden behind parser-sensitive SQL forms", () => {
|
||||
for (const sql of ["EXPLAIN ANALYZE DELETE FROM prod_app.users WHERE id = 1", "/*! DELETE FROM prod_app.users WHERE id = 1 */", "COPY prod_app.users FROM '/tmp/users.csv'", "SELECT * INTO prod_app.backup_users FROM users", "SELECT * FROM prod_app.users INTO OUTFILE '/tmp/users.csv'"]) {
|
||||
expect(assessProductionSql(sql, connection(), "staging")).toMatchObject({ active: true, isMutation: true, databases: ["prod_app"] });
|
||||
}
|
||||
});
|
||||
|
||||
it("matches the shared SQL target safety corpus", () => {
|
||||
for (const corpusCase of productionSafetyCorpus) {
|
||||
const assessment = assessProductionSql(
|
||||
corpusCase.sql,
|
||||
connection({
|
||||
db_type: corpusCase.dialect,
|
||||
production_databases: corpusCase.productionDatabases,
|
||||
}),
|
||||
corpusCase.activeDatabase,
|
||||
);
|
||||
expect(
|
||||
{
|
||||
active: assessment.active,
|
||||
isMutation: assessment.isMutation,
|
||||
databases: assessment.databases,
|
||||
},
|
||||
corpusCase.name,
|
||||
).toEqual({
|
||||
active: corpusCase.active,
|
||||
isMutation: corpusCase.isMutation,
|
||||
databases: corpusCase.databases,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("detects qualified procedure calls and privilege targets", () => {
|
||||
for (const sql of ["CALL prod_app.purge_users()", "CALL `prod_app`.`purge_users`()", "GRANT ALL ON prod_app.* TO 'u'@'%'", "GRANT EXECUTE ON PROCEDURE prod_app.purge_users TO 'u'@'%'"]) {
|
||||
expect(assessProductionSql(sql, connection(), "staging")).toMatchObject({ active: true, isMutation: true, databases: ["prod_app"] });
|
||||
}
|
||||
});
|
||||
|
||||
it("allows resolved non-production procedure and privilege targets", () => {
|
||||
expect(assessProductionSql("CALL staging.purge_users()", connection(), "staging")).toMatchObject({ active: false, isMutation: true });
|
||||
expect(assessProductionSql("GRANT ALL ON staging.* TO 'u'@'%'", connection(), "staging")).toMatchObject({ active: false, isMutation: true });
|
||||
});
|
||||
|
||||
it("conservatively confirms ambiguous production targets", () => {
|
||||
for (const sql of ["CALL purge_users()", "GRANT PROCESS ON *.* TO 'u'@'%'", "GRANT ALL ON users TO 'u'@'%'", "CREATE USER 'u'@'%'"]) {
|
||||
expect(assessProductionSql(sql, connection(), "staging")).toMatchObject({ active: true, isMutation: true, databases: ["prod_app"] });
|
||||
}
|
||||
});
|
||||
|
||||
it("does not treat read-only qualified references as write targets", () => {
|
||||
expect(assessProductionSql("SELECT * FROM prod_app.orders; DELETE FROM staging.users WHERE id = 1", connection(), "staging")).toMatchObject({ active: false, isMutation: true });
|
||||
});
|
||||
|
||||
it("treats unrecognized SQL as a production mutation until proven read-only", () => {
|
||||
expect(isProductionMutation("MAINTAIN UNKNOWN THING")).toBe(true);
|
||||
expect(assessProductionSql("MAINTAIN UNKNOWN THING", connection(), "prod_app")).toMatchObject({ active: true, isMutation: true });
|
||||
});
|
||||
|
||||
it("does not require a production confirmation for reads", () => {
|
||||
expect(assessProductionSql("SELECT * FROM prod_app.orders", connection(), "staging")).toMatchObject({ active: false, isMutation: false });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user