← Dedicated courses

SECURITY · GO SERVICES · 19 LESSONS

Secure Go services

Each lesson shows the attack against a small local model and fixes it with the standard library or x/crypto. You learn which component owns each decision, what to test and what a real deployment still needs.

Start with “Authentication and authorization: a verified token is only the beginning” →

Who it is for

Go developers who ship web services and APIs and want a practical, code-first security checklist they can apply in review.

Before you start

Go web basics: handlers, middleware, templates and database access.

What you will be able to do

  • Separate authentication from authorization and control outbound requests and TLS identity.
  • Prevent SQL injection, XSS, CSRF, open redirects, path traversal and command injection.
  • Keep secrets out of logs, compare tokens in constant time, encrypt records with AES-GCM and store passwords with argon2id.
  • Fuzz parsers, audit dependencies with govulncheck and pin your toolchain.

Unit 1. Service security foundations

Authentication and authorization, cryptographic purposes, controlled egress and TLS peer identity.

  1. Authentication and authorization: a verified token is only the beginning

    Validate identity separately from permission to act on a particular resource.

    3 min · Worked lesson

  2. Passwords, MACs and randomness solve different problems

    Choose purpose-specific primitives and bound their cost instead of inventing cryptography.

    3 min · Worked lesson

  3. SSRF: let user input choose data, not an arbitrary network destination

    Keep outbound destinations controlled and validate redirects and connection-time policy.

    3 min · Worked lesson

  4. TLS: verify the peer instead of disabling the error

    Keep certificate-chain and hostname verification while making trust and rotation explicit.

    3 min · Worked lesson

Unit page →

Unit 2. 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

Unit page →

Unit 3. Secrets, cryptography and supply chain

Redaction, constant-time checks, AEAD encryption, password storage, token design, fuzzing and dependency hygiene.

  1. Keep secrets out of logs and errors with a type that refuses to print itself

    Wrap credentials in a Secret type that redacts itself in fmt, slog and JSON, redact credentials in URLs, and load secrets with errors that name the source but never the value.

    6 min · Worked lesson

  2. Compare secrets in constant time: an early return reveals how much of a guess was right

    Recognize timing-dependent comparisons and replace them with crypto/subtle or hmac.Equal over fixed-size digests, including the lookup and error paths around them.

    5 min · Worked lesson

  3. Encrypt records with AES-GCM: random nonces, a key ID, and the record ID as additional data

    Seal application records with AES-GCM so that tampering, row swapping and key rotation are handled explicitly, and explain why a repeated nonce breaks GCM.

    5 min · Worked lesson

  4. Store passwords with argon2id: encoded parameters, rehash on login, one answer for every failure

    Hash passwords with argon2id in a self-describing PHC-style string, upgrade parameters at login, handle bcrypt's 72-byte limit and keep login failures indistinguishable.

    5 min · Worked lesson

  5. Design tokens deliberately: hashed opaque tokens, strictly verified signed tokens, expiry and rotation

    Choose between opaque and signed tokens, store opaque tokens as hashes with rotation and reuse detection, and verify HMAC-signed JWTs with a pinned algorithm and checked claims.

    5 min · Worked lesson

  6. Fuzz the parsers that read untrusted bytes, and bound what they may allocate or inflate

    Write a native Go fuzz test with seed inputs and real invariants for a binary parser, harden the parser against what it finds, and cap decompressed size with io.LimitReader.

    5 min · Worked lesson

  7. Secure the Go supply chain: reachable vulnerabilities, verified modules, pinned tools, auditable binaries

    Run govulncheck in CI, rely on go.sum and the checksum database correctly, pin tools with tool directives, build with -trimpath, and audit a binary's embedded build information.

    6 min · Worked lesson

Unit page →

Related: Gin in production · Around Go: DevOps and reliability