משאבים · 63
אחסון במטמון HTTP: הגנה על תגובות מותאמות אישית
הפרדת מטמונים משותפים, פרטיים ויישומים, בדיקת מפתחות, אימות מחדש ופסילה לאחר שינויים בהרשאות גישה.
מעודכן · 6 min
מה מדריך זה עוזר להשיג
- שכבות מלאי
- בחירת שמירה
- בדיקת מפתחות
- אימות מחדש ושינוי תרגיל
בדיקה מהירה
- האם אי-מטמון פירושו ששום דבר לא נשמר?
- האם Vary מגן על זכויות גישה?
- האם כל API צריך להיות מאוחסן במטמון?
שיטה שלב אחר שלב
- 1
שכבות מלאי
רשימת מטמוני דפדפן, CDN, פרוקסי, עובד שירות ויישומים. סיווג תגובות ציבוריות ומותאמות אישית. מדיניות HTTP אינה מתארת באופן אוטומטי אחסון במטמון המיועד בקוד.
תוצר: דיאגרמת שכבות ובעלים.
- 2
בחירת שמירה
RFC 9111 מבחין בין הנחיות: ללא מטמון דורש אימות לפני שימוש חוזר, ללא אחסון אוסר על אחסון HTTP רלוונטי, ופרטי לא מוסמך אינו כולל אחסון משותף. אף אחד מהם לבדו מבטיח סודיות מערכת.
תוצר: מדיניות שמירה לכל תגובה.
- 3
בדיקת מפתחות
בדיקת שיטה, URI ומימדים המשנים ייצוג. שינויים נוגעים לשדות בקשה; זה לא הרשאה. חיפוש שפה שהושמטה, משא ומתן פורמט והקשר חשבון בשכבות יישום.
תוצר: מפתחות ובדיקות הפרדה.
- 4
אימות מחדש ושינוי תרגיל
בדיקת תגובות חדשות וישנות, עדכוני נתונים וזכויות שבוטלו. בדיקת ETag, אימות מותנה והתנהגות של מקור לא זמין. תגובה שהייתה נכונה בעבר עשויה להפוך לרגישה לאחר ביטול.
תוצר: עקבות לפני ואחרי.
- 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.
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.
| Current tag | Received tag | Strong comparison | Weak comparison |
|---|---|---|---|
| "v1" | "v1" | Matches | Matches |
| "v1" | W/"v1" | Does not match | Matches |
| W/"v1" | W/"v1" | Does not match | Matches |
| "v1" | "v2" | Does not match | Does not match |
| "v1" | "V1" | Does not match | Does 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: 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-cacheFictional 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
| Fictional situation | Condition and expected outcome | Interpretation |
|---|---|---|
| Read using a weak tag | GET · "v1"Access allowed · Conditional field optional | W/"v1" and "v1" match weakly. |
| Different client version | GET · "v1"Access allowed · Conditional field optional | The client needs the selected representation. |
| HEAD revalidation | HEAD · "v1"Access allowed · Conditional field optional | The status can be 304; the response has no content. |
| Write based on an old version | PUT · "v2"Access allowed · Conditional field optional | Read again and resolve the conflict before retrying. |
| Weak tag with If-Match | PUT · "v1"Access allowed · Conditional field optional | Strong comparison rejects a weak tag. |
| Required condition absent on PUT | PUT · "v1"Access allowed · Conditional field required | 428 follows the policy announced by this API. |
| Access denied despite matching tag | GET · "v1"Access denied · Conditional field optional | Authorisation precedes HTTP conditions. |
| Write on the correct version | PUT · "v1"Access allowed · Conditional field required | The model accepts PUT and selects a 204 success. |
| If-None-Match: * on existing data | PUT · "v1"Access allowed · Conditional field optional | The wildcard matches any existing representation. |
| If-Match: * on existing data | PUT · W/"v1"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
| Response field | HTTP effect within this scope | Evidence to retain |
|---|---|---|
| Cache-Control: no-cache | Unqualified 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-store | Forbids storage by private and shared HTTP caches. It is not a general privacy guarantee. | Also review logs, history, screenshots and application storage. |
| Cache-Control: private | Unqualified 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. |
| Vary | Adds 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.
- IETF · RFC 9110 — HTTP Semantics · June 2022 · STD 97
§8.8.3.2; §13.1.1–13.1.2; §13.2; §15.3.5; §15.4.5 - IETF · RFC 9111 — HTTP Caching · June 2022 · STD 98
§4.1; §4.3; §5.2.2.4–5.2.2.5; §5.2.2.7 - IETF · RFC 6585 — Additional HTTP Status Codes · April 2012 · Standards Track
§3 · 428 Precondition Required
References reviewed
הפניות רשמיות
הפניות תומכות בשיטה. התאימו את הבדיקות להקשר שלכם; הן אינן הסמכה. כותרות הפניות ומסמכי המקור המקוריים עשויים להיות בשפה אחרת.
תאריך בדיקת המקורות .
