Skip to content

Commit 2e2cfe4

Browse files
committed
Validate Postgres schema identifier length
1 parent f969848 commit 2e2cfe4

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

packages/openworkflow/postgres/backend.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ describe("BackendPostgres.connect errors", () => {
3838
}),
3939
).rejects.toThrow(/Invalid schema name/);
4040
});
41+
42+
test("throws for schema names longer than 63 bytes", async () => {
43+
await expect(
44+
BackendPostgres.connect(DEFAULT_POSTGRES_URL, {
45+
schema: "a".repeat(64),
46+
}),
47+
).rejects.toThrow(/at most 63 bytes/i);
48+
});
4149
});
4250

4351
describe("BackendPostgres schema option", () => {

packages/openworkflow/postgres/postgres.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ describe("postgres", () => {
4242
test("throws for invalid schema names", () => {
4343
expect(() => migrations("invalid-schema")).toThrow(/Invalid schema name/);
4444
});
45+
46+
test("throws for schema names longer than 63 bytes", () => {
47+
expect(() => migrations("a".repeat(64))).toThrow(/at most 63 bytes/i);
48+
});
4549
});
4650

4751
describe("migrate()", () => {

packages/openworkflow/postgres/postgres.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type Postgres = ReturnType<typeof postgres>;
1010
export type PostgresOptions = Parameters<typeof postgres>[1];
1111

1212
const SCHEMA_NAME_PATTERN = /^[a-zA-Z_]\w*$/;
13+
const MAX_POSTGRES_IDENTIFIER_BYTES = 63;
1314

1415
/**
1516
* newPostgres creates a new Postgres client.
@@ -273,6 +274,12 @@ export function assertValidSchemaName(schema: string): void {
273274
`Invalid schema name "${schema}". Use a Postgres identifier (letters, numbers, underscores; cannot start with a number).`,
274275
);
275276
}
277+
278+
if (Buffer.byteLength(schema, "utf8") > MAX_POSTGRES_IDENTIFIER_BYTES) {
279+
throw new Error(
280+
`Invalid schema name "${schema}". Postgres identifiers must be at most ${String(MAX_POSTGRES_IDENTIFIER_BYTES)} bytes.`,
281+
);
282+
}
276283
}
277284

278285
/**

0 commit comments

Comments
 (0)