Cybersecurity 5 min read

Fetch Metadata Headers: Limiting Cross-Site Requests to Reduce CSRF Risk

Learn how to read Sec-Fetch-Site, Sec-Fetch-Mode, and Sec-Fetch-Dest to assess browser request context, reduce CSRF risk, and implement them without breaking application flows.

Kode sumber ditampilkan pada layar laptop

Cross-Site Request Forgery (CSRF) attacks exploit situations where a browser has already stored a user session, then unwittingly sends requests to a site the user trusts. CSRF tokens and SameSite cookies remain essential, but developers can also examine request context through Fetch Metadata Headers. Modern browsers send these headers to explain the relationship between the triggering page and the request destination.

With carefully designed rules, servers can reject cross-site requests that attempt to modify data while still allowing navigation and integrations that are genuinely needed. This mechanism does not replace authentication or CSRF tokens; its function is to add a relatively easy-to-audit layer of defense.

What Are Fetch Metadata Headers?

Fetch Metadata is a set of HTTP headers prefixed with Sec-Fetch- that tell the server the context of a browser request. These headers do not identify the user and do not prove that an action is legitimate. Their values help servers distinguish requests from pages within the same site from those triggered by foreign sites.

  • Sec-Fetch-Site explains the relationship between the source and destination sites: same-origin, same-site, cross-site, or none.
  • Sec-Fetch-Mode describes the fetch mode, such as navigate, cors, no-cors, or same-origin.
  • Sec-Fetch-Dest indicates the destination such as document, image, script, or empty on many fetch requests.
  • Sec-Fetch-User typically has a value of ?1 on navigations triggered by user activation; this header is not present on every request.

Understanding Sec-Fetch-Site Values

The same-origin value indicates a request came from the same origin, including matching scheme, host, and port. Same-site means it remains within the same site context even though origins may differ, such as two subdomains managed by the same organization. Cross-site indicates a different site origin, while none is commonly found on user-initiated navigations such as opening an address or bookmark.

The distinction between site and origin is important to prevent rules that are too loose or too strict. A subdomain that shares a site does not necessarily have the same trust level. Evaluate domain structure, subdomain management, and which endpoints accept cookies before determining policy.

How These Headers Help Reduce CSRF

CSRF typically targets endpoints that change account state, such as changing email addresses, updating profiles, or making transactions. If an endpoint automatically accepts session cookies, the browser could include those cookies on requests triggered by foreign pages according to cookie policies and browser behavior. Fetch Metadata checks give the server an additional signal to reject inappropriate cross-site requests.

A practical starting rule is to separate safe methods from data-modifying methods. GET, HEAD, and OPTIONS should not change application state. For POST, PUT, PATCH, and DELETE, cross-site requests can be rejected by default, unless specific endpoints genuinely require reviewed cross-site flows. Data-modifying endpoints should not use GET merely for convenience.

Designing Server Policy

Start with a simple rule: for endpoints that use browser sessions, reject cross-site data-modifying requests unless there is a legitimate, documented business reason. Do not create broad exceptions such as allowing entire subdomains without review. Exceptions should specify particular endpoints, explain which integration requires access, and be tested whenever the flow changes.

This rule should be complemented by CSRF tokens validated on the server, session cookies with appropriate SameSite attributes, authentication, authorization, and input validation. Context headers do not grant permission to users and do not prove that request content is safe. Every action must still verify whether the user is authorized to perform that operation.

Deployment Strategy Without Breaking Features

  1. Start with logging. Record Sec-Fetch-Site values, methods, and endpoints in your testing environment. Avoid logging cookies, tokens, or personal data.
  2. Map sensitive endpoints. Identify data-modifying operations, then ensure they do not use GET.
  3. Test critical flows. Cover login, logout, forms, payments, uploads, OAuth callbacks, and third-party integrations. Test from multiple browsers and non-browser API clients.
  4. Enable gradually. Apply first to sensitive endpoints, monitor rejection responses, then expand coverage after exceptions are validated.

Payment integrations, federated login, cross-origin uploads, or public APIs may have different requirements. Do not block everything without understanding business flows. If a callback must come from outside your site, validate signatures, state, nonce, or appropriate protocol mechanisms rather than relying solely on context headers.

Limitations and Mistakes to Avoid

Headers prefixed with Sec- are designed so that page JavaScript in the browser cannot modify them arbitrarily. However, non-browser clients can send self-crafted headers, while older browsers may not send them at all. If headers are absent, do not automatically assume the request is safe. Apply controls appropriate to client type and do not use Fetch Metadata as a replacement for authentication or access restriction.

Fetch Metadata is also not a substitute for CSRF tokens, authentication, authorization, or proper origin checks. Use multiple layers of protection and test legitimate application flows, including login, payments, uploads, and third-party integrations.

Implementation Checklist

  • Log Fetch Metadata headers on important requests.
  • Start with monitoring before blocking requests.
  • Apply different rules for navigation, APIs, webhooks, and non-browser clients.
  • Prepare fallback for browsers that do not send these headers.

Conclusion

Fetch Metadata helps servers recognize request context and reduce CSRF risk or unwanted cross-site loading. Use it as an additional layer, establish policy based on endpoint type, and maintain CSRF tokens and access checks required by your application.

Bagikan artikel ini

No comments yet

    Leave a comment

    Your email address will not be published. Required fields are marked *