资源 · 63

HTTP 缓存:保护个性化响应

分离共享缓存、私有缓存和应用程序缓存,检查密钥,并在访问权限更改后进行重新验证和失效处理。

已更新 · 12 min

无人值守的安全服务器与网络机房通道 由人工智能生成的编辑插图

本指南有助于实现的目标

  • 清单层
  • 选择保留策略
  • 检查密钥
  • 执行重新验证和更改

快速检查

  • 无缓存是否意味着不保留任何内容?
  • Vary 是否保护访问权限?
  • 每个 API 都应该缓存吗?

分步指南

  1. 1

    清单层

    列出浏览器、CDN、代理、Service Worker 和应用程序缓存。 对公共响应和个性化响应进行分类。 HTTP 策略不会自动描述代码中实现的缓存。

    交付物:层和所有者图。

  2. 2

    选择保留策略

    RFC 9111 区分了指令:no-cache 要求在重用前进行验证,no-store 禁止相关的 HTTP 存储,而 unqualified private 排除共享存储。 单独使用 None 并不能保证系统机密性。

    交付成果:每个响应的保留策略。

  3. 3

    检查密钥

    检查方法、URI 和维度如何改变表示形式。 Vary 关注请求字段;它不涉及授权。 在应用层查找省略的语言、格式协商和帐户上下文。

    交付成果:密钥和分离测试。

  4. 4

    执行重新验证和更改

    测试新响应和过期响应、数据更新和已撤销的权限。 检查 ETag、条件验证和源不可用行为。 撤销后,以前正确的响应可能变得敏感。

    交付成果:攻击前后的跟踪记录。

  5. 5

    使用两个账户进行重放攻击。

    使用虚拟身份和标记。 交替使用热缓存和冷缓存向同一 URI 发送请求。 在不记录真实令牌的情况下,检查注销、直接链接和日志记录。

    交付成果:负面测试和失效策略。

同时衡量缓存速度与新鲜度

虚构示例:数据不代表客户或实际观察结果。

情境

两次读取都很快,但数据源变化后,其中一次仍显示旧的可用状态。

检查

比较数值、版本、读取位置和变更后的时间,按系统约定测试失效、过期以及访问原始来源。

行动

按用途明确可接受的过时程度并监测差异。延迟降低不能证明已提供数据的一致性。

可重复使用的工作表

填写您已授权的观察结果。 这些字段是工作模板,而非观察结果。

字段待记录信息
响应公共或个性化;维度
层缓存、所有者和密钥
策略保留、新鲜度和验证
试验虚构帐户、更改和观察

虚构示例

示例情境

虚构示例:CDN 对两个测试帐户在同一 URI 处重复使用配置文件响应。

决策及预期证据

该场景检测泄露,检查策略和密钥,然后在纠正和撤销访问权限后重放。

区分机制

机制目的验证或限制
无缓存允许存储并进行必要的验证不要将其解读为禁止存储
不限定的私有排除共享缓存不要将其解读为保密性保证
无存储阻止相关的 HTTP 存储分别检查应用程序缓存和历史记录

管理指标

指标衡量内容首要行动
账户分离禁止来自其他账户的标记检查正文、链接和元数据
过期时间和验证按照策略重用测试过期时间和不可用源
失效延迟直到更正表示的时间命名每个仍然过期的层

常见错误

  • 不要将其解读为禁止存储
  • 不要将其解读为保密性保证
  • 分别检查应用程序缓存和历史记录

常见问题解答

无缓存是否意味着不保留任何内容?

否。 它要求在重用之前进行验证;它不像无存储那样禁止存储。

Vary 是否保护访问权限?

否。它参与表示选择; 授权仍然独立进行。

每个 API 都应该缓存吗?

否。收益取决于成本、新鲜度和风险。 敏感响应可能需要避免共享缓存。

ETag、HTTP 缓存与并发更新

此语言版本的详细参考说明以英语提供。

ETag: revalidate responses and prevent lost updates

An ETag is an opaque validator for the selected representation. It compares versions; it is neither authorisation nor an access token, and it need not be a file hash. Personalised responses require separate decisions about access, cache selection and HTTP conditions.

Local teaching model: a representation already exists; at most one conditional field contains a single ETag or *. GET, HEAD and PUT are covered. Tag lists, date validators, ranges, creation, combined conditions and the already-succeeded modification exception are excluded. No network call, storage or state change occurs; this does not test your API.

虚构的无人网络实验室,琥珀色灯光照亮深色工作台上的网关设备。 由人工智能生成的编辑插图
Generated conceptual scene: a fictional empty network laboratory. It shows neither a real facility nor a protocol measurement.

Strong and weak ETag comparison serve different purposes

