Skip to content

Commit 81be233

Browse files
committed
fix(audits): include drizzle subpath imports
1 parent 2a3cea0 commit 81be233

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

scripts/check-db-audit-candidates.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ describe('database audit candidate scans', () => {
3131
expect(mayBindDrizzleSql("import { sql as query } from 'drizzle-orm'")).toBe(true)
3232
})
3333

34+
it('finds drizzle sql subpath imports', () => {
35+
expect(mayBindDrizzleSql("import { sql } from 'drizzle-orm/sql'")).toBe(true)
36+
})
37+
3438
it('decodes escaped drizzle module literals', () => {
3539
expect(mayBindDrizzleSql(String.raw`const { sql } = require('drizzle\x2dorm')`)).toBe(true)
3640
})

scripts/check-sql-date-binding.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ const ALLOW_ANNOTATION = '// sql-date-bound:'
2828
const DRIZZLE_MODULE = 'drizzle-orm'
2929
const STATEMENT_TYPE = /(Statement|Declaration)$/
3030

31+
function isDrizzleModule(value: unknown): value is string {
32+
return (
33+
typeof value === 'string' &&
34+
(value === DRIZZLE_MODULE || value.startsWith(`${DRIZZLE_MODULE}/`))
35+
)
36+
}
37+
3138
interface Violation {
3239
file: string
3340
line: number
@@ -223,10 +230,7 @@ function collectSqlBindings(program: SyntaxNode): SqlBindings {
223230
const visit = (node: SyntaxNode) => {
224231
if (node.type === 'ImportDeclaration' && isSyntaxNode(node.source)) {
225232
const source = node.source.value
226-
const isDrizzle =
227-
typeof source === 'string' &&
228-
(source === DRIZZLE_MODULE || source.startsWith(`${DRIZZLE_MODULE}/`))
229-
if (isDrizzle && Array.isArray(node.specifiers)) {
233+
if (isDrizzleModule(source) && Array.isArray(node.specifiers)) {
230234
for (const specifier of node.specifiers) {
231235
if (!isSyntaxNode(specifier) || !isSyntaxNode(specifier.local)) continue
232236
const local = specifier.local.name
@@ -270,10 +274,7 @@ function isDrizzleImportCall(node: unknown): boolean {
270274
const args = Array.isArray(current.arguments) ? current.arguments : []
271275
const source = isSyntaxNode(current.source) ? current.source : args.find(isSyntaxNode)
272276
const value = source?.value
273-
return (
274-
typeof value === 'string' &&
275-
(value === DRIZZLE_MODULE || value.startsWith(`${DRIZZLE_MODULE}/`))
276-
)
277+
return isDrizzleModule(value)
277278
}
278279

279280
const unwrapAwait = (node: SyntaxNode): unknown =>
@@ -562,7 +563,7 @@ export function mayBindDrizzleSql(source: string): boolean {
562563
if (
563564
(token === ts.SyntaxKind.StringLiteral ||
564565
token === ts.SyntaxKind.NoSubstitutionTemplateLiteral) &&
565-
value === DRIZZLE_MODULE
566+
isDrizzleModule(value)
566567
) {
567568
hasDrizzleModule = true
568569
}

0 commit comments

Comments
 (0)