HTTP headers
Tools
HTTP (Hypertext Transfer Protocol) is a stateless application layer protocol used for the transmission of resources like web application data and runs on top of TCP.
It was specifically designed for communication between web browsers and web servers.
HTTP utilizes the typical client-server architecture for communication, whereby the browser is the client, and the web server is the server.
Resources are uniquely identified with a URL/URI.
HTTP has 3 versions; HTTP 1.0, HTTP 1.1., and HTTP/2. And HTTP/3 in on its way.
- HTTP 1.1 is the most widely used version of HTTP and has several advantages over HTTP 1.0 such as the ability to re-use the same TCP connection, take advantage of the 3 ways handshake that was performed and request for multiple URI’s/Resources in that connection.
Structure of a HTTP request
Request Line: The request line is the first line of an HTTP request and contains the following three components:
Request Headers: they provide additional information about the request. Common headers include:
Request Body (Optional): Some HTTP methods (like POST or PUT) include a request body where data is sent to the server, typically in JSON or form data format.
HTTP verbs (or methods)
Structure of a HTTP response
Response headers: Similar to request headers, response headers provide additional information about the response. Common headers include:
Response Body (Optional): The response body contains the actual content of the response. For example, in the case of an HTML page, the response body will contain the HTML markup.
In response to the HTTP Request, the web server will respond with the requested resource, preceded by a bunch of new HTTP response headers. These new response headers from the web server will be used by your web browser to interpret the content contained in the Response content/body of the response.
An example:
Date header
The "Date" header in an HTTP response is used to indicate the date and time when the response was generated by the server. It helps clients and intermediaries to understand the freshness of the response and to synchronize the time between the server and the client. This is used in a blind SQLinjection, to see how long it takes for the server to respond.
Status code
The status code can be resume in the following chart:
Content-Type
The "Content-Type" header in an HTTP response is used to indicate the media type of the response content. It tells the client what type of data the server is sending so that the client can handle it appropriately.
List of all content-type headers
Cache-control
Cache-control: Cache-control is a header used to specify caching policies for browsers and other caching services. Specifically, the Cache-Control
HTTP header field holds directives (instructions) — in both requests and responses — that control caching in browsers and shared caches (e.g. Proxies, CDNs).
Why this configuration is considered safe? Cache-control: no-store, no-cache, max-age=0. - The max-age=N response directive indicates that the response remains fresh until N seconds after the response is generated. - The no-cache response directive indicates that the response can be stored in caches, but the response must be validated with the origin server before each reuse, even when the cache is disconnected from the origin server. - The no-store response directive indicates that any caches of any kind (private or shared) should not store this response.
Server header
The Server header displays the Web Server banner, for example, Apache, Nginx, IIS etc. Google uses a custom web server banner: gws (Google Web Server).
Set-Cookie
From geeksforgeeks: "The HTTP header Set-Cookie is a response header and used to send cookies from the server to the user agent. So the user agent can send them back to the server later so the server can detect the user."
Understanding SameSite attribute
Differences between SameSite and SameOrigin: we will use the URL http://www.example.org
to see the differences more clearly.
URL | Description | same-site | same-origin |
---|---|---|---|
http://www.example.org |
Identical URL | ✅ | ✅ |
http://www.example.org:80 |
Identical URL (implicit port) | ✅ | ✅ |
http://www.example.org:8080 |
Different port | ✅ | ❌ |
http://sub.example.org |
Different subdomain | ✅ | ❌ |
https://www.example.org |
Different scheme | ❌ | ❌ |
http://www.example.evil |
Different TLD | ❌ | ❌ |
When thinking about SameSite
cookies, we're only thinking about "same-site" or "cross-site".
CORS - Cross-Origin Resource Sharing
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.
For security reasons, browsers restrict cross-origin HTTP requests initiated from scripts.
X-XSS-Protection
The HTTP X-XSS-Protection
response header is a feature of Internet Explorer, Chrome and Safari that stops pages from loading when they detect reflected cross-site scripting XSS attacks.
Syntax
Strict-Transport-Security
The HTTP Strict-Transport-Security response header (often abbreviated as HSTS) informs browsers that the site should only be accessed using HTTPS, and that any future attempts to access it using HTTP should automatically be converted to HTTPS.
Directives
Example:
Additionally, Google maintains an HSTS preload service (used also by Firefox and Safari). By following the guidelines and successfully submitting your domain, you can ensure that browsers will connect to your domain only via secure connections. While the service is hosted by Google, all browsers are using this preload list. However, it is not part of the HSTS specification and should not be treated as official. Directive for the preload service is:
Sending the preload
directive from your site can have PERMANENT CONSEQUENCES and prevent users from accessing your site and any of its subdomains if you find you need to switch back to HTTP.
What OWASP says about HSTS response header.
Exploitation
Site owners can use HSTS to identify users without cookies. This can lead to a significant privacy leak. Take a look here for more details.
Cookies can be manipulated from sub-domains, so omitting the includeSubDomains
option permits a broad range of cookie-related attacks that HSTS would otherwise prevent by requiring a valid certificate for a subdomain. Ensuring the secure
flag is set on all cookies will also prevent, some, but not all, of the same attacks.
So... basically HSTS addresses the following threats:
- User bookmarks or manually types
http://example.com
and is subject to a man-in-the-middle attacker: HSTS automatically redirects HTTP requests to HTTPS for the target domain. - Web application that is intended to be purely HTTPS inadvertently contains HTTP links or serves content over HTTP: HSTS automatically redirects HTTP requests to HTTPS for the target domain
- A man-in-the-middle attacker attempts to intercept traffic from a victim user using an invalid certificate and hopes the user will accept the bad certificate: HSTS does not allow a user to override the invalid certificate message
HTTPS
HTTPS (Hypertext Transfer Protocol Secure) is a secure version of the HTTP protocol, which is used to transmit data between a user's web browser and a website or web application.
HTTPS provides an added layer of security by encrypting the data transmitted over the internet, making it more secure and protecting it from unauthorized access and interception.
HTTPS is also commonly referred to as HTTP Secure. HTTPS is the preferred way to use and configure HTTP and involves running HTTP over SSL/TLS.
SSL (Secure Sockets Layer) and TLS (Transport Layer Security) are cryptographic protocols used to provide secure communication over a computer network, most commonly the internet. They are essential for establishing a secure and encrypted connection between a user's web browser or application and a web server.
HTTPS does not protect against web application flaws! Various web application attacks will still work regardless of the use of SSL/TLS.(Attacks like XSS and SQLi will still work)
The added encryption layer only protects data exchanged between the client and the server and does stop attacks against the web application itself.