If-Match requires strong comparison: neither tag may start with W/. If-None-Match uses weak comparison, matching the opaque values regardless of W/. Values remain case-sensitive. The * wildcard checks representation existence here, not a particular version.

Strong and weak ETag comparison serve different purposes
Current tagReceived tagStrong comparisonWeak comparison
"v1""v1"MatchesMatches
"v1"W/"v1"Does not matchMatches
W/"v1"W/"v1"Does not matchMatches
"v1""v2"Does not matchDoes not match
"v1""V1"Does not matchDoes not match

Simulate an HTTP decision without contacting a server

The six parameters yield 432 combinations. Requiring a conditional field and selecting 204 for successful PUT are policies of this fictional scenario. Requiring any condition does not prevent every lost update: If-Match: * compares no version.

Model decision : 304

304: If-None-Match finds a weak match for GET or HEAD. Reuse the stored representation under cache rules with the revalidation metadata.

304 and 204: no response content.

Request excerpt

GET /demo/resource HTTP/1.1
Host: api.example.test
If-None-Match: W/"v1"

Response excerpt

HTTP/1.1 304 Not Modified
Date: Thu, 22 Oct 2026 00:00:00 GMT
ETag: "v1"
Cache-Control: private, no-cache

Fictional HTTP/1.1 excerpts for reading: bodies are omitted even where allowed. api.example.test is an example domain. The date is fixed. This model does not update the ETag after PUT; the separate walkthrough follows successive versions.

Ten reproducible situations

Ten reproducible situations
Fictional situationCondition and expected outcomeInterpretation
Read using a weak tagGET · "v1"
If-None-Match: W/"v1" → 304

Access allowed · Conditional field optional

W/"v1" and "v1" match weakly.
Different client versionGET · "v1"
If-None-Match: "v2" → 200

Access allowed · Conditional field optional

The client needs the selected representation.
HEAD revalidationHEAD · "v1"
If-None-Match: "v1" → 304

Access allowed · Conditional field optional

The status can be 304; the response has no content.
Write based on an old versionPUT · "v2"
If-Match: "v1" → 412

Access allowed · Conditional field optional

Read again and resolve the conflict before retrying.
Weak tag with If-MatchPUT · "v1"
If-Match: W/"v1" → 412

Access allowed · Conditional field optional

Strong comparison rejects a weak tag.
Required condition absent on PUTPUT · "v1"
none → 428

Access allowed · Conditional field required

428 follows the policy announced by this API.
Access denied despite matching tagGET · "v1"
If-None-Match: "v1" → 403

Access denied · Conditional field optional

Authorisation precedes HTTP conditions.
Write on the correct versionPUT · "v1"
If-Match: "v1" → 204

Access allowed · Conditional field required

The model accepts PUT and selects a 204 success.
If-None-Match: * on existing dataPUT · "v1"
If-None-Match: * → 412

Access allowed · Conditional field optional

The wildcard matches any existing representation.
If-Match: * on existing dataPUT · W/"v1"
If-Match: * → 204

Access allowed · Conditional field required

The wildcard passes even with a weak current tag: it does not protect a version.

Four rules for a personalised response

Four rules for a personalised response
Response fieldHTTP effect within this scopeEvidence to retain
Cache-Control: no-cacheUnqualified no-cache permits storage, but any reuse needs successful validation with the origin server.Observe a conditional round trip; do not read no-cache as a storage prohibition.
Cache-Control: no-storeForbids storage by private and shared HTTP caches. It is not a general privacy guarantee.Also review logs, history, screenshots and application storage.
Cache-Control: privateUnqualified private forbids storage by a shared cache; a private cache may store under the other constraints.Distinguish browser, proxy and CDN. A directive does not replace access checks.
VaryAdds the named request fields to selection of a stored response. It does not decide who may access a resource.Test relevant variants. Do not assume a cookie or Authorization automatically prevents all caching.

Useful API acceptance evidence

  • Expected request and status: method, test resource, access rights, conditional field and current ETag. Use fictional data and remove secrets.
  • Observed response: status, ETag, Date, Cache-Control, Vary and no content for HEAD, 204 or 304. A 304 must carry the required metadata that would have accompanied the 200.
  • Concurrency: verify that a stale write fails without changing the resource; retain the conflict-resolution decision. Do not blindly replace the ETag and resend the same write.

Follow the two-client conflict →

Primary references and limits

The standards define HTTP conditions and caching. The ten cases, laboratory and two-client walkthrough are original illustrations. Displayed results are calculated by this local model, not measurements of this site or your infrastructure.

References reviewed

官方参考文献

参考文献支持该方法。 请根据您的实际情况调整检查;它们并非认证。 原始参考文献标题和源文档可能使用其他语言。

参考资料查阅日期 .