-
Notifications
You must be signed in to change notification settings - Fork 49
Escape prompt search filters #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { NextRequest } from "next/server"; | ||
|
|
||
| const mockFrom = vi.fn(); | ||
|
|
||
| const supabaseClient = { | ||
| from: mockFrom, | ||
| }; | ||
|
|
||
| vi.mock("@/lib/supabase/server", () => ({ | ||
| createClient: vi.fn(() => Promise.resolve(supabaseClient)), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/auth/get-user", () => ({ | ||
| getAuthContext: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/service", () => ({ | ||
| createServiceClient: vi.fn(() => supabaseClient), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/prompts/security-scan", () => ({ | ||
| scanPrompt: vi.fn(), | ||
| })); | ||
|
|
||
| import { GET } from "./route"; | ||
|
|
||
| function makeGetRequest(params: Record<string, string> = {}) { | ||
| const url = new URL("http://localhost/api/prompts"); | ||
| Object.entries(params).forEach(([key, value]) => { | ||
| url.searchParams.set(key, value); | ||
| }); | ||
| return new NextRequest(url, { method: "GET" }); | ||
| } | ||
|
|
||
| function makePromptQuery() { | ||
| const range = vi.fn().mockResolvedValue({ data: [], count: 0, error: null }); | ||
| const order = vi.fn().mockReturnValue({ range }); | ||
| const overlaps = vi.fn().mockReturnValue({ order }); | ||
| const eqAfterSearch = vi.fn().mockReturnValue({ order }); | ||
| const or = vi.fn().mockReturnValue({ eq: eqAfterSearch, overlaps, order }); | ||
| const eq = vi.fn().mockReturnValue({ or, eq: eqAfterSearch, overlaps, order }); | ||
| const select = vi.fn().mockReturnValue({ eq }); | ||
|
|
||
| return { select, eq, or, eqAfterSearch, overlaps, order, range }; | ||
| } | ||
|
|
||
| describe("GET /api/prompts", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("escapes PostgREST filter characters in search terms", async () => { | ||
| const query = makePromptQuery(); | ||
| mockFrom.mockReturnValue(query); | ||
|
|
||
| const response = await GET(makeGetRequest({ search: "ai%,foo_(v1)." })); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(query.or).toHaveBeenCalledWith( | ||
| "title.ilike.%ai\\%\\,foo\\_\\(v1\\)\\.%,description.ilike.%ai\\%\\,foo\\_\\(v1\\)\\.%,tagline.ilike.%ai\\%\\,foo\\_\\(v1\\)\\.%" | ||
| ); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,17 @@ import { createServiceClient } from "@/lib/supabase/service"; | |
| import { promptListingSchema, slugify } from "@/lib/prompts/validation"; | ||
| import { scanPrompt } from "@/lib/prompts/security-scan"; | ||
|
|
||
| function escapePostgrestSearch(value: string) { | ||
| return value | ||
| .replace(/\\/g, "\\\\") | ||
| .replace(/%/g, "\\%") | ||
| .replace(/_/g, "\\_") | ||
| .replace(/,/g, "\\,") | ||
| .replace(/\(/g, "\\(") | ||
| .replace(/\)/g, "\\)") | ||
| .replace(/\./g, "\\."); | ||
| } | ||
|
Comment on lines
+8
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
PostgREST's documented approach for handling reserved characters ( A search for Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
|
|
||
| /** | ||
| * GET /api/prompts - Public listing of active prompts | ||
| */ | ||
|
|
@@ -31,7 +42,10 @@ export async function GET(request: NextRequest) { | |
| .eq("status", "active"); | ||
|
|
||
| if (search) { | ||
| query = query.or(`title.ilike.%${search}%,description.ilike.%${search}%,tagline.ilike.%${search}%`); | ||
| const safeSearch = escapePostgrestSearch(search); | ||
| query = query.or( | ||
| `title.ilike.%${safeSearch}%,description.ilike.%${safeSearch}%,tagline.ilike.%${safeSearch}%` | ||
| ); | ||
| } | ||
|
|
||
| if (category) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test verifies that
or()is called with a specific escaped string, but because Supabase is fully mocked it cannot detect whether PostgREST would accept or correctly interpret that string. A scenario where backslash-escaped commas silently produce wrong query results (or a 400 from PostgREST) would pass this test. An integration test or a test asserting the double-quoting format recommended by PostgREST docs would give much stronger coverage.