Overview of SOP, CORS, and CSRF (2024)

If you read this article, you must have known these abbreviations and have some basic understanding of web security. This article will briefly guide you to existing web security standards, the most common web attacks, and prevention methods.

SOP

Copy link

SOP (Same Origin Policy) — is an important concept in the web application security model. The source combines the scheme (protocol), hostname, and port.

Overview of SOP, CORS, and CSRF (1)

Websites that use the same scheme, hostname, and port have the same origin and are otherwise the сross-origin. For instance, http://site.store.com and http://api.store.com are the same sites but have various sources.

The policy rule suggests that if both websites have the same source, then scripts on one website can only get data from another.

For example, on http://site.store.com, we want to show content from http://api.store.com. A GET request returns an error:

Overview of SOP, CORS, and CSRF (2)

If you look closer at the details of this request, you can notice one thing. The browser showing the request status as a CORS error:

Overview of SOP, CORS, and CSRF (3)

However, if you open the details, the request was successful and received the data according to the Content-Length. However, the browser will not show the response.

Overview of SOP, CORS, and CSRF (4)

CORS

Copy link

CORS (Cross-Origin Resource Sharing) is a mechanism that allows requesting restricted resources from outside the domain of a web page.

CORS requests can be divided into simple and preflighted. Simple requests are considered “safe” (“Request methods are considered “safe” if their defined semantics are essentially read-only” from RFC HTTP/1.1). Requests that won't change data on the server.

CORS list of safe methods and headers:

Overview of SOP, CORS, and CSRF (5)

GET and HEAD are methods for reading data. POST is allowed because such requests were already widely used before the creation of CORS (for example, POST requests that are made using HTML form) and could break many sites if they were forbidden.

Simple request

Copy link

According to browser policy, simple requests use methods and headers from the safe list. Browsers do not block simple requests.

Let me remind you, earlier, we made a GET request from the source http://site.store.com to the source http://api.store.com.

Overview of SOP, CORS, and CSRF (6)

However, the following conclusions can already be drawn:

  1. It was a GET request, and the Content-type was text/plain, so we made a simple request.
  2. By policy, the browser doesn’t block simple requests, which means the request was successful.
  3. Since the browser did not show us the response body—the request was blocked.

CORS doesn’t block simple requests but blocks reading the response body.

Preflight requests

Copy link

Let's try to make a preflight request, such as DELETE:

Overview of SOP, CORS, and CSRF (7)

We will get precisely the same CORS error as in the previous request. However, if you open the browser console, you can see two requests:

Overview of SOP, CORS, and CSRF (8)

The first request is the DELETE request we sent earlier. The second request has a preflight type specified. If we look at the details of this request, we will see that this is a request with the OPTIONS method. You can also notice that the Access-Control-Request-Headers header with the DELETE method was sent in the request.

Overview of SOP, CORS, and CSRF (9)

The browser sends a preflight request to understand what (non-simple/unsafe) requests the source allows. In response to a preflight request, the browser will wait for specific headers, based on which it will then decide whether to send or block the request.

The browser expects one or more of the following HTTP headers:

  • Access-Control-Allow-Origin (authorized sources)
  • Access-Control-Allow-Methods (allowed methods)
  • Access-Control-Allow-Headers (allowed headers)
  • Access-Control-Allow-Credentials (true/false, do need to pass cookies).

If we parse the case with a DELETE request, then send only the OPTIONS request, and since the browser did not receive any Access-Control-Allow headers in the response, it simply blocked the DELETE request. If we open the details of this request, we will see that it was indeed not sent:

Complex requests CORS first checks and then decides to block or allow them.

How to set up CORS for simple requests

Copy link

Let's set up our first simple request (also set up for any other simple request) to be able to read the response body. It will be enough to send the Access-Control-Allow-Origin header, which http://api.store.com should send to http://site.store.com, allowing this origin to receive/read its data.

Overview of SOP, CORS, and CSRF (11)

