← Secure Go services

SECURITY · GO SERVICES · 8 LESSONS

Web application security

SQL injection, XSS, CSRF, session cookies, path traversal, command injection, open redirects and security headers.

  1. SQL injection: the query text and the user's values must reach the driver separately

    Send untrusted values as database/sql arguments, pick identifiers from allow-lists in code and escape LIKE wildcards with an explicit ESCAPE clause.

    6 min · Worked lesson

  2. html/template escapes by context, and template.HTML is where that protection switches off

    Render untrusted data with html/template, read its context-specific escaping and treat every typed-content conversion as a reviewed trust decision.

    6 min · Worked lesson

  3. CSRF: reject cross-origin writes with Sec-Fetch-Site and http.CrossOriginProtection

    Protect cookie-authenticated endpoints with net/http's CrossOriginProtection, know what SameSite cookies add and recognize when a synchronizer token is still required.

    5 min · Worked lesson

  4. Session cookies: lock down the attributes and keep the session's lifetime on the server

    Issue a __Host- session cookie with the right attributes, rotate the ID at login and enforce idle, absolute and logout expiry in a server-side store.

    5 min · Worked lesson

  5. Path traversal: confine file access with os.Root, because Join and Clean only edit strings

    Open user-named files through os.Root, use filepath.IsLocal and Localize for early rejection and extract archives without zip-slip.

    5 min · Worked lesson

  6. Command injection: run tools without a shell and keep user input out of option position

    Start external tools with exec.CommandContext and a fixed argv, block argument injection with validation and --, and bound the child's time and environment.

    5 min · Worked lesson

  7. Open redirects: accept a single-slash path or an allow-listed host, not any URL that parses

    Validate redirect targets so only same-site paths and allow-listed https URLs pass, including the //host, backslash and userinfo tricks that browsers and net/url read differently.

    4 min · Worked lesson

  8. Security headers: a per-request CSP nonce, HSTS, nosniff, frame-ancestors and Referrer-Policy

    Write middleware that sets a nonce-based Content-Security-Policy and the other baseline security headers, and roll them out without breaking pages or locking in HSTS preload by accident.

    5 min · Worked lesson