Skip to content

Commit

Permalink
Fix escaping for binding parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Nov 18, 2024
1 parent 9b20ddc commit 2177fd5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ Changelog

To be released.

- Use the explicitly typed binding instead of an implicit one to work
around the stringification issue.
- Fixed a bug where binding parameters have not been properly escaped with
some settings of Postgres.js.

### Version 0.2.1

Expand Down
12 changes: 10 additions & 2 deletions src/kv.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { KvKey, KvStore, KvStoreSetOptions } from "@fedify/fedify";
import type { JSONValue, Sql } from "postgres";
import type { JSONValue, Parameter, Sql } from "postgres";
import { driverSerializesJson } from "./utils.ts";

/**
* Options for the PostgreSQL key-value store.
Expand Down Expand Up @@ -39,6 +40,7 @@ export class PostgresKvStore implements KvStore {
readonly #sql: Sql<{}>;
readonly #tableName: string;
#initialized: boolean;
#driverSerializesJson = false;

/**
* Creates a new PostgreSQL key-value store.
Expand Down Expand Up @@ -84,7 +86,7 @@ export class PostgresKvStore implements KvStore {
INSERT INTO ${this.#sql(this.#tableName)} (key, value, ttl)
VALUES (
${key},
${this.#sql.json(value as JSONValue)},
${this.#json(value)},
${ttl}
)
ON CONFLICT (key)
Expand Down Expand Up @@ -116,6 +118,7 @@ export class PostgresKvStore implements KvStore {
ttl interval
);
`;
this.#driverSerializesJson = await driverSerializesJson(this.#sql);
this.#initialized = true;
}

Expand All @@ -126,4 +129,9 @@ export class PostgresKvStore implements KvStore {
async drop(): Promise<void> {
await this.#sql`DROP TABLE IF EXISTS ${this.#sql(this.#tableName)};`;
}

#json(value: unknown): Parameter {
if (this.#driverSerializesJson) return this.#sql.json(value as JSONValue);
return this.#sql.json(JSON.stringify(value));
}
}
12 changes: 10 additions & 2 deletions src/mq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import type {
MessageQueueEnqueueOptions,
MessageQueueListenOptions,
} from "@fedify/fedify";
import type { Sql } from "postgres";
import type { JSONValue, Parameter, Sql } from "postgres";
import postgres from "postgres";
import { driverSerializesJson } from "./utils.ts";

/**
* Options for the PostgreSQL message queue.
Expand Down Expand Up @@ -61,6 +62,7 @@ export class PostgresMessageQueue implements MessageQueue {
readonly #channelName: string;
readonly #pollIntervalMs: number;
#initialized: boolean;
#driverSerializesJson = false;

constructor(
// deno-lint-ignore ban-types
Expand All @@ -86,7 +88,7 @@ export class PostgresMessageQueue implements MessageQueue {
await this.#sql`
INSERT INTO ${this.#sql(this.#tableName)} (message, delay)
VALUES (
${this.#sql.json(message)},
${this.#json(message)},
${delay.toString()}
);
`;
Expand Down Expand Up @@ -181,6 +183,7 @@ export class PostgresMessageQueue implements MessageQueue {
throw e;
}
}
this.#driverSerializesJson = await driverSerializesJson(this.#sql);
this.#initialized = true;
}

Expand All @@ -190,6 +193,11 @@ export class PostgresMessageQueue implements MessageQueue {
async drop(): Promise<void> {
await this.#sql`DROP TABLE IF EXISTS ${this.#sql(this.#tableName)};`;
}

#json(value: unknown): Parameter {
if (this.#driverSerializesJson) return this.#sql.json(value as JSONValue);
return this.#sql.json(JSON.stringify(value));
}
}

// cSpell: ignore typname
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Sql } from "postgres";

export async function driverSerializesJson(sql: Sql<{}>): Promise<boolean> {
const result = await sql`SELECT ${sql.json('{"foo":1}')}::jsonb AS test;`;
return result[0].test === '{"foo":1}';
}

0 comments on commit 2177fd5

Please sign in to comment.