flawopen.com/JavaScript में SQL Injection/Prisma
क्या Prisma $queryRaw SQL Injection से सुरक्षित है?
क्वेरी पैरामीटर को SQL फ़ॉर्म में सीलबंद मान-खाने की तरह समझें: डेटाबेस इसे सिंटैक्स नहीं, डेटा मानता है। $queryRaw का tagged template interpolated मानों को bind करता है। placeholders और arguments अलग देने पर $queryRawUnsafe भी मान bind कर सकता है; अविश्वसनीय पाठ को जोड़कर SQL बनाने पर जोखिम आता है। दोनों में से कोई भी table या column names को parameterize नहीं करता।
const userId = request.query.userId;
const query =
`SELECT id FROM users WHERE id = ${userId}`;
const rows = await prisma.$queryRawUnsafe(query);
const rows = await prisma.$queryRaw`
SELECT id FROM users WHERE id = ${userId}
`;
// PostgreSQL example: $1 binds data, not an identifier.
const columns = { name: 'name', created: 'created_at' } as const;
const column = columns[requestedSort as keyof typeof columns] ?? 'created_at';
const pageSize = 25;
const sortedRows = await prisma.$queryRawUnsafe(
`SELECT id FROM users ORDER BY ${column} LIMIT $1`,
pageSize
);
// The selected SQL structure comes from a fixed map.
Prisma parameters क्या करते हैं — और क्या नहीं
$queryRaw में tagged-template call सीधे इस्तेमाल करें: हर interpolated मान database parameter बनता है। पहले SQL string बनाकर उसे अकेले argument के रूप में न दें। Prisma $queryRawUnsafe(query, ...values) में placeholders भी स्वीकार करता है; इससे मान bind हो सकते हैं, लेकिन SQL text भरोसेमंद होना चाहिए। Parameters डेटा मानों के लिए हैं, identifiers, operators या SQL fragments के लिए नहीं। चुने जा सकने वाले columns को application की fixed allowlist से map करें। कई type-safe raw-query उपयोगों के लिए Prisma TypedSQL की अनुशंसा करता है।
Prisma raw queries का audit कैसे करें
grep -rn "\$queryRawUnsafe\|\$executeRawUnsafe" --include="*.ts" --include="*.js" .अक्सर पूछे जाने वाले प्रश्न
यह raw SQL strings लेता है और अलग से दिए गए मानों के लिए placeholders इस्तेमाल कर सकता है। इसकी flexibility असुरक्षित string बनाने को आसान करती है; SQL text और arguments साथ जाँचें। इससे dynamic identifiers सुरक्षित नहीं होते; उन्हें fixed allowlist से चुनें।
स्रोत
- Prisma ORM दस्तावेज़ — Raw queries और SQL Injection रोकथाम
- Prisma ORM दस्तावेज़ — TypedSQL
- Prisma दस्तावेज़ — Raw database access
- flawopen.com — JavaScript में SQL Injection