Skip to content

feat: standardise PostgREST string escaping across codebase (Closes #1543)#1894

Merged
dipexplorer merged 3 commits into
RatLoopz:mainfrom
nimkarprachi17:feat/standardise-postgrest-escaping
Jun 15, 2026
Merged

feat: standardise PostgREST string escaping across codebase (Closes #1543)#1894
dipexplorer merged 3 commits into
RatLoopz:mainfrom
nimkarprachi17:feat/standardise-postgrest-escaping

Conversation

@nimkarprachi17

Copy link
Copy Markdown
Contributor

🛑 STOP: Assignment & File Scope Check

  • I am assigned to this issue.
  • I verified that this PR ONLY touches the required files.

📋 PR Summary & Link

📸 Proof of Work

  • Zero TypeScript errors (npx tsc --noEmit clean on both api and web)
  • git diff --stat main confirms exactly 6 files changed — all scoped to this issue
  • Pre-existing lint errors in AnalyticsCharts.tsx and CacheStatsCard.tsx are unrelated to this PR

Files changed:

  • apps/api/src/utils/db.ts — added escapePostgrest()
  • apps/web/lib/supabase/utils.ts — new file with escapePostgrest()
  • apps/api/src/routes/interactions.ts — fixed 2 naked .or() filters
  • apps/api/src/routes/scan.ts — fixed 2 naked .or() filters
  • apps/api/src/services/medicineRag.service.ts — fixed 1 naked .or() filter
  • apps/web/app/[locale]/components/SearchBar.tsx — fixed 1 naked .or() filter

🏷️ PR Type

  • type: feature
  • 🔒 type: security
  • ♻️ type: refactor

✅ Checklist

  • My PR has a linked issue (Closes #1543)
  • I have pulled the latest main and resolved any conflicts

@dipexplorer dipexplorer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PostgREST quoting syntax is incorrect and breaks the search.

When you use .ilike.%${escapePostgrest(val)}%, the resulting string evaluates to .ilike.%"val". In PostgREST, double quotes must wrap the entire value to escape commas, not sit inside the % wildcards. Because the quotes are in the middle, PostgREST will either throw a syntax error or search for literal double-quotes.

Additionally, in interactions.ts, escaped is already processed by escapeIlike(). Passing it into escapePostgrest(escaped) causes double-escaping (\\\\, \\%).

How to fix:

  1. Modify escapePostgrest to just escape characters without hardcoding the surrounding double quotes:
export function escapePostgrest(val: string): string {
    return val
        .replace(/\\/g, "\\\\")
        .replace(/%/g, "\\%")
        .replace(/_/g, "\\_")
        .replace(/"/g, '""'); 
}
  1. For .ilike() filters, wrap the entire expression in double quotes in your template literals, placing the % wildcards inside:
.or(`brand_name.ilike."%${escapePostgrest(trimmed)}%",batch_number.ilike."%${escapePostgrest(trimmed)}%"`)

Please update this across all modified files.

@dipexplorer dipexplorer added level:advanced 55 pts and removed level:intermediate 35 pts labels Jun 14, 2026
@nimkarprachi17

Copy link
Copy Markdown
Contributor Author

Fixed all issues raised in review:

  1. escapePostgrest() no longer hardcodes double quotes — they are now placed in the template literals wrapping the entire value: "%${escapePostgrest(val)}%"
  2. Removed escapePostgrest from UUID comparisons in interactions.ts — UUIDs cannot contain commas so no escaping is needed there
  3. Updated consistently across all modified files: interactions.ts, scan.ts, medicineRag.service.ts, SearchBar.tsx, and both utility files

@dipexplorer dipexplorer added quality:clean multiplier x1.2 type:bug Something isn't working labels Jun 15, 2026
@dipexplorer dipexplorer merged commit 339299e into RatLoopz:main Jun 15, 2026
16 of 19 checks passed
@github-project-automation github-project-automation Bot moved this from 📥 Backlog to 🎉 Merged in SahiDawa Workflow Jun 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🎉 Congratulations @nimkarprachi17! Your Pull Request "feat: standardise PostgREST string escaping across codebase (Closes #1543)" has been successfully merged by @dipexplorer.

Thank you for your valuable contribution to SahiDawa! 🇮🇳
If this was for GSSoC 2026, your work is officially merged and valid. Keep up the great work and feel free to claim other open issues. 🚀

Follow us on LinkedIn: https://www.linkedin.com/company/ratloopz/ to get shoutout

This was referenced Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved Approved for gssoc level:advanced 55 pts quality:clean multiplier x1.2 status: open-for-all type:bug Something isn't working type:feature New feature or request

Projects

Status: 🎉 Merged

Development

Successfully merging this pull request may close these issues.

[FEATURE] Standardise PostgREST string escaping across the codebase

2 participants