{"id":103250,"date":"2021-10-20T08:48:28","date_gmt":"2021-10-20T15:48:28","guid":{"rendered":"https:\/\/www.backblaze.com\/blog\/?p=103250"},"modified":"2026-01-30T07:31:47","modified_gmt":"2026-01-30T15:31:47","slug":"crash-cors-a-guide-for-using-cors","status":"publish","type":"post","link":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/","title":{"rendered":"Crash CORS: A Guide for Using CORS"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103252\" src=\"https:\/\/www.backblaze.com\/blog\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1.jpg\" alt=\"\" width=\"1440\" height=\"820\" srcset=\"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1.jpg 1440w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1-300x171.jpg 300w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1-1024x583.jpg 1024w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1-768x437.jpg 768w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1-560x319.jpg 560w\" sizes=\"auto, (max-width: 1440px) 100vw, 1440px\" \/><\/p>\n<p id=\"bzdropcap\">The dreaded CORS error is the bane of many a web developer\u2019s day-to-day life. Even for experts, it can be eternally frustrating. Today, we\u2019re digging into CORS, starting with the basics and working up to sharing specific code samples to help you enable CORS for your web development project. What is CORS? Why do you need it? And how can you enable it to save time and resources?<\/p>\n<p>We\u2019ll answer those questions so you can put the CORS bane behind you.<\/p>\n<h2><strong>What Is CORS?<\/strong><\/h2>\n<p>CORS stands for cross-origin resource sharing. To define CORS, we first need to explain its counterpoint\u2014the same-origin policy, or SOP. The SOP is a policy that all modern web browsers use for security purposes, and it dictates that a web address with a given origin can only request data from the same origin. CORS is a mechanism that allows you to bypass the SOP so you can request data from websites with <em>different<\/em> origins.<\/p>\n<p>Let\u2019s break that down piece by piece, then we\u2019ll get into why you\u2019d want to bypass the same-origin policy in the first place.<\/p>\n<h3><strong>What Is an Origin?<\/strong><\/h3>\n<p>All websites have an origin, and it is defined by the protocol, domain, and port of its URL.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-103255\" src=\"https:\/\/www.backblaze.com\/blog\/wp-content\/uploads\/2021\/10\/Site-Name-Transparent-1024x292.png\" alt=\"\" width=\"1024\" height=\"292\" srcset=\"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/Site-Name-Transparent-1024x292.png 1024w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/Site-Name-Transparent-300x85.png 300w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/Site-Name-Transparent-768x219.png 768w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/Site-Name-Transparent-560x159.png 560w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/Site-Name-Transparent.png 1440w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>You\u2019re probably familiar with the working parts of a URL, but they each have a different function. The protocol, also known as the scheme, identifies the method for exchanging data. Typical protocols are http, https, or mailto. The domain, also known as the hostname, is a specific webpage\u2019s unique identifier. You may not be as familiar with the port as it\u2019s not normally visible in a typical web address. Just like a port on the water, it\u2019s the connection point where information comes in and out of a server. Different port numbers specify the types of information the port handles.<\/p>\n<p>When you understand what an origin is, the \u201ccross-origin\u201d part of CORS makes a bit more sense. It simply means web addresses with different origins. In web addresses with the same origin, the protocol, domain, and port all match.<\/p>\n<h3><strong>What Is the Same-origin Policy?<\/strong><\/h3>\n<p>The same-origin policy was developed and implemented as a security measure against a specific website vulnerability that was discovered and exploited in the 2000s. Before the same-origin policy was in place, bad actors could use cookies stored in people\u2019s browsers to make requests to other websites illicitly. This is known as cross-site request forgery, or CSRF, pronounced \u201csea surf.\u201d It\u2019s also known as \u201csession riding.\u201d Tubular.<\/p>\n<p>Let\u2019s say you log in to Netflix on your laptop to add Ridley Scott\u2019s 1982 classic, \u201cBlade Runner\u201d to your queue, as one does. You click \u201cRemember Me\u201d so you don\u2019t have to log in every time, and your browser keeps your credentials stored in a cookie so that the Netflix site knows you are logged in no matter where you navigate within their site.<\/p>\n<p>Afterwards, you\u2019re bored, so you fall down an internet rabbit hole wondering why \u201cBlade Runner\u201d is called \u201cBlade Runner\u201d when there are few blades and little running. You end up on a site about samurai swords that happens to be malicious\u2014it has a script in its code that uses your authentication credentials stored in that cookie to make a request to Netflix that can change your address and add a bunch of DVDs to your queue (also, it\u2019s 2006, and <a href=\"https:\/\/www.scmagazine.com\/news\/strategy\/netflix-fixes-cross-site-request-forgery-hole\" target=\"_blank\" rel=\"noopener\">this actually happened<\/a>). You\u2019ve become a victim of cross-site request forgery.<\/p>\n<p>To thwart this threat, browsers enabled the same-origin policy to prohibit requests from one origin to another.<\/p>\n<h2><strong>Why Do You Need CORS?<\/strong><\/h2>\n<p>While the same-origin policy helped stop bad actors from nefariously accessing websites, it posed a problem\u2014sometimes you need or want assets and data from different origins. This is where the \u201cresource sharing\u201d part of cross-origin resource sharing comes in.<\/p>\n<p>CORS allows you to set rules governing which origins are allowed to bypass the same-origin policy so you can share resources from those origins.<\/p>\n<p>For example, you might host your website\u2019s front end at www.catblaze.com, but you host your back-end API at api.catblaze.com. Or, you might need to display a bunch of cat videos stored in Backblaze B2 Cloud Storage on your website, <a href=\"http:\/\/www.catblaze.com\" target=\"_blank\" rel=\"noopener\">www.catblaze.com<\/a> (more on that below).<\/p>\n<h2><strong>Do I Need CORS?<\/strong><\/h2>\n<p>Let\u2019s say you have a website, and you dabble in some coding. You\u2019re probably thinking, \u201cI can already use images and stuff from other websites. Do I need CORS?\u201d And you\u2019re right to ask. Most browsers allow simple http requests like <code>get<\/code>, <code>head<\/code>, and <code>post<\/code> without requiring CORS rules to be set in advance. Embedding images from other sites, for example, typically requires a <code>get<\/code> request to grab that data from a different origin. If that\u2019s all you need, you\u2019re good to go. You can use simple requests to embed images and other data from other websites without worrying about CORS.<\/p>\n<p>But some assets, like fonts or iframes, might not work\u2014then, you can use CORS\u2014but if you\u2019re a casual user, you can probably stop here.<\/p>\n<div class=\"abstract\" style=\"line-height: 1.8; margin: 24px 12px; padding: 24px 12px 10px 12px;\">\n<p><strong>Coding Explainer: What Is an http Request?<\/strong><\/p>\n<p>An http request is the way you, the client, use code to talk to a server. A complete http request includes a request line, request headers, and a message body if necessary. The request line specifies the method of the request. There are generally eight types:<\/p>\n<ul>\n<li><code>get<\/code>: \u201cI want something from the server.\u201d<\/li>\n<li><code>head<\/code>: \u201cI want something from the server, but only give me the headers, not the content.\u201d<\/li>\n<li><code>post<\/code>: \u201cI want to send something to the server.\u201d<\/li>\n<li><code>put<\/code>: \u201cI want to send something to the server that replaces something else.\u201d<\/li>\n<li><code>delete<\/code>: &#8230;self-explanatory.<\/li>\n<li><code>patch<\/code>: \u201cI want to change something on the server.\u201d<\/li>\n<li><code>options<\/code>: \u201cIs this request going to go through?\u201d (This one is important for CORS!)<\/li>\n<li><code>trace:<\/code> \u201cShow me what you just did.\u201d Useful for debugging.<\/li>\n<\/ul>\n<p>After the method, the request line also specifies the path of the URL the method applies to as well as the http version.<\/p>\n<p>The request headers communicate some specifics around how you want to receive the information. There are usually a whole bunch of these. (Wikipedia has <a href=\"https:\/\/en.wikipedia.org\/wiki\/List_of_HTTP_header_fields\" target=\"_blank\" rel=\"noopener\">a good list<\/a>.) They\u2019re typically formatted thusly: <code>name:value<\/code>. For example:<\/p>\n<p><code>accept:text\/plain<\/code> means you want the response to be in text format.<\/p>\n<p>Finally, the message body contains anything you might want to send. For example, if you use the method <code>post<\/code>, the message body would contain what you want to post.<\/p>\n<\/div>\n<h3><strong>Do I Need CORS With Cloud Storage?<\/strong><\/h3>\n<p>People use cloud storage for <a href=\"https:\/\/www.backblaze.com\/case-studies\" target=\"_blank\" rel=\"noopener\">all manner of data storage purposes<\/a>, and most do not need to use CORS to access resources stored in a cloud instance. For example, you can make API calls to Backblaze B2 from any computer to use the resources you have stored in your storage buckets. If you\u2019re running a mobile application and transferring data back and forth to Backblaze B2, for instance, you don\u2019t need CORS. The mobile application doesn\u2019t rely on a web browser.<\/p>\n<p>You only need CORS if you\u2019re specifically running code inside of a web browser and you need to make API calls from the browser to Backblaze B2. For example, if you\u2019re using an in-browser video player and want to play videos stored in Backblaze B2.<\/p>\n<p>Fortunately, if you <em>do<\/em> need CORS, Backblaze B2 allows you to configure CORS to your exact specifications while other cloud providers may have completely open CORS policies. Why is that important? An open CORS policy makes you vulnerable to CSRF attacks. To continue with the video example, let\u2019s say you\u2019re storing a bunch of videos that you want to make available on your website. If they\u2019re stored with a cloud provider that has an open CORS policy, you have two choices\u2014open or closed. You pick open so that your website visitors can call up those videos on demand, but that leaves you vulnerable to a CSRF that could allow a bad actor to download your videos. With Backblaze, you can specify the exact CORS rules you need.<\/p>\n<p>If you are using Backblaze B2 to store data that will be displayed in a browser, or you\u2019re just curious, read on to learn more about using CORS. CORS has saved developers lots of time and money by reducing maintenance effort and code complexity.<\/p>\n<h2><strong>How Does CORS Work?<\/strong><\/h2>\n<p>Unlike simple <code>get<\/code>, <code>head<\/code>, and <code>post<\/code> requests, some types of requests can alter the origin\u2019s data. These include requests like <code>delete<\/code>, <code>put<\/code>, and <code>patch<\/code>. Any type of request that could alter the origin\u2019s data will trigger CORS, as will simple requests that have non-standard http headers or requests in certain programming languages like AJAX. When CORS is triggered, the browser sends what\u2019s called a preflight request to see if the CORS rules allow the request.<\/p>\n<h3><strong>What Is a Preflight Request?<\/strong><\/h3>\n<p>A preflight request, also known as an <code>options<\/code> request, asks the server if it&#8217;s okay to make the CORS request. If the preflight request comes back successfully, then the browser will complete the actual request. Few other systems in computing do this by default, so it\u2019s important to understand when using CORS.<\/p>\n<p>A preflight request has the following headers:<\/p>\n<ul>\n<li><code>origin<\/code>: Identifies the origin from which the CORS request originates.<\/li>\n<li><code>access-control-request-method<\/code>: Identifies the method of the CORS request.<\/li>\n<li><code>access-control-request-headers<\/code>: Lists the headers that will be included in the CORS request.<\/li>\n<\/ul>\n<p>The web server then responds with the following headers:<\/p>\n<ul>\n<li><code>access-control-allow-origin<\/code>: Confirms the origin is allowed.<\/li>\n<li><code>access-control-allow-method<\/code>: Confirms the methods are allowed.<\/li>\n<li><code>access-control-allow-headers<\/code>: Confirms the headers are allowed.<\/li>\n<\/ul>\n<p>The values that follow these headers must match the values specified in the preflight request. If so, the browser will permit the actual CORS request to come through.<\/p>\n<h3><strong>Setting CORS Up: An Example<\/strong><\/h3>\n<p>To provide an example for setting CORS up, we\u2019ll use Backblaze B2. By default, the Backblaze B2 servers will say &#8220;no&#8221; to preflight requests. Adding CORS rules to your bucket tells Backblaze B2 which preflight requests to approve. You can enable CORS in the Backblaze B2 UI if you only need to allow one, specific origin or if you want to be able to share the bucket with all origins.<\/p>\n<figure id=\"attachment_103259\" aria-describedby=\"caption-attachment-103259\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-103259\" src=\"https:\/\/www.backblaze.com\/blog\/wp-content\/uploads\/2021\/10\/image2-1-1024x583.png\" alt=\"\" width=\"1024\" height=\"583\" srcset=\"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image2-1-1024x583.png 1024w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image2-1-300x171.png 300w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image2-1-768x437.png 768w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image2-1-560x319.png 560w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image2-1.png 1371w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-103259\" class=\"wp-caption-text\">Click the CORS rules link to configure CORS.<\/figcaption><\/figure>\n<figure id=\"attachment_103260\" aria-describedby=\"caption-attachment-103260\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-103260\" src=\"https:\/\/www.backblaze.com\/blog\/wp-content\/uploads\/2021\/10\/image4-1024x926.png\" alt=\"\" width=\"1024\" height=\"926\" srcset=\"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image4-1024x926.png 1024w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image4-300x271.png 300w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image4-768x695.png 768w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image4-560x506.png 560w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image4.png 1161w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-103260\" class=\"wp-caption-text\">In the CORS rules pop-up, you can choose how you want to configure CORS rules.<\/figcaption><\/figure>\n<p>If you need more specificity than that, you can select the option for custom rules and use the Backblaze B2 <a href=\"https:\/\/www.backblaze.com\/b2\/docs\/quick_command_line.html\" target=\"_blank\" rel=\"noopener\">command line tool<\/a>.<\/p>\n<p>When a CORS preflight or cross-origin download is requested, Backblaze B2 evaluates the CORS rules on the file&#8217;s bucket. Rules may be set at the time you create the bucket with <code>b2_create_bucket<\/code> or updated on an existing bucket using <code>b2_update_bucket<\/code>.<\/p>\n<p>CORS rules only affect Backblaze B2 operations in their &#8220;<code>allowedOperations<\/code>&#8221; list. Every rule must specify at least one in their <code>allowedOperations<\/code>.<\/p>\n<h4><strong>CORS Rule Structure<\/strong><\/h4>\n<p>Each CORS rule may have the following parameters:<\/p>\n<p><strong>Required:<\/strong><\/p>\n<ul>\n<li><code>corsRuleName<\/code>: A name that humans can recognize to identify the rule.<\/li>\n<li><code>allowedOrigins<\/code>: A list of the origins you want to allow.<\/li>\n<li><code>allowedOperations<\/code>: A list that specifies the operations you want to allow, including:\n<ul>\n<li><strong>B2 Native API Operations:<\/strong><\/li>\n<li><code>B2_download_file_by_name<\/code><\/li>\n<li><code>B2_download_file_by_id<\/code><\/li>\n<li><code>B2_upload_file<\/code><\/li>\n<li><code>B2_upload_part<\/code><\/li>\n<li><strong>S3 Compatible Operations:<\/strong><\/li>\n<li><code>S3_delete<\/code><\/li>\n<li><code>S3_get<\/code><\/li>\n<li><code>S3_head<\/code><\/li>\n<li><code>S3_post<\/code><\/li>\n<li><code>S3_put<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>Optional:<\/strong><\/p>\n<ul>\n<li><code>allowedHeaders<\/code>: A list of headers that are allowed in a preflight request\u2019s Access-Control-Request-Headers value.<\/li>\n<li><code>exposeHeaders<\/code>: A list of headers that may be exposed to an application inside the client.<\/li>\n<li><code>maxAgeSeconds<\/code>: The maximum number of seconds that a browser can cache the response to a preflight request.<\/li>\n<\/ul>\n<p>The following sample configuration allows downloads, including range requests, from any https origin and will tell browsers that it&#8217;s okay to expose the &#8216;x-bz-content-sha1&#8217; header to the web page.<\/p>\n<p><code>[<br \/>\n{<br \/>\n\"corsRuleName\": \"downloadFromAnyOrigin\",<br \/>\n\"allowedOrigins\": [<br \/>\n\"https\"<br \/>\n],<br \/>\n\"allowedHeaders\": [\"range\"],<br \/>\n\"allowedOperations\": [<br \/>\n\"b2_download_file_by_id\",<br \/>\n\"b2_download_file_by_name\"<br \/>\n],<br \/>\n\"exposeHeaders\": [\"x-bz-content-sha1\"],<br \/>\n\"maxAgeSeconds\": 3600<br \/>\n}<br \/>\n]<\/code><\/p>\n<p>You may add up to 100 CORS rules to each of your buckets. Backblaze B2 uses the first rule that matches the request. A CORS preflight request matches a rule if the origin header matches one of the rule&#8217;s <code>allowedOrigins<\/code>, if the operation is in the rule&#8217;s <code>allowedOperations<\/code>, and if every value in the <code>Access-Control-Request-Headers<\/code> is in the rule&#8217;s <code>allowedHeaders<\/code>.<\/p>\n<h4><strong>Using CORS: Examples in Action<\/strong><\/h4>\n<p>Using your browser&#8217;s console, you can copy and paste the following examples to see CORS requests succeed or fail. As a handy guide for you, the text files we\u2019ll be requesting include the bucket configuration of the Backblaze B2 buckets we\u2019re calling.<\/p>\n<p>In the first example, we\u2019ll make a request to <code>get<\/code> the text file <code>bucket_info.txt<\/code> from a bucket named \u201ccors-allow-none\u201d that does not allow CORS requests:<\/p>\n<p><code>fetch(<br \/>\n'https:\/\/f000.backblazeb2.com\/file\/cors-allow-none\/bucket_info.txt',<br \/>\n{<br \/>\nmethod: 'GET'<br \/>\n}<br \/>\n).then(resp =&gt; resp.text()).then(console.log)<\/code><\/p>\n<p>As you can see, this request returns a CORS error:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-103261\" src=\"https:\/\/www.backblaze.com\/blog\/wp-content\/uploads\/2021\/10\/image8-1024x844.png\" alt=\"\" width=\"1024\" height=\"844\" srcset=\"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image8-1024x844.png 1024w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image8-300x247.png 300w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image8-768x633.png 768w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image8-560x462.png 560w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image8.png 1031w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Next, we\u2019ll try the same request on a bucket named \u201ccors-allow-all\u201d that allows CORS with any origin, but only specific headers.<\/p>\n<p><code>fetch(<br \/>\n'https:\/\/f000.backblazeb2.com\/file\/cors-allow-all\/bucket_info.txt',<br \/>\n{<br \/>\nmethod: 'GET'<br \/>\n}<br \/>\n).then(resp =&gt; resp.text()).then(console.log)<\/code><\/p>\n<p>When you run the code, you will see some text output to the console indicating that, indeed, the bucket allows CORS with all origins, but specific headers:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-103262\" src=\"https:\/\/www.backblaze.com\/blog\/wp-content\/uploads\/2021\/10\/image3-1024x844.png\" alt=\"\" width=\"1024\" height=\"844\" srcset=\"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image3-1024x844.png 1024w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image3-300x247.png 300w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image3-768x633.png 768w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image3-560x462.png 560w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image3.png 1031w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>We didn\u2019t include any headers in our request, so the request was successful and the text file we wanted\u2014<code>bucket_info.txt<\/code>\u2014appears below the text output in the console. As you can see in the text output, the bucket is configured with an asterisk \u201c<code>*<\/code>,\u201d also known as a \u201cwildcard,\u201d to allow all origins (more on that later).<\/p>\n<p>Next, we\u2019ll try the same thing on the bucket that allows CORS with all origins, but this time triggers a preflight check for a header that is not allowed:<\/p>\n<p><code>fetch(<br \/>\n'https:\/\/f000.backblazeb2.com\/file\/cors-allow-all\/bucket_info.txt',<br \/>\n{<br \/>\nmethod: 'GET',<br \/>\nheaders: { 'X-Fake-Header': 'breaking-cors-for-fun' }<br \/>\n}<br \/>\n).then(resp =&gt; resp.text()).then(console.log)<\/code><\/p>\n<p>Our bucket is configured to only allow the headers <code>authorization<\/code> and <code>range<\/code>, but we\u2019ve included the header <code>X-Fake-Header<\/code> with the value <code>breaking-cors-for-fun<\/code>\u2014definitely not allowed\u2014in the request.<\/p>\n<p>When we run this request, we can see another type of failure:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-103263\" src=\"https:\/\/www.backblaze.com\/blog\/wp-content\/uploads\/2021\/10\/image7-1024x844.png\" alt=\"\" width=\"1024\" height=\"844\" srcset=\"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image7-1024x844.png 1024w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image7-300x247.png 300w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image7-768x633.png 768w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image7-560x462.png 560w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image7.png 1031w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Below the request, but above the CORS errors, you\u2019ll see that the browser sent an <code>options<\/code> request. As we mentioned earlier, this is the preflight request that asks the server if it\u2019s okay to make the <code>get<\/code> request. In this case, the preflight request failed.<\/p>\n<p>However, this request will succeed if we change our bucket settings to allow all headers.<\/p>\n<p><code>fetch(<br \/>\n'https:\/\/f000.backblazeb2.com\/file\/cors-allow-all\/bucket_info.txt',<br \/>\n{<br \/>\nmethod: 'GET',<br \/>\nheaders: { 'X-Fake-Header': 'breaking-cors-for-fun' }<br \/>\n}<br \/>\n).then(resp =&gt; resp.text()).then(console.log)<\/code><\/p>\n<p>Below, you can see the text output \u201cThis bucket allows CORS with all origins and any header values.\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-103264\" src=\"https:\/\/www.backblaze.com\/blog\/wp-content\/uploads\/2021\/10\/image1-1024x844.png\" alt=\"\" width=\"1024\" height=\"844\" srcset=\"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image1-1024x844.png 1024w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image1-300x247.png 300w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image1-768x633.png 768w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image1-560x462.png 560w, https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/image1.png 1031w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>The request was successful, and the text file we requested appears in the console.<\/p>\n<p>At this point, it\u2019s important to note that when configuring your own buckets, you should use caution when using the wildcard \u201c<code>*<\/code>\u201d to allow any origin or header. It\u2019s probably best to avoid the wildcard if possible. It\u2019s okay to allow any origin to access your bucket, but, if so, you\u2019ll probably want to enumerate the headers that matter to avoid CSRF attacks.<\/p>\n<p>For more information on using CORS with Backblaze B2, including some tips on using CORS with the Backblaze S3 Compatible API, check out our documentation <a href=\"https:\/\/www.backblaze.com\/b2\/docs\/cors_rules.html\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<h2><strong>Stay on CORS<\/strong><\/h2>\n<p>Ah, another inevitable CORS pun. Did you see it coming? I hope so. In conclusion, here are a few things to remember about CORS and how you can use it to avoid CORS errors in the future:<\/p>\n<ul>\n<li>The same-origin policy was developed to make websites less vulnerable to threats, and it prevents requests between websites with different origins.<\/li>\n<li>CORS bypasses the same-origin policy so that you can share and use data from different origins.<\/li>\n<li>You only need to configure CORS rules for your Backblaze B2 data if you are making calls to Backblaze B2 from code within a web browser.<\/li>\n<li>By setting CORS rules, you can specify which origins are allowed to request data from your Backblaze B2 buckets.<\/li>\n<\/ul>\n<p>Are you using CORS? Do you have any other questions? Let us know in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn more about cross-origin resource sharing (CORS) and how to enable it using Backblaze B2 Cloud Storage.<\/p>\n","protected":false},"author":159,"featured_media":103252,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[7],"tags":[468],"class_list":["post-103250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-storage","tag-b2cloud","entry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Guide for Using CORS: What It Is, Why You Need It, and How it Works<\/title>\n<meta name=\"description\" content=\"CORS, which stands for cross-origin resource sharing, can be a difficult beast to master. What is CORS, and how can you use it most effectively?\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Guide for Using CORS: What It Is, Why You Need It, and How it Works\" \/>\n<meta property=\"og:description\" content=\"CORS, which stands for cross-origin resource sharing, can be a difficult beast to master. What is CORS, and how can you use it most effectively?\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/\" \/>\n<meta property=\"og:site_name\" content=\"Backblaze Blog | Cloud Storage &amp; Cloud Backup\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/backblaze\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-20T15:48:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-01-30T15:31:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1440\" \/>\n\t<meta property=\"og:image:height\" content=\"820\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Molly Clancy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@backblaze\" \/>\n<meta name=\"twitter:site\" content=\"@backblaze\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Molly Clancy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Guide for Using CORS: What It Is, Why You Need It, and How it Works","description":"CORS, which stands for cross-origin resource sharing, can be a difficult beast to master. What is CORS, and how can you use it most effectively?","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/","og_locale":"en_US","og_type":"article","og_title":"Guide for Using CORS: What It Is, Why You Need It, and How it Works","og_description":"CORS, which stands for cross-origin resource sharing, can be a difficult beast to master. What is CORS, and how can you use it most effectively?","og_url":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/","og_site_name":"Backblaze Blog | Cloud Storage &amp; Cloud Backup","article_publisher":"https:\/\/www.facebook.com\/backblaze","article_published_time":"2021-10-20T15:48:28+00:00","article_modified_time":"2026-01-30T15:31:47+00:00","og_image":[{"width":1440,"height":820,"url":"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1.jpg","type":"image\/jpeg"}],"author":"Molly Clancy","twitter_card":"summary_large_image","twitter_creator":"@backblaze","twitter_site":"@backblaze","twitter_misc":{"Written by":"Molly Clancy","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/#article","isPartOf":{"@id":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/"},"author":{"name":"Molly Clancy","@id":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/#\/schema\/person\/a92e54b3011e599a575611dbbb443b5c"},"headline":"Crash CORS: A Guide for Using CORS","datePublished":"2021-10-20T15:48:28+00:00","dateModified":"2026-01-30T15:31:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/"},"wordCount":2572,"commentCount":2,"publisher":{"@id":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/#primaryimage"},"thumbnailUrl":"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1.jpg","keywords":["B2Cloud"],"articleSection":["Cloud Storage"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/","url":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/","name":"Guide for Using CORS: What It Is, Why You Need It, and How it Works","isPartOf":{"@id":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/#primaryimage"},"image":{"@id":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/#primaryimage"},"thumbnailUrl":"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1.jpg","datePublished":"2021-10-20T15:48:28+00:00","dateModified":"2026-01-30T15:31:47+00:00","description":"CORS, which stands for cross-origin resource sharing, can be a difficult beast to master. What is CORS, and how can you use it most effectively?","breadcrumb":{"@id":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/#primaryimage","url":"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1.jpg","contentUrl":"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1.jpg","width":1440,"height":820},{"@type":"BreadcrumbList","@id":"https:\/\/www.backblaze.com\/blog\/crash-cors-a-guide-for-using-cors\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Crash CORS: A Guide for Using CORS"}]},{"@type":"WebSite","@id":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/#website","url":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/","name":"Backblaze Cloud Solutions Blog","description":"Cloud Storage &amp; Cloud Backup","publisher":{"@id":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/#organization","name":"Backblaze","url":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/www.backblaze.com\/blog\/wp-content\/uploads\/2017\/12\/backblaze_icon_transparent.png?fit=512%2C512&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.backblaze.com\/blog\/wp-content\/uploads\/2017\/12\/backblaze_icon_transparent.png?fit=512%2C512&ssl=1","width":512,"height":512,"caption":"Backblaze"},"image":{"@id":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/backblaze","https:\/\/x.com\/backblaze","https:\/\/www.youtube.com\/user\/Backblaze","https:\/\/en.wikipedia.org\/wiki\/Backblaze"]},{"@type":"Person","@id":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/#\/schema\/person\/a92e54b3011e599a575611dbbb443b5c","name":"Molly Clancy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/02\/ClancyMolly_Headshot_reduced-150x150.png","url":"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/02\/ClancyMolly_Headshot_reduced-150x150.png","contentUrl":"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/02\/ClancyMolly_Headshot_reduced-150x150.png","caption":"Molly Clancy"},"description":"Molly Clancy is a content writer who specializes in explaining tech concepts in an easy, approachable way. With more than 15 years of experience, she has a broad background in industries ranging from B2B tech to engineering to luxury travel. A deep curiosity drives her repeated success explaining what terms like OS kernel and preflight request mean so that anyone can understand them.","url":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/author\/molly\/"}]}},"jetpack_featured_media_url":"https:\/\/backblazeprod.wpenginepowered.com\/wp-content\/uploads\/2021\/10\/bb-bh-What-is-CORS-1.jpg","_links":{"self":[{"href":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/wp-json\/wp\/v2\/posts\/103250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/wp-json\/wp\/v2\/users\/159"}],"replies":[{"embeddable":true,"href":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/wp-json\/wp\/v2\/comments?post=103250"}],"version-history":[{"count":0,"href":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/wp-json\/wp\/v2\/posts\/103250\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/wp-json\/wp\/v2\/media\/103252"}],"wp:attachment":[{"href":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/wp-json\/wp\/v2\/media?parent=103250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/wp-json\/wp\/v2\/categories?post=103250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/backblazeprod.wpenginepowered.com\/blog\/wp-json\/wp\/v2\/tags?post=103250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}