# HTTP

## [Statuses great link](https://www.loggly.com/wp-content/uploads/2015/06/http-decision-diagram.png)

## Cookies

![Set-cookie](http://www.plantuml.com/plantuml/svg/BOwnJiOm48FtF8KtOFXVe09KYOKHYOuieJ4qehqhTvVszObKLd_MPuyUsQ9jQxg3CswpwaybzA0TbMUQrqe9FDuwjHvYWD5t5MSI3SBgzn83xdMFgfBN1tm8NgVf3GjpeGruxzgNIzlHqpC-JK-dOJRceEFQTAYXw9Qh-7-nEGkguAj5CyBus-ZXAQMHKlhfFm00)

\
`Set-Cookie: <name>=<value>[; <Max-Age>=<age>] [; expires=<date>][; domain=<domain_name>] [; path=<some_path>][; secure][; HttpOnly]`

```java
if (max-age AND expiry) {
    use max-age
}
```

**Default**: cookie is associated with the _location of current document_ (**domain** as well as **path**).

**Best practices:**

* Cookie security: manage cookie scope &#x20;

### Secure (Security)

```java
if (HTTPS && cookie_has_Secure_attribute) {
    transmit_cookie()    
} 
if (HTTP && cookie_has_Secure_attribute) {
    do_not_transmit_cookie    
}
```

### HttpOnly (Security)

A cookie can be set and used over HTTP, but also **directly** on the web browser **via JavaScript**.

```java
if (XSS_breach && CSP_did_not_help) { 
    if (cookie_has_HttpOnly_flag) {
        JS_can_not_access_cookie
        break;
    }

    inject_Malicious_JS()
    access_Cookie_Value()
}
```

### First-party and third-party

```java
if (cookie.domain == browser_address_bar) => first_party cookie
if (cookie.domain != browser_address_bar) => third_party cookie
```

### SameSite (security)

Useful links [ONE](https://web.dev/samesite-cookie-recipes/) [TWO](https://digiday.com/media/what-is-chrome-samesite/) [THREE](https://web.dev/samesite-cookies-explained/)

[Spec](https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00). `SameSite` allows you to declare if your cookie should be restricted to a first-party or same-site context. Preventing Cross Site Request Forgery (CSRF).

```java
Set-Cookie: CookieName=CookieValue; SameSite=Strict;
Set-Cookie: CookieName=CookieValue; SameSite=Lax;
```

(If the user is on `www.web.dev` and requests an image from `static.web.dev` then that is a **same-site** request. `your-project.github.com` and `my-project.github.com` are **separate** sites. If the user is on `your-project.github.io` and requests an image from `my-project.github.io` that's a **cross-site** request.).

* **`SameSite = Strict`** => the cookies will be sent only in first-party context (only if `cookie.site == browser_url_bar.site` ).
  * For example, if you click on a link that points to a Facebook profile page, and if Facebook.com has set its cookie as SameSite=Strict, you cannot continue navigation on Facebook (view the Facebook page) unless you log in to Facebook again. The reason for this is because Facebook\`s cookie was not sent by this request.
* **`SameSite = Lax`** =>  the cookie will be sent along with the **GET** request initiated by third party website (this request must cause top level navigation).
  * Resources can be loaded by iframe, img tags, and script tags. These requests can also operate as GET requests, but none of them cause TOP LEVEL navigation. Basically, they don't change the URL in your address bar. Because these GET requests do not cause a TOP LEVEL navigation, thus cookies set to Lax won't be sent with them.
    * | **Request Type** | **Example Code**                   | **Cookies sent** |
      | ---------------- | ---------------------------------- | ---------------- |
      | Link             | \<a href="...">\</a>               | Normal, Lax      |
      | Perender         | \<link rel="prerender" href=".."/> | Normal, Lax      |
      | Form GET         | \<form method="GET" action="...">  | Normal, Lax      |
      | Form POST        | \<form method="POST" action="..."> | Normal           |
      | iframe           | \<iframe src="...">\</iframe>      | Normal           |
      | AJAX             | $.get("...")                       | Normal           |
      | Image            | \<img src="...">                   | Normal           |
* **`SameSite = None`** => you intentionally want the cookie sent in a third-party context. **When** the SameSite=**None** attribute is present, an additional **Secure** attribute **must be used** so cross-site cookies can only be accessed over HTTPS connections. This **won’t mitigate** all **risks** associated with **cross-site access** **but** it will provide **protection against network attacks**.
  * [https://www.chromestatus.com/feature/5633521622188032](https://www.chromestatus.com/feature/5633521622188032)

\--------------------------------

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-LzCqH7vGrEf5gCxptMz%2F-LzCrPma_5OIHW2CUAcL%2Fsame-site-cookie-diagram-1.png?alt=media\&token=3af4fbfb-8884-48f6-a3cf-08f3a6c5f3cb)

```
if (cookie.domain == external_service
    && cookie.domain != address_bar) {
    cross-site
}
```

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-LzBiH3FcJp3a6N2Poim%2F-LzBkI2tXE7hWf0MDSsS%2FScreen%20Shot%202020-01-22%20at%2011.16.26.png?alt=media\&token=7c2f5171-4bb5-4835-8bb0-c4bd57cd165b)

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-LzBiH3FcJp3a6N2Poim%2F-LzBkVVX7njffk9pJV5Z%2FScreen%20Shot%202020-01-22%20at%2011.17.34.png?alt=media\&token=705be2e9-9dd9-4d71-9afa-b530daace559)

:warning:**Chrome, Since 02.2020**: `SameSite=Lax` as default. Only cookies `SameSite=None; Secure` are available for external access. &#x20;

* Chrome implements these behavior as of version 80. [Firefox](https://groups.google.com/d/msg/mozilla.dev.platform/nx2uP0CzA9k/BNVPWDHsAQAJ) has them available to test as of Firefox 69 and will make them default behavior in the future. To test these behaviors in Firefox, open [`about:config`](http://kb.mozillazine.org/About:config) and set `network.cookie.sameSite.laxByDefault`. [Edge](https://groups.google.com/a/chromium.org/d/msg/blink-dev/AknSSyQTGYs/8lMmI5DwEAAJ) also plans to change its default behaviors.
* [SameSite=None: Known Incompatible Clients](https://www.chromium.org/updates/same-site/incompatible-clients)

#### How to test

You can test this behaviour as of Chrome 76 by enabling `chrome://flags/#cookies-without-same-site-must-be-secure` and from Firefox 69 in [`about:config`](http://kb.mozillazine.org/About:config) by setting `network.cookie.sameSite.noneRequiresSecure`.

## HTTPS

HTTP transforms data in plain text.\
`SSL 3.0 (@Deprecated, Secure Sockets Layer) <-based_on_SSL_3.0- TLS 1.3 (Transport Layer Security)`\
Goals of HTTPS:

* **Privacy**: encrypting data traffic
* **Integrity**: data received on either side was not altered unknowingly along the way
* **Authentication**: website you are talking to is who they say they are &#x20;

### Symmetric encription

The same key is used for encryption and decryption.\
This is what home WIFI uses. There is one key (password), which is set into router and laptop.

```
ENCRYPTION: encryption_algorithm(data, encryption_key) = cipher_text
```

![symmetric encryption](http://www.plantuml.com/plantuml/svg/XT0n3e9G3CRndLDqKmSkG8nXWDN54n3ub4ReUxP5ysulGMFY8Cxzfx-cfNcZFer3jg5J6aUuSakGLZaw1ydQWI5E-O4CUeTIiKnJT7JKDTxGLd6ROBxB93XemDaBgezBGm_sdkop-8hqgfGl_PnLzS_i3U_pDTXY4CENNDN_vVK3IqWtV-G9)

### Asymmetric encryption

Two different keys are used. One to encrypt, second to decrypt. => _Public key Cryptography_.\
![simple overview](http://www.plantuml.com/plantuml/svg/FSun4e8m48NXFgTudPKNe73mA25vm8v9baaMDRTN6CVo_wf_REQhxJcv2-wjvqoh4i0IgcmcMjmgaPXLRTAtExoVZkiDaVyQi2WRj10ltrrH8n9d6x3jKvA01tzQPLaFhlBqnjD7blXT-000)

### Self-signed certificate

Self-signed certificate is a certificate that is **not** signed by certificate authority (CA).

```java
protocol = https
var certificate
if (certificate.NOT_signed_by_CA()) {
    browser_warning = visit_website()
    if (bypass_warning) {
        possible(() -> man-in-the-middle_attack_with(certificate))
    }
} else {
    no_warning = visit_website()
}
```

### Digital signature

```java
signed_data = sign_data(private_key)
result = verify_signature(public_key)

if (result is correct) {
    certain_that_data_came_from_owner_of_associated_private_key
    certain_that_data_was_not_modified_along_the_way
}
```

## TLS

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FlTzuvGpFXFJNbDUQcnVd%2FScreenshot%202024-02-10%20at%2010.45.07.png?alt=media&#x26;token=13d406cb-91aa-466c-9147-cf15214ce7e7" alt=""><figcaption></figcaption></figure>