Let’s check:

Overview of SOP, CORS, and CSRF (12)

The same header in the response:

Overview of SOP, CORS, and CSRF (13)

You can only specify one source in this header. Or you can specify the value "*", but it will allow requests from any source.

A list of authorized sources is usually maintained when multiple sources are needed. If the given source (from the Origin header) is in the list, it is written to the Access-Control-Allow-Origin header.

How to set up CORS for preflight requests

Copy link

First, we allow a complex DELETE request (the configuration of any other preflight request is the same). Next, in response, you will need to send two headers, Access-Control-Allow-Origin and Access-Control-Allow-Methods.

Overview of SOP, CORS, and CSRF (14)

You can, in the Access-Control-Allow-Methods header, specify multiple methods separated by commas:

Overview of SOP, CORS, and CSRF (15)

Specifying the “safe” GET / HEAD / POST methods in the header is also unnecessary. CORS will not block these methods even if they are not in the Access-Control-Allow-Methods header.

Check:

Overview of SOP, CORS, and CSRF (16)

These headers must be sent in both the OPTIONS response and the DELETE request.

Overview of SOP, CORS, and CSRF (17)

If you send headers in the response only to the OPTIONS and not send it to the DELETE request, then the DELETE request will pass, but the browser will show a CORS error. Therefore, it will not be possible to read the response, as was the case with the first GET request.

Overview of SOP, CORS, and CSRF (18)

What Happens if You Send Unauthorized Requests?

Copy link

If you send a request with a method that the source did not allow, you will get an error that this method is not in the Access-Control-Allow-Methods header:

Overview of SOP, CORS, and CSRF (19)

If you send an "insecure" Content-type header, the browser will send the Access-Control-Request-Headers header in the request and wait for this header in the Access-Control-Allow-Headers in response.

Overview of SOP, CORS, and CSRF (20)

Overview of SOP, CORS, and CSRF (21)

Access-Control-Allow-Credentials

Copy link

By default, no cookies are sent in complex cross-origin requests. Therefore, tools such as XMLHttpRequest and Fetch need to be configured:

  • XMLHttpRequest: xhr.withCredentials = true;
  • Fetch: credentials: ‘include’

The source that accepts the request must confirm cookies can be used. To do this, the source sends an Access-Control-Allow-Credentials header whose value is "true." But then Access-Control-Allow-Origin must be specified. Otherwise, we get an error:

Overview of SOP, CORS, and CSRF (22)

If the value "*" were allowed, it would mean that any origin can make a request using cookies.

Check:

Overview of SOP, CORS, and CSRF (23)

If there is no Access-Control-Allow-Credentials header, then you can get an error:

Overview of SOP, CORS, and CSRF (24)

Finally

Copy link

In summary, you can customize the CORS policy for the origin with these four additional headers.

CSRF

Copy link

CSRF (Cross-Site Request Forgery) is a web attack that exploits loopholes in the SOP policy, and CORS does not block them. The attack consists of the attacker running malicious scripts in the victim's browser, and thus the victim performs unintended actions on his behalf.

CSRF attack will work if application authorization is based on cookies. The attack uses the knowledge that if the user has cookies for the domain, the browser will automatically add them to the request.

How can a CSRF exploit be triggered

Copy link

The attacker places a malicious script on his or her website. Then encourages victims to follow the link. If the victim is logged in to the attacked site, the browser will automatically add session cookies to the request. The attacked site will process the request, treating it as made by the victim user. Some exploits can use the GET method and be completely self-contained, so the attacker does not need to use an external site.

Classic example

Copy link

Imagine that a user has logged in to http://bank.com and created a session cookie.

The attacker posted the following exploit on his or her website:

Overview of SOP, CORS, and CSRF (25)

When the user goes to the attacker's site, the invisible HTML form will automatically work and send a POST request to change the mail.

You can see that the browser has automatically added session cookies:

