-
Notifications
You must be signed in to change notification settings - Fork 0
🛡️ Sentinel: [CRITICAL] Fix SSRF in bulk lookup API #195
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
base: main
Are you sure you want to change the base?
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,7 @@ | ||||||||||||||||
| import { NextRequest, NextResponse } from "next/server"; | ||||||||||||||||
| import { POST as urlLookup } from "@/app/api/lookup/url/route"; | ||||||||||||||||
| import { POST as doiLookup } from "@/app/api/lookup/doi/route"; | ||||||||||||||||
| import { POST as isbnLookup } from "@/app/api/lookup/isbn/route"; | ||||||||||||||||
|
|
||||||||||||||||
| interface LookupResult { | ||||||||||||||||
| input: string; | ||||||||||||||||
|
|
@@ -22,27 +25,26 @@ export async function POST(request: NextRequest) { | |||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Refactored to process lookups concurrently for performance improvement | ||||||||||||||||
| const baseUrl = request.nextUrl.origin; | ||||||||||||||||
|
|
||||||||||||||||
| const lookupPromises = items.map(async (item) => { | ||||||||||||||||
| const trimmedItem = item.trim(); | ||||||||||||||||
| if (!trimmedItem) { | ||||||||||||||||
| return { input: item, success: false, error: "Empty input" }; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| try { | ||||||||||||||||
| let apiEndpoint: string; | ||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||||||||||||||
| let handler: (req: NextRequest) => Promise<any>; | ||||||||||||||||
| let body: object; | ||||||||||||||||
|
|
||||||||||||||||
| // Detect input type | ||||||||||||||||
| if (trimmedItem.match(/^(https?:\/\/|www\.)/i)) { | ||||||||||||||||
| apiEndpoint = "/api/lookup/url"; | ||||||||||||||||
| handler = urlLookup; | ||||||||||||||||
| body = { url: trimmedItem }; | ||||||||||||||||
|
Comment on lines
40
to
42
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. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Normalize This branch accepts Proposed fix if (trimmedItem.match(/^(https?:\/\/|www\.)/i)) {
handler = urlLookup;
- body = { url: trimmedItem };
+ body = { url: trimmedItem.match(/^www\./i) ? `https://${trimmedItem}` : trimmedItem };📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
| } else if (trimmedItem.match(/^10\.\d{4,}/)) { | ||||||||||||||||
| apiEndpoint = "/api/lookup/doi"; | ||||||||||||||||
| handler = doiLookup; | ||||||||||||||||
| body = { doi: trimmedItem }; | ||||||||||||||||
| } else if (trimmedItem.match(/^(97[89])?\d{9}[\dXx]$/)) { | ||||||||||||||||
| apiEndpoint = "/api/lookup/isbn"; | ||||||||||||||||
| handler = isbnLookup; | ||||||||||||||||
| body = { isbn: trimmedItem }; | ||||||||||||||||
| } else { | ||||||||||||||||
| return { | ||||||||||||||||
|
|
@@ -52,13 +54,13 @@ export async function POST(request: NextRequest) { | |||||||||||||||
| }; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Make the API call | ||||||||||||||||
| const response = await fetch(`${baseUrl}${apiEndpoint}`, { | ||||||||||||||||
| // Directly invoke the handler to prevent SSRF via Host header manipulation | ||||||||||||||||
| const syntheticRequest = new NextRequest(new URL('http://localhost'), { | ||||||||||||||||
| method: "POST", | ||||||||||||||||
| headers: { "Content-Type": "application/json" }, | ||||||||||||||||
| body: JSON.stringify(body), | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| const response = await handler(syntheticRequest); | ||||||||||||||||
| const data = await response.json(); | ||||||||||||||||
|
|
||||||||||||||||
| if (response.ok && data.data) { | ||||||||||||||||
|
|
||||||||||||||||
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.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Validate item element types before trimming.
itemsis only checked as an array, so a payload like{ "items": [123] }makesitem.trim()reject and turns the whole bulk request into a 500.Proposed fix
if (!items || !Array.isArray(items) || items.length === 0) { return NextResponse.json({ error: "Items array is required" }, { status: 400 }); } + + if (!items.every((item): item is string => typeof item === "string")) { + return NextResponse.json({ error: "All items must be strings" }, { status: 400 }); + }📝 Committable suggestion
🤖 Prompt for AI Agents