Skip to content

Commit 244d7b7

Browse files
committed
fix(audit): report the line of a raw sealed column name
1 parent d344d56 commit 244d7b7

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

‎scripts/check-sealed-columns.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ describe('check-sealed-columns', () => {
6060

6161
it('flags physical column names in raw SQL outside tests and the backfill', () => {
6262
const source = [
63-
`await db.execute(sql\`SELECT access_token FROM account WHERE id = \${id}\`)`,
63+
`await db.execute(sql\`SELECT id FROM account WHERE id = \${id}\nAND access_token IS NULL\`)`,
6464
"sql.raw('refresh_token IS NULL')",
6565
`sql\`SELECT id FROM oauth_refresh_token WHERE grant = \${'refresh_token'}\``,
6666
].join('\n')
6767
expect(found(source)).toEqual([
68-
['raw-column-name', 1],
6968
['raw-column-name', 2],
69+
['raw-column-name', 3],
7070
])
7171
expect(found(source, 'lib/file.integration.ts')).toEqual([])
7272
expect(found(source, 'packages/db/script-migrations/0037_seal_account_tokens.ts')).toEqual([])

‎scripts/check-sealed-columns.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,11 @@ export function findViolations(file: string, source: string, columns: SealedColu
143143
const names = new RegExp(`\\b(?:${columns.map((c) => escapeRegExp(c.columnName)).join('|')})\\b`)
144144
for (const pattern of [SQL_TEMPLATE, SQL_RAW]) {
145145
for (const match of source.matchAll(pattern)) {
146-
const body = match[match.length - 1].replace(/\$\{[^}]*\}/g, (value) =>
147-
' '.repeat(value.length)
148-
)
146+
const capture = match[match.length - 1]
147+
const bodyOffset = (match.index ?? 0) + match[0].lastIndexOf(capture)
148+
const body = capture.replace(/\$\{[^}]*\}/g, (value) => ' '.repeat(value.length))
149149
const hit = names.exec(body)
150-
if (hit)
151-
report('raw-column-name', (match.index ?? 0) + match[0].indexOf(body) + hit.index, hit[0])
150+
if (hit) report('raw-column-name', bodyOffset + hit.index, hit[0])
152151
}
153152
}
154153
return violations

0 commit comments

Comments
 (0)