Overview of SOP, CORS, and CSRF (26)

This is a simple request (POST with Content-type application/x-www-form-urlencoded because made it via an HTML form), so CORS didn't block it.

Overview of SOP, CORS, and CSRF (27)

Ways to protect against CSRF attacks

Copy link

Content-Type based protection

Copy link

Usually, POST requests that do some kind of sensitive action do not accept application/x-www-form-urlencoded, as in the example above. And then you might think that if the handlers in the application accept only application/json, this can protect against CSRF attacks because CORS will block such a request.

But sometimes, you can bypass this protection. You can change the Content-type to text/plain and simulate the desired format:

Overview of SOP, CORS, and CSRF (28)

Additional characters are added to the form data format to make it a valid JSON format.

The attacker changes the exploit:

Overview of SOP, CORS, and CSRF (29)

If the backend does not validate the Content-type, then this can work.

Overview of SOP, CORS, and CSRF (30)

For example, we wrote golang handlers using the standard encoding/json package. We also used the echo and gin frameworks for the experiment. In echo, the (context).Bind method returned status code 415 Unsupported Media Type, and we used the (context) in gin.BindJSON method, the method did not return an error and skipped the request.

Some developers use unusual Content-types to protect against CSRF attacks. Such protection is called content-type based protection. We don’t recommend relying only on this type of protection method because it can imitate data format, and there may not be a Content-type check on the backend.

SameSite attribute

Copy link

Used to configure cookie sending for cross-domain requests. Added to the Set-Cookie header: Set-Cookie: SessionId=sYMnfCUrAlmqVVZn9dqevxyFpKZt30NN; SameSite=Strict

It can have two meanings:

  • Strict: the browser will not include cookies in any cross-origin requests;
  • Lax: the browser will only include the cookie on GET cross-origin requests.

Passing cookies across different browsers

Copy link

In Google Chrome, one feature related to cookies can be noticed: cross-origin requests except GET no longer receive cookies two minutes after creation.

Overview of SOP, CORS, and CSRF (31)

In the picture, the request is made from http://evilsite.com (Origin header).

But for example, in Firefox, you can get a cookie after 1 hour or more:

Overview of SOP, CORS, and CSRF (32)

New security model for cookies

Copy link

Since February 2020, Google has implemented a new cookie security model. Very few developers follow the recommended practice of specifying the SameSite attribute when creating a cookie. This leaves many cookies and exposes your sites to possible CSRF attacks. And to increase security, the Chrome browser decided to set the value of the SameSite attribute by default equal to Lax, if the value is not specified at creation.

After the implementation of this model, many sites stopped working correctly. And Google then made a temporary feature, POST + Lax, when cookies are transmitted in POST requests within 2 minutes after they are created.

After introducing the new model, other browsers also announced plans to implement it in their browsers. For example, when creating cookies in Firefox, if SameSite is not specified, a warning is shown that cookies will soon be considered Lax by default.

If you want cookies to work in the old way, you need to specify SameSite equal to None when creating.

Can SameSite protect against CSRF attacks?

Copy link

SameSite in Lax mode provides partial protection because the POST method is used more in attacks. But you should check if the web application does not recognize HTTP methods. In this situation, even if the application uses the POST method by design, it may accept requests that are switched to using the GET method. It is also possible that the application uses GET requests to perform sensitive actions. For these reasons, relying solely on the SameSite attribute is not recommended.

CSRF tokens

Copy link

CSRF token is a value generated on the server side and passed to the client. The client then includes the token in all subsequent HTTP requests.

There are several patterns for using CSRF tokens.

  • Synchronizer Tokens are when a token is generated and stored on the server along with the user session. Clients receive the token. In subsequent requests, the client passes the token in the HTTP header. The server compares the tokens.
  • A Double Submit Cookie generates a token along with a CSRF cookie. The client receives the CSRF token and the Set-Cookie header. The client returns a CSRF token on subsequent requests, and the browser sends a CSRF cookie. The server compares the token from the cookie and the token from the header. By doing this, you do not need to store tokens on the server.

