Logo

dev-resources.site

for different kinds of informations.

Third-Party Cookies Are Gone (Or Not). How Can I Still Embed Cross-Site Apps?

Published at
7/31/2024
Categories
cors
cookies
webapps
Author
samiekblad
Categories
3 categories in total
cors
open
cookies
open
webapps
open
Author
10 person written this
samiekblad
open
Third-Party Cookies Are Gone (Or Not). How Can I Still Embed Cross-Site Apps?

In 2024, there has been significant discussion about third-party cookies becoming obsolete or user configurable. While these changes are beneficial for user privacy, they complicate the lives of web application developers who want their applications or widgets to be shared across different websites. The main question remains: can I still make embedded web applications? How can we ensure that those still work?

Here’s a brief guide on how to configure your web application to ensure it remains unaffected by upcoming changes.

Server-Side Configuration

Assuming your web application is already designed for website embedding, you only need to configure a few things on the server side to keep your application up and running.

1. Enable TLS for HTTPS

While maintaining secure data transmission has been a best practice for a long time, enabling TLS (or SSL) for HTTPS is now mandatory. The method for enabling SSL depends on where your application is hosted. For instance, Fly.io offers a force_https = true option to configure apps to use HTTPS only.

2. Configure Session Cookies

Embedded applications often rely on session cookies to function correctly. Properly configuring these cookies with the necessary attributes is critical.

  1. SameSite=None: Means that the browser sends the cookie with both cross-site and same-site requests. The Secure attribute must also be set when setting this value.
  2. Secure: Ensures that the cookies are only sent over HTTPS, protecting them from being intercepted in transit.
  3. Partitioned: This attribute, part of the CHIPS (or Cookies Having Independent Partitioned State) proposal, ensures that cookies are partitioned based on the top-level site context, adding an extra layer of privacy by isolating cookies to the specific context. This is important since otherwise browsers will block the cookies in the future.

In the end, your session cookie headers should look something like this:

Set-Cookie: PHPSESSID=abcde12345; path=/; HttpOnly; SameSite=Strict; Secure; Partitioned
Enter fullscreen mode Exit fullscreen mode

or

Set-Cookie: JSESSIONID=abcde12345; Path=/; HttpOnly; SameSite=Strict; Secure; Partitioned
Enter fullscreen mode Exit fullscreen mode

3. Add Basic CORS Headers

The W3C defines the CORS protocol, which allows you to specify which sites can embed your content. Adding the appropriate CORS headers to your HTTP responses ensures that your content can be securely embedded across different origins. Some required during the preflight (OPTIONS) request and others during the actual (GET, POST,...) request.

Sent During Preflight Requests:

  • Access-Control-Allow-Methods: Specifies the HTTP methods (e.g., GET, POST, PUT, DELETE) allowed when accessing the resource. This header is sent in response to a preflight request to inform the client of the methods supported by the server.
  • Access-Control-Allow-Headers: Lists the HTTP headers that can be used during the actual request. This is sent in the preflight response to let the client know which headers can be included in the actual request.
  • Access-Control-Max-Age: Indicates how long the results of the preflight request can be cached by the client. This allows the client to cache the preflight response for a specified duration.

Sent During Actual Requests:

  • Access-Control-Allow-Origin: Specifies the origin(s) allowed to access the resource. You need to use an explicit list of allowed domains instead of the wildcard (*). This header is sent in the actual response to indicate which origins have access to the resource.
  • Access-Control-Allow-Credentials: true tells browsers that the server allows cross-origin HTTP requests to include credentials. This is sent either in preflight or in the actual response to indicate whether credentials (such as cookies or HTTP authentication) are allowed to be included in the real request.

How you set these headers in HTTP responses depends on the framework and runtime environment you use, but the principles are the same.

Configuring the Hosting Page

Some HTTP headers need to be configured on the hosting page. Consider setting up the Cross-Origin-Embedder-Policy and the Cross-Origin-Resource-Policy. By default, these policies allow embedding and resource loading, but configuring them helps enhance the security of your application by restricting unauthorized access and embedding.

Demo Setup: Seeing is Believing

As you might know, my default full stack consists of Spring Boot and Vaadin, and that is what I use here too. You'll find my small demo live at: samie.github.io/vaadin-cors-sample. When using Chrome, test it with the third-party cookies disabled.

Image description

Conclusion

Yes, embedding cross-site applications will remain a valid approach in the future. By carefully configuring your server to handle cookies and headers correctly, you can maintain secure and functional cross-origin embedding even as third-party cookies evolve. This ensures your applications or widgets can continue to work across different websites without compromising on security or functionality.

Like always, source code at GitHub. See you there!

cookies Article's
30 articles in total
Favicon
Cookies auto clearing after browser refresh issue , CORS related express cookies issue
Favicon
Understanding Cookies in Rails: A Developer's Guide
Favicon
Understanding JWT Tokens vs. Session Cookies: The Best for Web Authentication
Favicon
How to use Cookies in Postman?
Favicon
Comprehensive Guide to Cookies in JavaScript
Favicon
Understanding Cookies: What They Are, How They Work, and Why They Matter for Your Privacy
Favicon
What Cookies are Important for Privacy?
Favicon
Making the most annoying cookie banners we could think of 🍪🤬
Favicon
The Most Annoying Cookie Banner Ever Hackathon 🤬🍪
Favicon
How to Set Up User Cookies and Connect Google Analytics in Your React App
Favicon
Cookie Consent Headaches Across Subdomains
Favicon
Hacking Cookies and PWA on Ubuntu
Favicon
Third-Party Cookies Are Gone (Or Not). How Can I Still Embed Cross-Site Apps?
Favicon
🍪 Cookies - A humbling auth experience
Favicon
Javascript Ls/ss/cookies
Favicon
Contextual Targeting vs Cookies: Who will win in 2024?
Favicon
Javascript Ls/ss/cookies😎
Favicon
Introduction to JWT and Cookie storage
Favicon
Demystifying Session-Based Authentication: Your Angular Roadmap
Favicon
How to persist client-side preferences on the client in Svelte (w/o DB)
Favicon
Cookies in Depth using Javascript and NodeJs
Favicon
Cross-Domain Tracking Implementation
Favicon
Double chocolate chip cookie
Favicon
Mastering Authentication & Authorization: Exploring Identity Framework with .NET 8 and Migrations
Favicon
Elixir - Simple Req Cookie Jar
Favicon
Limitations of Cookies
Favicon
Cookies vs Session Storage vs Local Storage
Favicon
Best DX for cookies on localhost
Favicon
Understanding Cookies and Sessions in Node.js
Favicon
Cookies

Featured ones: