Cross-Origin Resource Sharing (CORS) Flashcards

1
Q

Why was there a need to securely interact with JavaScript, HTML and DOM API entities within the scope of the browser?

A

DOM and JavaScript makes it possible to reach all properties of an HTML document.
The richness of HTML introduced additional resources, such as other documents or media items, with their own cookies, DOM, JavaScript namespace, and other rich elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What did Netscape introduce in 1995?

A

The concept of same-origin policy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the same-origin policy?

A

The policy originally designed to protect access to the DOM, but has since been broadened to protect sensitive parts of the global JavaScript object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In web terms, what is the origin?

A

A set of common characteristics of a web resource URI, formed of three elements:
* scheme (protocol)
* hostname (domain/subdomain)
* port

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give an example of an origin.

A

http:// www.example.com: 8080/
scheme hostname port

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

All resources identified by ‘schema:hostname/anything:port’ have the same origin. [T/F]

A

T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What kinds of attacks could happen without Same-origin policy?

A

A bad site may trick a user to login with bank details and use a JavaScript call to access the DOM elements of the user’s bank loaded in iframe, such as balance.
e.g.
frames.bank_frame.document.getElementById(“balance”).value
This accesses the iframe element ‘bank_frame’ then the HTML element ‘balance’ in its DOM and get its value.
This could be extended to transfer the user’s money.
This is a cross-site request.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What could happen without Same-origin Policy?

A

Cross-site requests can be executed without a user’s consent or knowledge.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the Same Origin Policy specify?

A

When a browser connects to an origin (e.g. http://example1.org), it will not be able to connect to resources from a different origin (http://example2.org). JavaScript AJAX code to remote websites of a different origin are not allowed.
Marked-up images/links in the HTML content are allowed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Same Origin Policy is a defense mechanism that operates in and is enforced by the ________.

A

Browser.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Same Origin Policy is a Browser-based defense mechanism. [T/F]

A

T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Same Origin Policy permits ________ contained in a first web page to access data in a second web page if both
web pages have the same _______.

A

scripts
origin

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Same Origin Policy was introduced by _______ Navigator in 1995.

A

Netscape

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is Same Origin policy good for security most of the time?

A

Because most of the time a script running in the browser only needs to access resources on the same origin (e.g. API calls to the same backend that served the JavaScript code in the first place).
So the fact that JavaScript cannot normally access resources on other origins is good for security.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Give an example of a legitimate scenario where cross-origin access is desirable/necessary.

A

When an API is to be shared by multiple domains.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does CORS stand for?

A

Cross-origin Resource Sharing

17
Q

What are the purpose of CORS?

A

To define a way in which a browser and server can interact to determine whether it is safe to allow the cross-origin request

18
Q

CORS provide more _________ and ___________ than purely same-origin requests.

A

freedom
functionality

19
Q

CORS is more _______ than simply allowing all cross-origin requests.

A

secure

20
Q

What are the three aspects of CORS?

A

HTTP Extension
Fetch
Preflight

21
Q

In what way is HTTP extended in CORS?

A

Headers
Request/Response challenge

22
Q

What is the request header introduced by CORS?

A

Origin

23
Q

What is the Origin request header used for?

A

To indicate where a request (specifically a fetch) originates from.
e.g. Origin http://localhost:3001
It does not include any path information, but only the scheme, server name, and port.

23
Q

What is the Origin request header used for?

A

To indicate where a request (specifically a fetch) originates from.
e.g. Origin http://localhost:3001
It does not include any path information, but only the scheme, server name, and port.

24
Q

What is the response header introduced by CORS?

A

Access-Control-Allow-Origin

25
Q

What is the If the Access-Control-Allow-Origin header used for?

A

The server checks if the resource referenced in the script can be accessed by the Origin in the request (CORS).
If the recipient of the request accepts Origin, then
response succeeds, with the added Response Header:
Access-Control-Allow-Origin: «origin»
Note that wildcard * is allowed (any origin is permitted)

26
Q

What HTTP response message is returned with ‘curl -I -H “Origin: http://localhost:3001” http//localhost:3000/with-cors’ if the origin is accepted and the recipient/server is CORS configured?

A

HTTP/1.1 200 OK

27
Q

What HTTP response message is returned with ‘curl -I -H “Origin: http://localhost:3002” http//localhost:3000/with-cors’ if the origin is not accepted and the recipient/server is CORS configured?

A

HTTP/1.1 500 Internal Server Error

28
Q

What HTTP response message is returned with ‘curl -I http//localhost:3000/with-cors’ (Origin header is not provided) and the recipient/server is CORS configured?

A

HTTP/1.1 500 Internal Server Error

29
Q

What HTTP response message is returned with ‘curl -I -H “Origin: http://localhost:3001” http//localhost:3000/without-cors’ if the recipient/server is NOT CORS configured (accepts requests from any origin)?

A

HTTP/1.1 200 OK

30
Q

What HTTP response message is returned with ‘curl -I http//localhost:3000/without-cors’ with Origin header is not provided and the recipient/server is CORS configured (accepts requests from unspecified origin)?

A

HTTP/1.1 200 OK

31
Q

What is Fetch?

A

The request and response returned and the ability to check whether the response meets criteria set by the CORS Policy.

32
Q

How do you send the CORS preflight request using curl?

A

curl -I -X OPTIONS
-H “Access-Control-Request-Method: GET”
-H “Access-Control-Request-Headers: Content-Type”
-H “Origin: http://localhost:3001” http://localhost:3000/with-cors

33
Q

What is the response of a CORS preflight request with curl?

A

HTTP/1.1 204 No Content
Access-Control-Allow-Origin: http://localhost:3001 Access-Control-Allow-Methods:GET, HEAD, PUT, PATCH, POST, DELETE
Access-Control-Allow-Headers: Content-Type Access-Control-Expose-Headers: Content-Length, X-Foo, X-Bar Vary: Origin,
Access-Control-Request-Headers
Content-Length: 0
Date: Sun, 02 Aug 2020 21:19:00 GMT

34
Q

Why does the CORS protocol exist?

A

To allow sharing responses cross-origin and allow for more versatile fetches than possible with HTML’s form element.

35
Q

How do more complex requests differ from simple requests?

A

A preflight request with the HTTP OPTIONS method must be sent first to check headers and origin are accepted before the GET request.

36
Q

What is the CORS protocol layered on top of and why?

A

HTTP.
Allows responses to declare they can be shared with other origins (using Access-Control-Allow-Origin header).

37
Q

Where is the decision whether JavaScript is allowed to access foreign domains using XMLHttpRequest made?

A

In the browser.

38
Q

The CORS policy does not ensure a resource is protected. Why?

A
  • A Resource “cannot protect” itself from arbitrary clients
  • Even a “modified Browser” may not implement CORS

The resource may still be accessed by many clients, e.g. a JavaScript, Java, curl command line or any script/program not running on the browser and having a CORS policy to enforce.