Using CSRF gorilla/mux middleware - https://github.com/gorilla/csrf

  1. Server creation:

Overview of SOP, CORS, and CSRF (33)

  • POST /login—system authorization.
    Further endpoints protected by authorization:
  • GET /user—get user profile.
  • POST /update_email—mail change.
    Using csrf.Protect, we create and configure CSRF middleware. The key is needed to encrypt the cookie data.

2. Passing a CSRF token to a client

When a user requests a profile (GET /user), we generate a CSRF token and pass it to the client. We get the token using the Token() method:

Overview of SOP, CORS, and CSRF (34)

If we look at the request, we will see the __gorilla_csrf cookie and the custom X-Csrf-Token header (these names can be changed when setting up the CSRF middleware).

Overview of SOP, CORS, and CSRF (35)

3. The client processes the response, receives the X-Csrf-Token header, and sends it on subsequent requests, for example, when changing mail:

Overview of SOP, CORS, and CSRF (36)

Overview of SOP, CORS, and CSRF (37)

Flowchart of CSRF middleware operation

Overview of SOP, CORS, and CSRF (38)

When you receive a profile (GET /user):

  • Trying to get a token from the __gorilla_csrf cookie.
  • Since we authenticated for the first time, we do not have a CSRF token and cookie. We get an error when trying to decrypt the token and get the base token.
  • A new base token is generated and encrypted. A __gorilla_csrf cookie is created.
  • A masked token is created (so that there is a unique token for each request). The masked token is stored in the context and can be obtained via the csrf.Token() method (we call getUser in the handler).
  • The HTTP method is checked, since we have GET, the token is not checked.
  • We get status 200, header X-Csrf-Token and Set-Cookie __gorilla_csrf.

When changing mail (POST /update_email):

  • Trying to get a token from the __gorilla_csrf cookie.
  • Trying to decrypt the token and get the base token. Since we already have tokens, the action is successful.
  • Creating the masked token (so there is a unique token for each request).
  • Checking the HTTP method. POST requests require the token to be checked.
  • Checks for HTTPS protocol. (In our example, HTTP is used). But if it were HTTPS, there would be a check for the Referrer header (from which source made the transition). If the Referrer and the source to which the request is made the match, then there will be no error. You can also specify the list of TrustedOrigins when setting up the middleware (if, for example, the request should be made from another origin).
  • Decrypts the token from the X-Csrf-Token header to get the base token.
  • A comparison is made between the cookie and header base tokens.
  • Since they match, we get status 200. Otherwise, there would be status 403.

To sum up

Copy link

We reviewed the SOP policy rules, the CORS mechanism, and how to set up a CORS policy. As you can see, the SOP policy solves many security issues, but it's quite restrictive. And CORS came about to ease policy and configure access between different origins.

We considered what CSRF attacks are and what methods of protection exist. At its core, CSRF attacks function through the trust model in browser standards. When an attacker clicks on a link, the browser initiates an HTTP request on behalf of the user, regardless of the link's source. This trust results in an authorization cookie being sent along with the request. CSRF attacks have been around for a long time, but so far, these attacks are common on the web and are easy to implement. Maybe soon, browser standards will change and make cross-site request forgery more difficult.

Altynai BaitulakovaSoftware Engineer
Overview of SOP, CORS, and CSRF (2024)

FAQs

Overview of SOP, CORS, and CSRF? ›

CSRF (Cross-Site Request Forgery) is a web attack that exploits loopholes in the SOP policy, and CORS does not block them. The attack consists of the attacker running malicious scripts in the victim's browser, and thus the victim performs unintended actions on his behalf.

What is SOP and CORS? ›

Two objects have the same origin only when the scheme, hostname, and port all match. SOP: Same Origin Policy : SOP restricts web resources from being accessed across different origins. CORS: Cross Origin Ressources Sharing : Allows web resources to be accessed across different origins with appropriate permissions.

