fix(security): sanitize SQL interpolation in Cloudflare Analytics Engine queries#1702
fix(security): sanitize SQL interpolation in Cloudflare Analytics Engine queries#1702artylobos wants to merge 3 commits intoCap-go:mainfrom
Conversation
…ine queries Add escapeSqlString() utility to prevent SQL injection via user-controlled parameters (deviceIds, search, actions, version_name, cursor) interpolated into Cloudflare Analytics Engine SQL queries in readDevicesCF and readStatsCF. Addresses GHSA-f83x-p28r-pf74.
📝 WalkthroughWalkthroughAdds a private helper Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
supabase/functions/_backend/utils/cloudflare.ts (1)
670-696: Consider escapingparams.app_idtoo for full consistency.Nice coverage on the changed filters. For defense-in-depth, I’d align the base app filters as well (Line 558 and Line 721) so all interpolated string inputs follow the same policy.
Suggested patch
- const conditions: string[] = [`index1 = '${params.app_id}'`] + const conditions: string[] = [`index1 = '${escapeSqlString(params.app_id)}'`]-WHERE - app_id = '${params.app_id}' ${deviceFilter} ${actionsFilter} ${searchFilter} ${startFilter} ${endFilter} +WHERE + app_id = '${escapeSqlString(params.app_id)}' ${deviceFilter} ${actionsFilter} ${searchFilter} ${startFilter} ${endFilter}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@supabase/functions/_backend/utils/cloudflare.ts` around lines 670 - 696, The app_id value is interpolated into SQL strings without using escapeSqlString; update all places that reference params.app_id (the base app filters earlier in the file and the later app filter near where searchFilter/deviceFilter/actionsFilter are built) to pass through escapeSqlString(params.app_id) before embedding; use the same escapeSqlString helper and keep the surrounding single-quote interpolation pattern so variables like deviceFilter, actionsFilter and searchFilter remain consistent and safe.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@supabase/functions/_backend/utils/cloudflare.ts`:
- Around line 670-696: The app_id value is interpolated into SQL strings without
using escapeSqlString; update all places that reference params.app_id (the base
app filters earlier in the file and the later app filter near where
searchFilter/deviceFilter/actionsFilter are built) to pass through
escapeSqlString(params.app_id) before embedding; use the same escapeSqlString
helper and keep the surrounding single-quote interpolation pattern so variables
like deviceFilter, actionsFilter and searchFilter remain consistent and safe.
…ency Wrap all app_id and params.app_id values with escapeSqlString() where they are interpolated into Analytics Engine SQL query strings. This addresses CodeRabbit review feedback on PR Cap-go#1702 requesting defense-in-depth consistency with the existing escaping applied to deviceIds, search, etc.
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (4)
supabase/functions/_backend/utils/cloudflare.ts (4)
1452-1452:⚠️ Potential issue | 🟠 MajorMissing escaping:
org_idnot sanitized in Analytics Engine query.This user-controlled parameter is interpolated directly, unlike
app_idwhich is escaped in other admin functions.🛡️ Proposed fix
- const orgFilter = org_id ? `AND blob2 = '${org_id}'` : '' + const orgFilter = org_id ? `AND blob2 = '${escapeSqlString(org_id)}'` : ''🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@supabase/functions/_backend/utils/cloudflare.ts` at line 1452, The org_id is interpolated unsafely into the SQL fragment via orgFilter (const orgFilter = org_id ? `AND blob2 = '${org_id}'` : ''), so change this to use a safe, parameterized query or at minimum escape/sanitize org_id before interpolation; update the code that builds the query to pass org_id as a bound parameter (or replace single quotes in org_id with escaped quotes) and reference blob2 and orgFilter accordingly so user-controlled org_id cannot inject SQL.
1634-1634:⚠️ Potential issue | 🟠 MajorMissing escaping:
org_idnot sanitized in Analytics Engine query.Same issue as in
getAdminPlatformOverview— user-controlled parameter should be escaped.🛡️ Proposed fix
- const orgFilter = org_id ? `AND blob2 = '${org_id}'` : '' + const orgFilter = org_id ? `AND blob2 = '${escapeSqlString(org_id)}'` : ''🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@supabase/functions/_backend/utils/cloudflare.ts` at line 1634, The orgFilter currently interpolates raw org_id into the SQL string (const orgFilter = org_id ? `AND blob2 = '${org_id}'` : ''), which allows injection; update the code in the Analytics Engine query to stop direct string interpolation of org_id and instead pass org_id as a parameter or use the same sanitization used in getAdminPlatformOverview (e.g., a parameterized query or an escape helper) so that org_id is safely bound/escaped before constructing orgFilter or removed from string concatenation entirely; locate usages of orgFilter and the org_id binding in this module to apply the parameterized binding consistently.
485-486:⚠️ Potential issue | 🟡 MinorInconsistent escaping:
channelNameshould useescapeSqlString.The inline escaping only handles single quotes, missing backslash escaping. For consistency and completeness with the rest of this security fix, use the new helper.
🛡️ Proposed fix
- const safeChannel = channelName ? channelName.replace(/'/g, `''`) : '' + const safeChannel = channelName ? escapeSqlString(channelName) : ''🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@supabase/functions/_backend/utils/cloudflare.ts` around lines 485 - 486, The code currently builds safeChannel by manually replacing single quotes (safeChannel = channelName ? channelName.replace(/'/g, `''`) : ''), which is inconsistent with the rest of the codebase; update it to use the escapeSqlString helper instead (use escapeSqlString(channelName) when channelName is truthy) and then use that escaped value to build channelFilter (`channelFilter = escaped ? \`AND default_channel = '${escaped}'\` : ''`), leaving the channelName and channelFilter symbols in place so the change is localized and consistent with other SQL escaping calls.
1008-1024:⚠️ Potential issue | 🟠 MajorD1 SQL injection:
appIdis not parameterized.While outside the immediate scope of this Analytics Engine fix, this D1 query interpolates
appIddirectly without escaping or parameterization. D1 supports bound parameters (as used elsewhere in this file, e.g.,createIfNotExistStoreInfo).🛡️ Proposed fix using parameterized query
export async function getStoreAppByIdCF(c: Context, appId: string): Promise<StoreApp> { if (!c.env.DB_STOREAPPS) return Promise.resolve({} as StoreApp) - const query = `SELECT * FROM store_apps WHERE app_id = '${appId}' LIMIT 1` + const query = `SELECT * FROM store_apps WHERE app_id = ? LIMIT 1` cloudlog({ requestId: c.get('requestId'), message: 'getStoreAppByIdCF query', query }) try { const readD1 = getD1ReadStoreAppSession(c) .prepare(query) + .bind(appId) .first() const res = await readD1 return res🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@supabase/functions/_backend/utils/cloudflare.ts` around lines 1008 - 1024, The getStoreAppByIdCF function currently interpolates appId into the SQL string causing a D1 SQL injection risk; change the query to use a parameter placeholder (e.g. "SELECT * FROM store_apps WHERE app_id = ? LIMIT 1") and call getD1ReadStoreAppSession(c).prepare(query).bind(appId).first() instead of directly interpolating appId into the template, keeping the existing try/catch and return behavior (function: getStoreAppByIdCF; methods: getD1ReadStoreAppSession, prepare, bind, first).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@supabase/functions/_backend/utils/cloudflare.ts`:
- Line 1452: The org_id is interpolated unsafely into the SQL fragment via
orgFilter (const orgFilter = org_id ? `AND blob2 = '${org_id}'` : ''), so change
this to use a safe, parameterized query or at minimum escape/sanitize org_id
before interpolation; update the code that builds the query to pass org_id as a
bound parameter (or replace single quotes in org_id with escaped quotes) and
reference blob2 and orgFilter accordingly so user-controlled org_id cannot
inject SQL.
- Line 1634: The orgFilter currently interpolates raw org_id into the SQL string
(const orgFilter = org_id ? `AND blob2 = '${org_id}'` : ''), which allows
injection; update the code in the Analytics Engine query to stop direct string
interpolation of org_id and instead pass org_id as a parameter or use the same
sanitization used in getAdminPlatformOverview (e.g., a parameterized query or an
escape helper) so that org_id is safely bound/escaped before constructing
orgFilter or removed from string concatenation entirely; locate usages of
orgFilter and the org_id binding in this module to apply the parameterized
binding consistently.
- Around line 485-486: The code currently builds safeChannel by manually
replacing single quotes (safeChannel = channelName ? channelName.replace(/'/g,
`''`) : ''), which is inconsistent with the rest of the codebase; update it to
use the escapeSqlString helper instead (use escapeSqlString(channelName) when
channelName is truthy) and then use that escaped value to build channelFilter
(`channelFilter = escaped ? \`AND default_channel = '${escaped}'\` : ''`),
leaving the channelName and channelFilter symbols in place so the change is
localized and consistent with other SQL escaping calls.
- Around line 1008-1024: The getStoreAppByIdCF function currently interpolates
appId into the SQL string causing a D1 SQL injection risk; change the query to
use a parameter placeholder (e.g. "SELECT * FROM store_apps WHERE app_id = ?
LIMIT 1") and call
getD1ReadStoreAppSession(c).prepare(query).bind(appId).first() instead of
directly interpolating appId into the template, keeping the existing try/catch
and return behavior (function: getStoreAppByIdCF; methods:
getD1ReadStoreAppSession, prepare, bind, first).
|
Hi @riderx 👋 This PR fixes multiple SQL injection vulnerabilities in The fix introduces an Could you take a look when you get a chance? These are security-critical fixes that protect against SQL injection attacks on the Cloudflare D1 database layer. Thanks! |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (4)
supabase/functions/_backend/utils/cloudflare.ts (4)
1452-1452:⚠️ Potential issue | 🟠 MajorMissing escape:
org_idnot sanitized.The
org_idparameter is interpolated without escaping, inconsistent with the PR's goal of sanitizing all user-controlled inputs.🐛 Proposed fix
- const orgFilter = org_id ? `AND blob2 = '${org_id}'` : '' + const orgFilter = org_id ? `AND blob2 = '${escapeSqlString(org_id)}'` : ''🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@supabase/functions/_backend/utils/cloudflare.ts` at line 1452, The org_id is directly interpolated into orgFilter causing an injection risk; instead, stop building SQL via string concatenation and either use a parameterized/bound query for blob2 or sanitize/escape org_id before concatenation. Locate the orgFilter assignment in cloudflare.ts (variable orgFilter, referenced blob2 and org_id) and change it to use a query parameter placeholder with the org_id bound by the DB client (preferred), or if binding is not available, validate org_id against an allowed pattern and escape single quotes before inserting.
485-486:⚠️ Potential issue | 🟡 MinorInconsistent escaping:
channelNamebypassesescapeSqlString.This inline escape only handles single quotes but not backslashes, making it inconsistent with the new
escapeSqlStringfunction and potentially vulnerable.🐛 Proposed fix
- const safeChannel = channelName ? channelName.replace(/'/g, `''`) : '' + const safeChannel = channelName ? escapeSqlString(channelName) : ''🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@supabase/functions/_backend/utils/cloudflare.ts` around lines 485 - 486, The code builds safeChannel and channelFilter using channelName.replace(...) which only escapes single quotes; replace that logic to call the existing escapeSqlString(channelName) helper (handling null/undefined) so escaping is consistent and backslashes are handled—i.e., compute safeChannel = channelName ? escapeSqlString(channelName) : '' and then build channelFilter = safeChannel ? `AND default_channel = '${safeChannel}'` : '' (use the escape function where safeChannel is referenced).
1008-1025:⚠️ Potential issue | 🟠 MajorD1 SQL injection:
appIdnot parameterized.Unlike other D1 queries in this file that use
.bind()for parameters, this function directly interpolatesappIdinto the SQL string. IfappIdoriginates from user input, this is vulnerable to SQL injection.🐛 Proposed fix using prepared statement
export async function getStoreAppByIdCF(c: Context, appId: string): Promise<StoreApp> { if (!c.env.DB_STOREAPPS) return Promise.resolve({} as StoreApp) - const query = `SELECT * FROM store_apps WHERE app_id = '${appId}' LIMIT 1` + const query = `SELECT * FROM store_apps WHERE app_id = ? LIMIT 1` cloudlog({ requestId: c.get('requestId'), message: 'getStoreAppByIdCF query', query }) try { const readD1 = getD1ReadStoreAppSession(c) .prepare(query) + .bind(appId) .first() const res = await readD1🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@supabase/functions/_backend/utils/cloudflare.ts` around lines 1008 - 1025, The SQL in getStoreAppByIdCF interpolates appId directly causing SQL injection risk; change the query to use a parameter placeholder (e.g. "WHERE app_id = ? LIMIT 1"), then call getD1ReadStoreAppSession(c).prepare(query).bind(appId).first() so the appId is bound instead of concatenated; keep existing error logging (cloudlog/cloudlogErr) and return behavior but replace the string-interpolated query and ensure any cloudlog that prints the query does not leak raw unbound values.
1634-1634:⚠️ Potential issue | 🟠 MajorMissing escape:
org_idnot sanitized.Same issue as in
getAdminPlatformOverview—theorg_idparameter needs escaping.🐛 Proposed fix
- const orgFilter = org_id ? `AND blob2 = '${org_id}'` : '' + const orgFilter = org_id ? `AND blob2 = '${escapeSqlString(org_id)}'` : ''🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@supabase/functions/_backend/utils/cloudflare.ts` at line 1634, The org_id value is interpolated into SQL without escaping, allowing injection; update the code that builds orgFilter (the const orgFilter = org_id ? `AND blob2 = '${org_id}'` : ''`) to use a safe parameterized query or at minimum escape single quotes (e.g., replace all ' with ''), and prefer switching the calling function (the code path that uses orgFilter, same pattern as getAdminPlatformOverview) to pass org_id as a bound parameter to the query rather than string interpolation so blob2 comparison is safe.
🧹 Nitpick comments (1)
supabase/functions/_backend/utils/cloudflare.ts (1)
13-16: Good addition for SQL injection defense.The escape function correctly handles single quotes and backslashes for ClickHouse SQL literals. For additional hardening, consider also stripping null bytes (
\x00) which could cause string truncation in some SQL implementations:🛡️ Optional: strip null bytes for defense-in-depth
function escapeSqlString(value: string): string { - return value.replace(/'/g, '\'\'').replace(/\\/g, '\\\\') + return value.replace(/\x00/g, '').replace(/'/g, '\'\'').replace(/\\/g, '\\\\') }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@supabase/functions/_backend/utils/cloudflare.ts` around lines 13 - 16, The escapeSqlString function currently escapes single quotes and backslashes but should also strip null bytes to avoid potential string truncation; update escapeSqlString to remove any '\x00' characters (e.g., apply a .replace(/\x00/g, '') or equivalent) in addition to the existing replaces so the function consistently sanitizes null bytes, single quotes, and backslashes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@supabase/functions/_backend/utils/cloudflare.ts`:
- Line 1452: The org_id is directly interpolated into orgFilter causing an
injection risk; instead, stop building SQL via string concatenation and either
use a parameterized/bound query for blob2 or sanitize/escape org_id before
concatenation. Locate the orgFilter assignment in cloudflare.ts (variable
orgFilter, referenced blob2 and org_id) and change it to use a query parameter
placeholder with the org_id bound by the DB client (preferred), or if binding is
not available, validate org_id against an allowed pattern and escape single
quotes before inserting.
- Around line 485-486: The code builds safeChannel and channelFilter using
channelName.replace(...) which only escapes single quotes; replace that logic to
call the existing escapeSqlString(channelName) helper (handling null/undefined)
so escaping is consistent and backslashes are handled—i.e., compute safeChannel
= channelName ? escapeSqlString(channelName) : '' and then build channelFilter =
safeChannel ? `AND default_channel = '${safeChannel}'` : '' (use the escape
function where safeChannel is referenced).
- Around line 1008-1025: The SQL in getStoreAppByIdCF interpolates appId
directly causing SQL injection risk; change the query to use a parameter
placeholder (e.g. "WHERE app_id = ? LIMIT 1"), then call
getD1ReadStoreAppSession(c).prepare(query).bind(appId).first() so the appId is
bound instead of concatenated; keep existing error logging
(cloudlog/cloudlogErr) and return behavior but replace the string-interpolated
query and ensure any cloudlog that prints the query does not leak raw unbound
values.
- Line 1634: The org_id value is interpolated into SQL without escaping,
allowing injection; update the code that builds orgFilter (the const orgFilter =
org_id ? `AND blob2 = '${org_id}'` : ''`) to use a safe parameterized query or
at minimum escape single quotes (e.g., replace all ' with ''), and prefer
switching the calling function (the code path that uses orgFilter, same pattern
as getAdminPlatformOverview) to pass org_id as a bound parameter to the query
rather than string interpolation so blob2 comparison is safe.
---
Nitpick comments:
In `@supabase/functions/_backend/utils/cloudflare.ts`:
- Around line 13-16: The escapeSqlString function currently escapes single
quotes and backslashes but should also strip null bytes to avoid potential
string truncation; update escapeSqlString to remove any '\x00' characters (e.g.,
apply a .replace(/\x00/g, '') or equivalent) in addition to the existing
replaces so the function consistently sanitizes null bytes, single quotes, and
backslashes.
|
|
Hi @riderx 👋 The lint issue has been fixed in the latest commit. Could you please approve the workflow run so CI can complete? The fix adds proper SQL escaping for user inputs in Cloudflare Analytics queries to prevent injection attacks. Happy to make any changes if needed. Thanks! |



Summary
Fixes SQL injection vulnerabilities in Cloudflare Analytics Engine queries (GHSA-f83x-p28r-pf74).
User-controlled string parameters were interpolated directly into SQL query strings without sanitization in
readDevicesCFandreadStatsCF, allowing an attacker to break out of string literals and inject arbitrary SQL.Changes
escapeSqlString()utility function that escapes single quotes and backslashes for safe SQL string interpolationescapeSqlString()to all user-controlled values interpolated into Analytics Engine SQL queriesAffected Functions
readDevicesCFparams.deviceIds— single value and IN-list interpolationparams.search— search filter interpolation (both with and without deviceIds)params.version_name— version filter interpolationparams.cursor— cursor-based pagination (timestamp and device_id components)readStatsCFparams.deviceIds— single value and IN-list interpolation (the IN-list was critically missing quotes entirely)params.actions— single value and IN-list interpolationparams.search— search filter interpolation (both with and without deviceIds)Critical Fix
The
readStatsCFIN-list fordeviceIdswas particularly dangerous — values were joined without any quoting at all, making injection trivial./claim #1667
Summary by CodeRabbit