{"id":68,"date":"2022-05-20T04:10:23","date_gmt":"2022-05-20T04:10:23","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/?p=68"},"modified":"2022-05-20T04:10:23","modified_gmt":"2022-05-20T04:10:23","slug":"cors","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/2022\/05\/20\/cors\/","title":{"rendered":"CORS"},"content":{"rendered":"\n<p>Cross-Origin Resource Sharing (CORS) describes an online resource requested from another outside domain. This is most often done by web pages making HTTP\/AJAX requests to other web servers to retrieve external resources such as images, stylesheets, and scripts. <\/p>\n\n\n\n<p><strong>Cross-site request forgery (CSRF)<\/strong><\/p>\n\n\n\n<p>While the CORS mechanism is necessary for the modern internet, it can also pose a security risk. A malicious attacker induces the victims to unintentionally share the user&#8217;s private information cookies or submit forms to perform privileged actions. These types of attacks are known as Cross-site request forgery (CSRF).<\/p>\n\n\n\n<p><strong>Same-Origin Policy (SOP)<\/strong><\/p>\n\n\n\n<p>As a result, most modern web browsers impose a Same-Origin Policy (SOP) to prevent malicious websites (or insecure websites injected with malicious code) to make requests or accessing data from another origin. The same-origin policy should also be implemented in the backend as well in case the user relaxed the SOP. <\/p>\n\n\n\n<p><strong>Preflights<\/strong><\/p>\n\n\n\n<p>Whenever a website wants to request external resources from another origin, web browsers will make a preflight request to the server checking if the server will permit the Cross-Origin Request. The preflight request will specify the origin of the cross-origin access request and HTTP method\/headers that will be used. If the server approves of the Cross-Origin Request, then it will respond with the allowed origins, headers, credentials, methods, as well as how long the preflight request needs to be made again.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/docs.aws.amazon.com\/sdk-for-javascript\/v2\/developer-guide\/images\/cors-overview.png\" alt=\"\n                    Process flow for CORS requests\n                \" \/><figcaption>Image from: https:\/\/docs.aws.amazon.com\/sdk-for-javascript\/v2\/developer-guide\/cors.html<\/figcaption><\/figure>\n\n\n\n<p><strong>Bypass SOP using Proxy<\/strong><\/p>\n\n\n\n<p>I decided to look into this topic because I run into an issue with Same-Origin Policy. In the web application of Chicken Tinder, an HTTP call is made to the REST API in the backend every time the user creates\/joins a flock and votes for restaurants. Since the URL of my backend API is hosted at a different origin from my front-end application, the browser would block the API call unless I decided to relax the SOP. This is obviously inconvenient and insecure for the users so I am in the process of resolving this issue. One way to circumvent the SOP restriction in a React App is to configure a proxy in package.json file, which will change all initially cross-origin requests to the backend (react app) server. One can also create a proxy server to make API calls on behalf of the client application. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1024\/1*Gr3T_pkgMBbZw4v6Mjcm0g.png\" alt=\"A Simple Cors Proxy for Javascript Browser applications | by Sandeep |  NodejsMadeEasy | Medium\" \/><figcaption>Image from https:\/\/medium.com\/nodejsmadeeasy\/a-simple-cors-proxy-for-javascript-applications-9b36a8d39c51<\/figcaption><\/figure>\n\n\n\n<p><strong>Read more about CORS:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/CORS#the_http_request_headers\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/CORS#the_http_request_headers<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Cross-origin_resource_sharing\">https:\/\/en.wikipedia.org\/wiki\/Cross-origin_resource_sharing<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/portswigger.net\/web-security\/csrf\">https:\/\/portswigger.net\/web-security\/csrf<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.stackhawk.com\/blog\/react-cors-guide-what-it-is-and-how-to-enable-it\/\">https:\/\/www.stackhawk.com\/blog\/react-cors-guide-what-it-is-and-how-to-enable-it\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cross-Origin Resource Sharing (CORS) describes an online resource requested from another outside domain. This is most often done by web pages making HTTP\/AJAX requests to other web servers to retrieve external resources such as images, stylesheets, and scripts. Cross-site request forgery (CSRF) While the CORS mechanism is necessary for the modern internet, it can also [&hellip;]<\/p>\n","protected":false},"author":12222,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/wp-json\/wp\/v2\/users\/12222"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/wp-json\/wp\/v2\/posts\/68\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/projectchickentinder\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}