What is the difference between CSRF and SOP? ›

CSRF attacks exploit the trust between a user and a website by tricking the user's browser into making unauthorized requests on their behalf. The SOP plays a important role in mitigating this type of attack by enforcing strict restrictions on how web pages can interact with each other.

What is cross site scripting attack vs CSRF? ›

XSS attacks exploit a user's trust in a website by injecting malicious scripts that run within the user's browser on trusted sites. In contrast, CSRF attacks exploit a website's trust in a user's browser, allowing attackers to perform unauthorized actions on behalf of the authenticated user.

What is cross site scripting vs CORS? ›

Cross-Origin Resource Sharing (CORS) is a security feature implemented by web browsers to prevent cross-site scripting (XSS) attacks. It works by blocking web pages from making requests to a different domain than the one that served the web page.

What is CORS and CSRF? ›

CSRF (Cross-Site Request Forgery) is a web attack that exploits loopholes in the SOP policy, and CORS does not block them. The attack consists of the attacker running malicious scripts in the victim's browser, and thus the victim performs unintended actions on his behalf.

What is CORS in simple terms? ›

Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.

What is CSRF explained simply? ›

Definition. Cross-Site Request Forgery (CSRF) is an attack that forces authenticated users to submit a request to a Web application against which they are currently authenticated. CSRF attacks exploit the trust a Web application has in an authenticated user.

What is CSRF basic example? ›

CSRF example

Those who click on the link while logged into their bank account will unintentionally initiate the $100 transfer. Note that if the bank's website is only using POST requests, it's impossible to frame malicious requests using a <a> href tag.

What are three main types of cross-site scripting? ›

Stored XSS, reflected XSS, and DOM-based XSS are the three most common types of cross-site scripting attacks. They differ in whether they affect the server or client side of the web application.

What is an example of a cross-site scripting attack? ›

A typical example of reflected cross-site scripting is a search form, where visitors sends their search query to the server, and only they see the result. Attackers typically send victims custom links that direct unsuspecting users toward a vulnerable page.

Is XSS worse than CSRF? ›

The consequences of XSS vulnerabilities are generally more serious than for CSRF vulnerabilities: CSRF often only applies to a subset of actions that a user is able to perform.

What attacks does CORS prevent? ›

CORS is important for web security because it prevents malicious pages from making requests on behalf of the user to APIs that are not configured to share resources with those pages. This helps protect sensitive data and prevent attacks such as CSRF (Cross-Site Request Forgery).

Is CORS configured on frontend or backend? ›

CORS or "Cross-Origin Resource Sharing" refers to the situations when a frontend running in a browser has JavaScript code that communicates with a backend, and the backend is in a different "origin" than the frontend.

Is CORS a middleware? ›

CORS is a node.js package for providing a Connect/Express middleware that can be used to enable CORS with various options.

What is CORS in SAP? ›

Overview. The SAP Commerce software uses the open source CORS filter, which is a standard Java EE filter. Cross-Origin Resource Sharing allows web browsers to overcome the same origin policy, which typically prohibits cross-site HTTP requests.

What is SOP in access? ›

Standard Operating Procedures or SOP for access control should be placed to ensure proper implementation. Department heads are responsible for applying for access to specific areas within the facility and gaining approval from IT before allocating these rights to their employees.

What does SOP mean in cyber security? ›

Standard Operating Procedures (SOPs) are formal, written guidelines or instructions for incident response that typically have both operational and technical components.

What does SOP stand for in SAP? ›

The sales and operations planning process delivers an updated, rolling operational plan and demand plan that typically extends 18 months to 36 months into the future.

Top Articles
6 things tax professionals need to know about cryptocurrency taxes - Thomson Reuters Institute
What is the greenest country on Earth? | Sterling TT
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Umn Biology
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 6267

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.