A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.html 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <!doctype html><!-- This is a valid HTML5 document. -->
  2. <!-- Screen readers, SEO, extensions and so on. -->
  3. <html lang="fr">
  4. <!-- Has to be within the first 1024 bytes, hence before the `title` element
  5. See: https://www.w3.org/TR/2012/CR-html5-20121217/document-metadata.html#charset -->
  6. <meta charset="utf-8">
  7. <!-- Why no `X-UA-Compatible` meta: https://stackoverflow.com/a/6771584 -->
  8. <!-- The viewport meta is quite crowded and we are responsible for that.
  9. See: https://codepen.io/tigt/post/meta-viewport-for-2015 -->
  10. <meta name="viewport" content="width=device-width,initial-scale=1">
  11. <!-- Required to make a valid HTML5 document. -->
  12. <title>HTTPWTF (archive) — David Larlet</title>
  13. <meta name="description" content="Publication mise en cache pour en conserver une trace.">
  14. <!-- That good ol' feed, subscribe :). -->
  15. <link rel="alternate" type="application/atom+xml" title="Feed" href="/david/log/">
  16. <!-- Generated from https://realfavicongenerator.net/ such a mess. -->
  17. <link rel="apple-touch-icon" sizes="180x180" href="/static/david/icons2/apple-touch-icon.png">
  18. <link rel="icon" type="image/png" sizes="32x32" href="/static/david/icons2/favicon-32x32.png">
  19. <link rel="icon" type="image/png" sizes="16x16" href="/static/david/icons2/favicon-16x16.png">
  20. <link rel="manifest" href="/static/david/icons2/site.webmanifest">
  21. <link rel="mask-icon" href="/static/david/icons2/safari-pinned-tab.svg" color="#07486c">
  22. <link rel="shortcut icon" href="/static/david/icons2/favicon.ico">
  23. <meta name="msapplication-TileColor" content="#f7f7f7">
  24. <meta name="msapplication-config" content="/static/david/icons2/browserconfig.xml">
  25. <meta name="theme-color" content="#f7f7f7" media="(prefers-color-scheme: light)">
  26. <meta name="theme-color" content="#272727" media="(prefers-color-scheme: dark)">
  27. <!-- Documented, feel free to shoot an email. -->
  28. <link rel="stylesheet" href="/static/david/css/style_2021-01-20.css">
  29. <!-- See https://www.zachleat.com/web/comprehensive-webfonts/ for the trade-off. -->
  30. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_regular.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  31. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_bold.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  32. <link rel="preload" href="/static/david/css/fonts/triplicate_t4_poly_italic.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" crossorigin>
  33. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_regular.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  34. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_bold.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  35. <link rel="preload" href="/static/david/css/fonts/triplicate_t3_italic.woff2" as="font" type="font/woff2" media="(prefers-color-scheme: dark)" crossorigin>
  36. <script>
  37. function toggleTheme(themeName) {
  38. document.documentElement.classList.toggle(
  39. 'forced-dark',
  40. themeName === 'dark'
  41. )
  42. document.documentElement.classList.toggle(
  43. 'forced-light',
  44. themeName === 'light'
  45. )
  46. }
  47. const selectedTheme = localStorage.getItem('theme')
  48. if (selectedTheme !== 'undefined') {
  49. toggleTheme(selectedTheme)
  50. }
  51. </script>
  52. <meta name="robots" content="noindex, nofollow">
  53. <meta content="origin-when-cross-origin" name="referrer">
  54. <!-- Canonical URL for SEO purposes -->
  55. <link rel="canonical" href="https://httptoolkit.tech/blog/http-wtf/">
  56. <body class="remarkdown h1-underline h2-underline h3-underline em-underscore hr-center ul-star pre-tick" data-instant-intensity="viewport-all">
  57. <article>
  58. <header>
  59. <h1>HTTPWTF</h1>
  60. </header>
  61. <nav>
  62. <p class="center">
  63. <a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
  64. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
  65. </svg> Accueil</a> •
  66. <a href="https://httptoolkit.tech/blog/http-wtf/" title="Lien vers le contenu original">Source originale</a>
  67. </p>
  68. </nav>
  69. <hr>
  70. <p>HTTP is fundamental to modern development, from frontend to backend to mobile. But like any widespread mature standard, it's got some funky skeletons in the closet.</p>
  71. <p>Some of these skeletons are little-known but genuinely useful features, some of them are legacy oddities relied on by billions of connections daily, and some of them really shouldn't exist at all. Let's look behind the curtain:</p>
  72. <h2 id="no-cache-means-do-cache"><a href="#no-cache-means-do-cache" aria-label="no cache means do cache permalink" class="anchor"><svg aria-hidden="true" focusable="false" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>No-cache means "do cache"</h2>
  73. <p>Caching has never been easy, but HTTP cache headers can be particularly confusing. The worst examples of this are <code class="language-text">no-cache</code> and <code class="language-text">private</code>. What does the below response header do?</p>
  74. <div class="gatsby-highlight" data-language="text">
  75. <pre class="language-text"><code class="language-text">Cache-Control: private, no-cache</code></pre>
  76. </div>
  77. <p>It looks like this means "don't store this response anywhere", right?</p>
  78. <p><em>Hahaha</em> no.</p>
  79. <p>In reality, this means "please store this response in all browser caches, but revalidate it when using it". In fact, this makes responses <em>more</em> cacheable, because this applies even to responses that wouldn't normally be cacheable by default.</p>
  80. <p>Specifically, <code class="language-text">no-cache</code> means that your content is explicitly cacheable, but whenever a browser or CDN wants to use it, they should send a request using <code class="language-text">If-Match</code> or <code class="language-text">If-Modified-Since</code> to ask the server whether the cache is still up to date first. Meanwhile <code class="language-text">private</code> means that this content is cacheable, but only in end-client browsers, not CDNs or proxies.</p>
  81. <p>If you were trying to disable caching because the response contains security or privacy sensitive data that shouldn't be stored elsewhere, you're now in big trouble. In reality, you probably wanted <code class="language-text">no-store</code>.</p>
  82. <p>If you send a response including a <code class="language-text">Cache-Control: no-store</code> header, nobody will ever cache the response, and it'll come fresh from the server every time. The only edge case is if you send that when a client already has a cached response, which this won't remove. If you want to do that and clear existing caches too, add <code class="language-text">max-age=0</code>.</p>
  83. <p>Twitter notably <a href="https://hacks.mozilla.org/2020/04/twitter-direct-message-caching-and-firefox/">hit this issue</a>. They used <code class="language-text">Pragma: no-cache</code> (a legacy version of the same header) when they should have used <code class="language-text">Cache-Control: no-store</code>, and accidentally persisted every user's private direct messages in their browser caches. That's not a big problem on your own computer, but if you share a computer or you use Twitter on a public computer somewhere, you've now left all your private messages conveniently unencrypted &amp; readable on the hard drive. Oops.</p>
  84. <h2 id="http-trailers"><a href="#http-trailers" aria-label="http trailers permalink" class="anchor"><svg aria-hidden="true" focusable="false" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>HTTP Trailers</h2>
  85. <p>You're probably aware of HTTP headers. An HTTP message starts with a first line that contains the method &amp; URL (for requests) or status code &amp; message (for responses) and then it has a series of key/value pairs for metadata, called headers, and then it has a body.</p>
  86. <p>Did you know you can also send trailers, to append metadata <em>after</em> a message body?</p>
  87. <p>These are not widely used, but they're fully standardized and in theory everything should support them, or at least ignore them. They can be useful if you have metadata that isn't easily available initially, and you don't want need to wait for it before you send the body.</p>
  88. <p>They are used in some API protocols like gRPC, and they're primarily valuable for metadata about the overall response itself, for example you can use trailers to <a href="https://www.fastly.com/blog/supercharging-server-timing-http-trailers">include Server-Timing metadata</a> to give the client performance metrics about server processing during a request, appended after the response is fully completed. They're especially useful for long responses, e.g. to include final status metadata after a long-running HTTP stream.</p>
  89. <p>It's still rare that you'll need this, but it's pretty cool that it works when you do. There's a few requirements:</p>
  90. <ul>
  91. <li>For server response trailers, the client must advertise support for this, with a <code class="language-text">TE: trailers</code> header on the initial request.</li>
  92. <li>The initial headers should specify the trailer fields that will be used later, with <code class="language-text">Trailer: &lt;field names&gt;</code>.</li>
  93. <li>Some headers are never allowed as trailers, including <code class="language-text">Content-Length</code>, <code class="language-text">Cache-Control</code>, <code class="language-text">Authorization</code>, <code class="language-text">Host</code> and similar standard headers, which are often required initially to parse, authenticate or route requests.</li>
  94. </ul>
  95. <p>To send trailers in HTTP/1.1, you'll also need to use chunked encoding. HTTP/2 meanwhile uses separate frames for the body &amp; headers, so this isn't necessary.</p>
  96. <p>A full HTTP/1.1 response with trailers might look like this:</p>
  97. <div class="gatsby-highlight" data-language="text">
  98. <pre class="language-text"><code class="language-text">HTTP/1.1 200 OK
  99. Transfer-Encoding: chunked
  100. Trailer: My-Trailer-Field
  101. [...chunked response body...]
  102. My-Trailer-Field: some-extra-metadata</code></pre>
  103. </div>
  104. <h2 id="http-1xx-codes"><a href="#http-1xx-codes" aria-label="http 1xx codes permalink" class="anchor"><svg aria-hidden="true" focusable="false" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>HTTP 1XX codes</h2>
  105. <p>Did you know that an HTTP request can receive multiple response status codes? A server can send an unlimited number of 1XX codes before a final status (200, 404, or whatever it may be). These act as interim responses, and can all include their own independent headers.</p>
  106. <p>There's a few different 1XX codes available: 100, 101, 102, and 103. They're not widely used, but in some niche use cases they have some cool powers:</p>
  107. <h3 id="http-100"><a href="#http-100" aria-label="http 100 permalink" class="anchor"><svg aria-hidden="true" focusable="false" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>HTTP 100</h3>
  108. <p>HTTP 100 is a response from a server that the request is ok <em>so far</em>, and the client should keep going.</p>
  109. <p>Most of the time, this is a no-op. If you've started sending a request, you were probably going to keep going anyway, although it's always nice to have the server's support &amp; encouragement.</p>
  110. <p>This becomes useful though if you send a request including a <code class="language-text">Expect: 100-continue</code> header. That header tells the server you expect a 100 response, and you're not going to send the full request body until you receive it.</p>
  111. <p>Sending <code class="language-text">Expect: 100-continue</code> allows the server to decide if it wants to receive the whole body, which might take a lot of time/bandwidth. If the URL &amp; headers are enough for it to already send a response (e.g. to reject a file upload) this is a quick and efficient way to do that. If the server does want to receive the full body, it sends an interim 100 response, the client continues, and then the server handles the complete request as normal when it's done.</p>
  112. <h3 id="http-101"><a href="#http-101" aria-label="http 101 permalink" class="anchor"><svg aria-hidden="true" focusable="false" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>HTTP 101</h3>
  113. <p>HTTP 101 is used to switch protocols. It says "I've sent you a URL and headers, and now I want to do something <em>completely different</em> with this connection". Not just a different request, but different protocol entirely.</p>
  114. <p>The main use case is to set up a websocket. To do so, the client sends a request including these two headers:</p>
  115. <div class="gatsby-highlight" data-language="text">
  116. <pre class="language-text"><code class="language-text">Connection: upgrade
  117. Upgrade: websocket</code></pre>
  118. </div>
  119. <p>Then, if the server accepts, it sends a response like:</p>
  120. <div class="gatsby-highlight" data-language="text">
  121. <pre class="language-text"><code class="language-text">HTTP/1.1 101 Switching Protocols
  122. Upgrade: websocket
  123. Connection: Upgrade</code></pre>
  124. </div>
  125. <p>And then from there they stop speaking HTTP, and start exchanging raw websocket data on this connection instead.</p>
  126. <p>This status is also used to upgrade from HTTP/1.1 to HTTP/2 on the same connection, and you could use it to transform HTTP connections into all sorts of other TCP-based protocols too.</p>
  127. <p>That said, this status <em>isn't</em> supported in HTTP/2, which uses a different mechanism for protocol negotiation and a <a href="https://tools.ietf.org/html/rfc8441">totally different mechanism</a> to set up websockets (which basically isn't supported anywhere - websockets are always HTTP/1.1 right now).</p>
  128. <h3 id="http-102"><a href="#http-102" aria-label="http 102 permalink" class="anchor"><svg aria-hidden="true" focusable="false" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>HTTP 102</h3>
  129. <p>HTTP 102 tells the client that the server is still processing the request, and it'll respond <em>soon</em>. This differs from 100 in that the whole request has now been received, and all the action is now happening on the server side, with the client just waiting.</p>
  130. <p>This isn't much used as far as I can tell, and it seems to mainly exist as a keep-alive, to make sure the client doesn't think the server has simply died. It's in the original HTTP specifications, but it's been removed from many new editions.</p>
  131. <p>Still, it is supported &amp; used in real places in the wild, so it's quite possible to use it in your applications if it fits your needs.</p>
  132. <h3 id="http-103"><a href="#http-103" aria-label="http 103 permalink" class="anchor"><svg aria-hidden="true" focusable="false" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>HTTP 103</h3>
  133. <p>HTTP 103 meanwhile is a new &amp; trendy status intended to partially replace HTTP/2's server push functionality (which is now <a href="https://groups.google.com/a/chromium.org/g/blink-dev/c/K3rYLvmQUBY/m/vOWBKZGoAQAJ?pli=1">being removed from Chrome</a>).</p>
  134. <p>Using HTTP 103, a server can send some headers early, before fully handling the request and sending the rest of the response. This is primarily designed for delivering link headers, like <code class="language-text">Link: &lt;/style.css&gt;; rel=preload; as=style</code>, telling the client about other content that it may want to start loading early (like stylesheets, JS &amp; images, for web page requests) in parallel with the full response.</p>
  135. <p>When the server receives a request that takes a little processing, it often can't fully send the response headers until that processing completes. HTTP 103 allows the server to immediately nudge the client to download other content in parallel, without waiting for the requested resource data to be ready.</p>
  136. <h2 id="referer"><a href="#referer" aria-label="referer permalink" class="anchor"><svg aria-hidden="true" focusable="false" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Referer</h2>
  137. <p>The HTTP Referer header tells the server which page you came from previously, or which URL triggered a resource load. This has some privacy challenges, but it's stuck around, and it's sent in most requests made as you browse the internet.</p>
  138. <p>Notably, it's spelled wrong. This was added in the very early days of the web, and the unix spell checker at the time didn't recognize either referer or referrer (the correct spelling). By the time anybody noticed, it was in serious use in infrastructure and tools all over the place, so nothing could be changed and we have to live with every browser request having a misspelled header forever.</p>
  139. <p>Not especially important unless you're writing code to read this header yourself, but a great parable for the challenges of network compatibility.</p>
  140. <p>For maximum confusion and damage potential, new privacy/security headers related to this like <code class="language-text">Referrer-Policy</code> <em>do</em> use the correct spelling.</p>
  141. <h2 id="websockets-random-uuid"><a href="#websockets-random-uuid" aria-label="websockets random uuid permalink" class="anchor"><svg aria-hidden="true" focusable="false" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Websocket's 'random' UUID</h2>
  142. <figure>
  143. <img alt="XKCD's getRandom() comic" src="https://imgs.xkcd.com/comics/random_number.png">
  144. <figcaption>There's always <a href="https://xkcd.com/221/">a relevant XKCD</a></figcaption>
  145. </figure>
  146. <p>We talked about how HTTP 101 requests are used to set up websockets earlier. A full request to do so might look like this:</p>
  147. <div class="gatsby-highlight" data-language="text">
  148. <pre class="language-text"><code class="language-text">GET /chat HTTP/1.1
  149. Host: server.example.com
  150. Upgrade: websocket
  151. Connection: Upgrade
  152. Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
  153. Sec-WebSocket-Protocol: chat, superchat
  154. Sec-WebSocket-Version: 13
  155. Origin: http://example.com</code></pre>
  156. </div>
  157. <p>with a response that starts the websocket connection like this:</p>
  158. <div class="gatsby-highlight" data-language="text">
  159. <pre class="language-text"><code class="language-text">HTTP/1.1 101 Switching Protocols
  160. Upgrade: websocket
  161. Connection: Upgrade
  162. Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
  163. Sec-WebSocket-Protocol: chat</code></pre>
  164. </div>
  165. <p>The <code class="language-text">Sec-WebSocket-Accept</code> key here is interesting. This is designed to stop caching proxies accidentally reusing websocket responses that they don't understand, by requiring the response to include a header that matches the client header. Specifically:</p>
  166. <ul>
  167. <li>The server receives a base64 websocket key from the client</li>
  168. <li>The server appends the UUID <code class="language-text">258EAFA5-E914-47DA-95CA-C5AB0DC85B11</code> to the base64 string</li>
  169. <li>The server hashes the resulting string, encodes the hash in base64, and sends that back</li>
  170. </ul>
  171. <p>This is deeply weird. A single fixed random UUID that's used in the setup of every single websocket forever? Appending strings to base64 strings without decoding, and then base64-ing the result again too?</p>
  172. <p>The idea is that this logic isn't something that could be happen by accident, or something that could ever be used elsewhere, to guarantee that both parties are intentionally starting a websocket connection. This confirms that the server or proxy isn't used cached data without understanding it, and the client hasn't been tricked into opening a websocket connection that it doesn't understand.</p>
  173. <p>This totally works, it's widely used and quick &amp; easy to implement, which is all great, but it's wild that every websocket connection in the world relies on one magic UUID.</p>
  174. <h2 id="websockets--cors"><a href="#websockets--cors" aria-label="websockets cors permalink" class="anchor"><svg aria-hidden="true" focusable="false" version="1.1" viewbox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Websockets &amp; CORS</h2>
  175. <p>While we're talking about websockets: did you know that websockets effectively ignore all the CORS and single-origin policy restrictions that would normally apply to HTTP requests?</p>
  176. <p>CORS ensures that JavaScript running on a.com can't read data from b.com unless the latter explicitly opts into that in its response headers.</p>
  177. <p>This is important for lots of reasons, notably including network-local servers (a public web page shouldn't be able to talk to your router) and browser state (requests from one domain shouldn't be able to use cookies from another).</p>
  178. <p>Unfortunately though, websockets ignore CORS entirely, assuming instead that all websocket servers are modern &amp; sensible enough to correctly check the <code class="language-text">Origin</code> header for themselves. Many servers do not, and most developers I've mentioned this to weren't aware of it.</p>
  179. <p>This opens a whole world of fun vulnerabilities, nicely summarized in <a href="https://christian-schneider.net/CrossSiteWebSocketHijacking.html">this article</a>.</p>
  180. <p>In short: if you have a websocket API, check the <code class="language-text">Origin</code> header and/or use CSRF tokens before trusting any incoming connections.</p>
  181. <p>Once upon a time (1982) <a href="https://tools.ietf.org/html/rfc822#section-4.7.4">an RFC</a> suggested that using an <code class="language-text">X-</code> prefix for message headers was a good way to differentiate custom extensions from standardized names.</p>
  182. <p>At the time this was relevant to email metadata, but this was later popularized for usage in HTTP headers too.</p>
  183. <p>This is still a common pattern, and if you look at HTTP requests as you browse the web you'll see quite a few of these:</p>
  184. <ul>
  185. <li><code class="language-text">X-Shenanigans: none</code> - this appears on every response from Twilio's API. I have no idea why, but it is comforting to know there's <em>definitely</em> no shenanigans this time round.</li>
  186. <li><code class="language-text">X-Clacks-Overhead: GNU Terry Pratchett</code> - a <a href="https://xclacksoverhead.org/home/about">tribute</a> to Terry Pratchett, based on the message protocols within his own books.</li>
  187. <li><code class="language-text">X-Requested-With: XMLHttpRequest</code> - appended by various JS frameworks including jQuery, to clearly differentiate AJAX requests from resource requests (which can't include custom headers like this).</li>
  188. <li><code class="language-text">X-Recruiting: &lt;cheesy pitch to get you to apply for a job&gt;</code> - quite a few companies add these as a quick way to try and hire the kind of people who read HTTP headers for fun.</li>
  189. <li><code class="language-text">X-Powered-By: &lt;framework&gt;</code> - used to advertise the framework or technology that the server is using (usually a bad idea).</li>
  190. <li><code class="language-text">X-Http-Method-Override</code> - used to set a method that couldn't be set as the real method of the request for some reason, usually a client or networking limitation. Mostly a bad idea nowadays, but still popular &amp; supported by quite a few frameworks.</li>
  191. <li><code class="language-text">X-Forwarded-For: &lt;ip&gt;</code> - A defacto standard used by many proxies &amp; load balancers to include the original requester's IP in upstream requests.</li>
  192. </ul>
  193. <p>Each of these is weird and wonderful in its own way, but the pattern in general is mostly a bad idea, and a new (2011) <a href="https://tools.ietf.org/html/draft-saintandre-xdash-00">RFC</a> now formally discourages its use.</p>
  194. <p>The problem is that many non-standard headers eventually do become standard. When that happens, if you used an <code class="language-text">X-</code> prefix, now you either have to change the name (breaking all existing implementations) or standardize the <code class="language-text">X-</code> prefix (defeating the point of the prefix entirely, and adding annoying noise to the name forever).</p>
  195. <p>This is frustrating, and it's broken some real standards:</p>
  196. <ul>
  197. <li>Almost all web forms on the internet submit data with an unnecessarily confusing &amp; long-winded <code class="language-text">Content-Type: application/x-www-form-url-encoded</code> header.</li>
  198. <li>In the <a href="https://tools.ietf.org/html/rfc2068#section-3.5">1997 RFC for HTTP</a> where it defines the parsing rules for <code class="language-text">content-encoding</code>, it requires all implementations to treat <code class="language-text">x-gzip</code> and <code class="language-text">x-compress</code> as equivalent to <code class="language-text">gzip</code> and <code class="language-text">compress</code> respectively.</li>
  199. <li>The <a href="https://tools.ietf.org/html/rfc7034">standardized</a> header for configuring web page framing is now forever <code class="language-text">X-Frame-Options</code>, not just <code class="language-text">Frame-Options</code></li>
  200. <li>Similarly, we have <code class="language-text">X-Content-Type-Options</code>, <code class="language-text">X-DNS-Prefetch-Control</code>, <code class="language-text">X-XSS-Protection</code>, and various <code class="language-text">X-Forwarded-*</code> CDN/proxy headers, all of which are widely implemented and have become either formally or defacto standard headers in widespread use.</li>
  201. </ul>
  202. <p>If you want to use a custom header, just use a custom header name that's not standardized by anybody else. If you really want to avoid collisions, consider namespacing it, but you're usually pretty safe if there's no standard header that appears after a 30 second google.</p>
  203. <hr>
  204. <p>Standardization is <em>hard</em>, and HTTP is full of weird corners and odd details when you look closely. Let me know what you think on <a href="https://twitter.com/pimterry">Twitter</a>.</p>
  205. <p>Interested in inspecting &amp; rewriting HTTP for yourself? <strong><a href="https://httptoolkit.tech">Try out HTTP Toolkit</a></strong>.</p>
  206. </article>
  207. <hr>
  208. <footer>
  209. <p>
  210. <a href="/david/" title="Aller à l’accueil"><svg class="icon icon-home">
  211. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-home"></use>
  212. </svg> Accueil</a> •
  213. <a href="/david/log/" title="Accès au flux RSS"><svg class="icon icon-rss2">
  214. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-rss2"></use>
  215. </svg> Suivre</a> •
  216. <a href="http://larlet.com" title="Go to my English profile" data-instant><svg class="icon icon-user-tie">
  217. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-user-tie"></use>
  218. </svg> Pro</a> •
  219. <a href="mailto:david%40larlet.fr" title="Envoyer un courriel"><svg class="icon icon-mail">
  220. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-mail"></use>
  221. </svg> Email</a> •
  222. <abbr class="nowrap" title="Hébergeur : Alwaysdata, 62 rue Tiquetonne 75002 Paris, +33184162340"><svg class="icon icon-hammer2">
  223. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-hammer2"></use>
  224. </svg> Légal</abbr>
  225. </p>
  226. <template id="theme-selector">
  227. <form>
  228. <fieldset>
  229. <legend><svg class="icon icon-brightness-contrast">
  230. <use xlink:href="/static/david/icons2/symbol-defs-2021-12.svg#icon-brightness-contrast"></use>
  231. </svg> Thème</legend>
  232. <label>
  233. <input type="radio" value="auto" name="chosen-color-scheme" checked> Auto
  234. </label>
  235. <label>
  236. <input type="radio" value="dark" name="chosen-color-scheme"> Foncé
  237. </label>
  238. <label>
  239. <input type="radio" value="light" name="chosen-color-scheme"> Clair
  240. </label>
  241. </fieldset>
  242. </form>
  243. </template>
  244. </footer>
  245. <script src="/static/david/js/instantpage-5.1.0.min.js" type="module"></script>
  246. <script>
  247. function loadThemeForm(templateName) {
  248. const themeSelectorTemplate = document.querySelector(templateName)
  249. const form = themeSelectorTemplate.content.firstElementChild
  250. themeSelectorTemplate.replaceWith(form)
  251. form.addEventListener('change', (e) => {
  252. const chosenColorScheme = e.target.value
  253. localStorage.setItem('theme', chosenColorScheme)
  254. toggleTheme(chosenColorScheme)
  255. })
  256. const selectedTheme = localStorage.getItem('theme')
  257. if (selectedTheme && selectedTheme !== 'undefined') {
  258. form.querySelector(`[value="${selectedTheme}"]`).checked = true
  259. }
  260. }
  261. const prefersColorSchemeDark = '(prefers-color-scheme: dark)'
  262. window.addEventListener('load', () => {
  263. let hasDarkRules = false
  264. for (const styleSheet of Array.from(document.styleSheets)) {
  265. let mediaRules = []
  266. for (const cssRule of styleSheet.cssRules) {
  267. if (cssRule.type !== CSSRule.MEDIA_RULE) {
  268. continue
  269. }
  270. // WARNING: Safari does not have/supports `conditionText`.
  271. if (cssRule.conditionText) {
  272. if (cssRule.conditionText !== prefersColorSchemeDark) {
  273. continue
  274. }
  275. } else {
  276. if (cssRule.cssText.startsWith(prefersColorSchemeDark)) {
  277. continue
  278. }
  279. }
  280. mediaRules = mediaRules.concat(Array.from(cssRule.cssRules))
  281. }
  282. // WARNING: do not try to insert a Rule to a styleSheet you are
  283. // currently iterating on, otherwise the browser will be stuck
  284. // in a infinite loop…
  285. for (const mediaRule of mediaRules) {
  286. styleSheet.insertRule(mediaRule.cssText)
  287. hasDarkRules = true
  288. }
  289. }
  290. if (hasDarkRules) {
  291. loadThemeForm('#theme-selector')
  292. }
  293. })
  294. </script>
  295. </body>
  296. </html>