mediumCWE-20API10:2023

Unsafe API Consumption

Your API blindly trusts responses from third-party APIs without validation, allowing attackers to exploit upstream services to compromise your application.

How It Works

When your API consumes data from third-party services, it often trusts the response implicitly. If an upstream API is compromised or returns unexpected data, your application processes it without sanitization. This can lead to injection attacks, data corruption, or business logic bypass. Attackers may compromise a less-secured third-party API or perform man-in-the-middle attacks to inject malicious payloads that your application processes as trusted data.

Vulnerable Code
app.get('/api/enrichment/:email', async (req, res) => {
  const data = await fetch(`https://api.third-party.com/lookup?email=${req.params.email}`);
  const profile = await data.json();
  await db.query(`UPDATE users SET name = '${profile.name}' WHERE email = '${req.params.email}'`);
  res.json(profile);
});
Secure Code
import { z } from 'zod';
const ProfileSchema = z.object({ name: z.string().max(100), company: z.string().optional() });

app.get('/api/enrichment/:email', async (req, res) => {
  const data = await fetch(`https://api.third-party.com/lookup?email=${encodeURIComponent(req.params.email)}`);
  const raw = await data.json();
  const profile = ProfileSchema.parse(raw);
  await db.query('UPDATE users SET name = $1 WHERE email = $2', [profile.name, req.params.email]);
  res.json(profile);
});

Real-World Example

In 2021, Codecov's Bash Uploader was compromised, injecting malicious code that exfiltrated environment variables from CI/CD pipelines. Companies like Twilio, HashiCorp, and Confluent that consumed Codecov's script without integrity verification were affected.

How to Prevent It

  • Validate and sanitize all data received from third-party APIs with schemas
  • Use parameterized queries even with trusted data sources
  • Verify TLS certificates and use certificate pinning for critical integrations
  • Set timeouts and circuit breakers for external API calls

Affected Technologies

Node.jsPythonGoJavaPHPC#

Data Hogo detects this vulnerability automatically.

Scan Your Repo Free

Related Vulnerabilities