Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Match % and _ literally when filtering the audit log by resource (#1926)

Regan Bellbe9b3c46f2d9
diff --git a/src/admin/postgres-audit-log.ts b/src/admin/postgres-audit-log.ts
index 7a5e0cdfa..7b70d4a6f 100644
--- a/src/admin/postgres-audit-log.ts
+++ b/src/admin/postgres-audit-log.ts
@@ -2,6 +2,7 @@ import { createPgPool } from "../persistence/pg-pool.ts";
import type { ScopeId } from "../types.ts";
import type { AuditEvent, AuditLog } from "../audit/audit-log.ts";
import { reportFailureAs } from "../util/errors.ts";
+import { likeContains } from "../persistence/like.ts";
function rowToEvent(r: Record<string, unknown>): AuditEvent {
return {
@@ -105,8 +106,8 @@ export function createPostgresAuditLog(connectionString: string): AuditLog {
conds.push(`at >= $${params.length}`);
}
if (resourceContains !== undefined) {
- params.push(`%${resourceContains}%`);
- conds.push(`resource LIKE $${params.length}`);
+ params.push(likeContains(resourceContains));
+ conds.push(`resource LIKE $${params.length} ESCAPE '\\'`);
}
const where = conds.length ? `WHERE ${conds.join(" AND ")}` : "";
params.push(limit);
diff --git a/src/directory/postgres-directory-store.ts b/src/directory/postgres-directory-store.ts
index 7e39f33d6..47a23f3e6 100644
--- a/src/directory/postgres-directory-store.ts
+++ b/src/directory/postgres-directory-store.ts
@@ -1,3 +1,4 @@
+import { likeEscape } from "../persistence/like.ts";
import { orgId as configOrgId } from "../config.ts";
import { createHash } from "node:crypto";
import { createPgPool, type PoolClient, withPgTransaction } from "../persistence/pg-pool.ts";
@@ -144,10 +145,6 @@ function hashRoster(rows: string[]): string {
return createHash("sha256").update(rows.slice().sort().join("\n")).digest("hex");
}
-function likeEscape(s: string): string {
- return s.replace(/([\\%_])/g, "\\$1");
-}
-
function dedupPairs<T>(rows: T[], idOf: (r: T) => string, pidOf: (r: T) => string): T[] {
const seen = new Map<string, Set<string>>();
const out: T[] = [];
diff --git a/src/files/postgres-file-artifact-store.ts b/src/files/postgres-file-artifact-store.ts
index c6e1d811a..d717887db 100644
--- a/src/files/postgres-file-artifact-store.ts
+++ b/src/files/postgres-file-artifact-store.ts
@@ -1,3 +1,4 @@
+import { likeContains } from "../persistence/like.ts";
import { createPgPool, withPgTransaction } from "../persistence/pg-pool.ts";
import type { ScopeId } from "../types.ts";
import type { DurableByteStore } from "./durable-byte-store.ts";
@@ -105,8 +106,8 @@ export function createPostgresFileArtifactStore(
filters.push(`created_in_scope = $${params.length}::text`);
}
if (opts?.nameQuery != null) {
- params.push(`%${opts.nameQuery.replace(/[\\%_]/g, (c) => `\\${c}`)}%`);
- filters.push(`name ILIKE $${params.length}::text`);
+ params.push(likeContains(opts.nameQuery));
+ filters.push(`name ILIKE $${params.length}::text ESCAPE '\\'`);
}
const visible = `SELECT * FROM file_artifacts WHERE ${filters.join(" AND ")}`;
const documents =
diff --git a/src/persistence/like.ts b/src/persistence/like.ts
new file mode 100644
index 000000000..8e0aa19d0
--- /dev/null
+++ b/src/persistence/like.ts
@@ -0,0 +1,7 @@
+export function likeEscape(s: string): string {
+ return s.replace(/[\\%_]/g, "\\$&");
+}
+
+export function likeContains(s: string): string {
+ return `%${likeEscape(s)}%`;
+}
diff --git a/test/postgres-audit-log.test.ts b/test/postgres-audit-log.test.ts
index 55dede2fc..7f722346e 100644
--- a/test/postgres-audit-log.test.ts
+++ b/test/postgres-audit-log.test.ts
@@ -87,3 +87,24 @@ test("pg audit log: recordOnce is durable and idempotent across instances", { sk
assert.equal(events.length, 1);
assert.equal(events[0]?.action, "layer-updated");
});
+
+test("pg audit log: resource filter treats % and _ as literal characters", { skip }, async () => {
+ await reset(true);
+ const log = createPostgresAuditLog(URL!);
+ const s1 = scopeId("personal", "U1");
+ log.record({ ...ev(1, "file.read", s1), resource: "my_report.pdf" });
+ log.record({ ...ev(2, "file.read", s1), resource: "myXreport.pdf" });
+ log.record({ ...ev(3, "file.read", s1), resource: "100% done" });
+ log.record({ ...ev(4, "file.read", s1), resource: "100 done" });
+
+ const underscore = await log.tail({ limit: 10, resourceContains: "my_report" });
+ assert.deepEqual(
+ underscore.map((e) => e.resource),
+ ["my_report.pdf"],
+ );
+ const percent = await log.tail({ limit: 10, resourceContains: "100%" });
+ assert.deepEqual(
+ percent.map((e) => e.resource),
+ ["100% done"],
+ );
+});