Shopify / shopify_app

A Rails Engine for building Shopify Apps
MIT License
1.74k stars 683 forks source link

Improve embedded requests detection with `Sec-Fetch-Dest` header #1873

Open kirillplatonov opened 4 days ago

kirillplatonov commented 4 days ago

What this PR does

Token exchange concern uses embedded param to determine whether request should be redirected to embedded or bounce page should be displayed: https://github.com/Shopify/shopify_app/blob/main/lib/shopify_app/controller_concerns/token_exchange.rb#L67

It works well when the app just loaded and Shopify provided all necessary params with id_token. But subsequent requests in app won't have embedded param in the URL. Meaning, that in case of invalid token error, the app will try to redirect user to embedded within iframe and it will lead to browser error: image

To solve this problem, I found an alternative way to detect embedded using Sec-Fetch-Dest browser header. It's a nice fallback to embedded param and should help us correctly detect embedded requests and redirect them properly.

References:

Checklist

Before submitting the PR, please consider if any of the following are needed:

kirillplatonov commented 4 days ago

@matteodepalo @zzooeeyy another important fix for token exchange auth to handle invalid token errors properly.