Edge Computing with Cloudflare Workers & Deno · Lektion

Eingabebereinigung und Schutz vor Injection

Schützen Sie Edge-Anwendungen vor XSS, SQL-Injection und verwandten Angriffen, indem Sie Eingaben bereinigen und Ausgaben korrekt encodieren.

Lektion 4 von 413 Schritte

Eingabebereinigung und Schutz vor Injection ist eine kostenlose Edge Computing with Cloudflare Workers & Deno-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Edge Computing with Cloudflare Workers & Deno-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Edge Computing with Cloudflare Workers & Deno-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Why Sanitization Matters

Even at the edge, untrusted input is the root of most attacks. Sanitization and proper output encoding stop:

  • Cross-Site Scripting (XSS)
  • SQL / query injection
  • Header and log injection

Validation checks shape, sanitization makes input safe to use.

Understanding XSS

XSS happens when attacker-controlled data is rendered as HTML and executes as script.

If a Worker echoes user input into a page without encoding, an attacker can inject scripts.

// Dangerous: user input goes straight into HTML
const html = '<div>' + userInput + '</div>';

Output Encoding for HTML

The fix for XSS is context-aware output encoding. Escape HTML-special characters before rendering.

function escapeHtml(s) {
  return s
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#39;');
}

Preventing SQL Injection

Never build SQL by string concatenation. Use parameterized queries, the D1 and Deno drivers bind values safely.

// Safe: bound parameter, never concatenated
const { results } = await env.DB
  .prepare('SELECT * FROM users WHERE email = ?')
  .bind(email)
  .all();

The Danger of Concatenation

Concatenated SQL lets an attacker break out of the intended query.

// NEVER do this
const sql = "SELECT * FROM users WHERE email = '" + email + "'";
// email = "' OR '1'='1" returns every row

Validate Then Sanitize

Combine both defenses: validate that input matches an expected pattern, then sanitize for the context it is used in.

const emailRe = /^[^@\s]+@[^@\s]+\.[^@\s]+$/;
if (!emailRe.test(email)) {
  return new Response('Invalid email', { status: 400 });
}

Header & Redirect Injection

User input placed into response headers or redirect URLs can inject newlines or open redirects.

  • Strip CR/LF from header values
  • Allowlist redirect destinations
const clean = value.replace(/[\r\n]/g, '');
headers.set('X-User-Tag', clean);

Content Security Policy

A CSP header is a strong second line of defense against XSS, it restricts what scripts may run.

headers.set(
  'Content-Security-Policy',
  "default-src 'self'; script-src 'self'"
);

Sanitizing Rich HTML

When you must accept HTML (e.g. user comments), use a vetted sanitizer library rather than regex, allowlist safe tags and attributes.

import DOMPurify from 'isomorphic-dompurify';
const safe = DOMPurify.sanitize(userHtml);

Defense in Depth

No single control is enough. Layer defenses:

  • Validate input shape
  • Use parameterized queries
  • Encode output per context
  • Set CSP and security headers

If one layer fails, the others still protect you.

Best Practices Summary

To keep edge apps safe:

  • Treat all input as hostile
  • Never concatenate SQL or HTML with raw input
  • Encode for the exact output context
  • Add CSP and strip control characters from headers

Quick Check

What is the most reliable way to prevent SQL injection in a D1 query?

Recap

You hardened your app against injection:

  • Encode output to stop XSS
  • Use parameterized queries to stop SQL injection
  • Strip control characters and allowlist redirects
  • Add CSP and sanitize rich HTML with a trusted library

Defense in depth keeps edge applications resilient even when one layer slips.

Kostenlos starten

Lerne Edge Computing with Cloudflare Workers & Deno mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
12
Lektionen
47

Häufig gestellte Fragen

Ist die Lektion „Eingabebereinigung und Schutz vor Injection“ kostenlos?

Ja — der vollständige Text von „Eingabebereinigung und Schutz vor Injection“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Edge Computing with Cloudflare Workers & Deno-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Edge Computing with Cloudflare Workers & Deno-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Eingabebereinigung und Schutz vor Injection“?

Schützen Sie Edge-Anwendungen vor XSS, SQL-Injection und verwandten Angriffen, indem Sie Eingaben bereinigen und Ausgaben korrekt encodieren. Du übst Edge Computing with Cloudflare Workers & Deno mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Edge Computing with Cloudflare Workers & Deno zu starten?

Keine Vorkenntnisse erforderlich. Edge Computing with Cloudflare Workers & Deno auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Eingabebereinigung und Schutz vor Injection“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Edge Computing with Cloudflare Workers & Deno-Lektion Code schreiben und ausführen?

Ja. Jede Edge Computing with Cloudflare Workers & Deno-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Authentifizierung und Autorisierung
  2. Rate Limiting und DDoS-Schutz
  3. Sicheres Secrets-Management
  4. Eingabebereinigung und Schutz vor Injection
← Zurück zu Edge Computing with Cloudflare Workers & Deno