Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Validate and sandbox third-party API consumption
Treat third-party API responses as untrusted input: validate, sanitize, limit, and avoid following redirects blindly.
Bad example
| 1 | async function enrichAddress(address: string) { |
| 2 | const r = await fetch('http://third-party.local/enrich?address=' + encodeURIComponent(address)); |
| 3 | const data = await r.json(); |
| 4 |
|
| 5 | // BAD: store unvalidated third-party data directly |
| 6 | await db.address.create({ data }); |
| 7 | } |
| 8 |
|
| 9 | async function sendMedicalRecord(payload: unknown) { |
| 10 | // BAD: follows redirects by default; no allowlist; no timeouts |
| 11 | await fetch('https://provider.example.com/store', { |
| 12 | method: 'POST', |
| 13 | body: JSON.stringify(payload), |
| 14 | headers: { 'Content-Type': 'application/json' } |
| 15 | }); |
| 16 | } |
Explanation (EN)
Untrusted third-party data can carry injection payloads; unencrypted transport, missing validation, redirect following, and no timeouts increase exposure and DoS risk.
Objašnjenje (HR)
Third-party podaci su neprovjereni i mogu nositi injection payload. Nešifriran transport, izostanak validacije, praćenje redirecta i bez timeouts povećava rizik i DoS.
Good example
| 1 | const EnrichedAddressSchema = z.object({ |
| 2 | street: z.string().min(1), |
| 3 | city: z.string().min(1), |
| 4 | country: z.string().min(2) |
| 5 | }); |
| 6 |
|
| 7 | async function enrichAddress(address: string) { |
| 8 | const r = await fetchWithSafeguards('https://provider.example.com/enrich', { |
| 9 | method: 'POST', |
| 10 | timeoutMs: 2000, |
| 11 | followRedirects: false, |
| 12 | headers: { 'Content-Type': 'application/json' }, |
| 13 | body: JSON.stringify({ address }) |
| 14 | }); |
| 15 |
|
| 16 | const json = await r.json(); |
| 17 | const parsed = EnrichedAddressSchema.safeParse(json); |
| 18 | if (!parsed.success) throw new Error('Invalid provider response'); |
| 19 |
|
| 20 | // Store only validated, expected fields |
| 21 | await db.address.create({ data: parsed.data }); |
| 22 | } |
Explanation (EN)
Use TLS, enforce timeouts, disable/allowlist redirects, validate response schemas, and sanitize before storing or forwarding third-party data.
Objašnjenje (HR)
Koristi TLS, uvedi timeoute, isključi/allowlistaj redirecte, validiraj response schema i sanitiziraj prije spremanja ili prosljeđivanja third-party podataka.
Notes (EN)
Also: limit response sizes, handle partial outages gracefully, and assess providers' security posture. Treat provider names/fields as untrusted (avoid building SQL/commands from them).
Bilješke (HR)
Također: ograniči veličinu odgovora, graceful degradacija kod outage-a i procijeni sigurnosni posture providera. Tretiraj provider polja/nazive kao untrusted (izbjegni gradnju SQL-a/komandi iz njih).