Skip to content

fix: Handle HTTP redirects for migrated instances in sharing sync#4658

Open
shepilov wants to merge 1 commit intomasterfrom
fix/http_redirects_on_sharing_sync
Open

fix: Handle HTTP redirects for migrated instances in sharing sync#4658
shepilov wants to merge 1 commit intomasterfrom
fix/http_redirects_on_sharing_sync

Conversation

@shepilov
Copy link
Contributor

@shepilov shepilov commented Feb 4, 2026

Summary

  • Detect when sharing requests are redirected to a new domain and automatically update the member instance URL
  • Fix "No token in request" errors when syncing sharings with migrated instances that use 302 redirects

Problem

When an instance is migrated from one domain to another (e.g., alice.mycozy.cloud → alice.twake.app) using 302 redirects instead of the official move functionality, sharing sync fails with Unauthorized: {"error":"No token in request"}. When Go's http.Client follows a 302 redirect to a different host, it strips the Authorization header for security reasons (to prevent credential leakage to untrusted domains).

So:

  1. Request goes to old domain with Authorization: Bearer
  2. Server returns 302 Redirect to new domain
  3. Go follows redirect but strips the Authorization header
  4. Request arrives at new domain without token → 401 Unauthorized

Solution

Detect redirects by comparing the response's final URL (res.Request.URL) with the original request domain. When a cross-host redirect is detected:

  1. Update member.Instance to the new domain
  2. Refresh the access token (which persists the sharing document with the updated member URL)
  3. Retry the request to the new domain

@shepilov shepilov requested a review from a team as a code owner February 4, 2026 10:46
@shepilov shepilov force-pushed the fix/http_redirects_on_sharing_sync branch 3 times, most recently from 71e3f81 to d8e9035 Compare February 4, 2026 13:05
Copy link
Member

@taratatach taratatach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like tryUpdateMemberInstance(err, m, opts) is called only if the error status is 410 Gone while we update opts.Domain if the error status is 401 Unauthorized.
Should we change the condition on line (newly) 666?

finalHost := res.Request.URL.Host
if finalHost != "" && finalHost != opts.Domain {
// Redirect detected - update member instance to the new domain
newURL := &url.URL{Scheme: res.Request.URL.Scheme, Host: finalHost}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we simply reuse res.Request.URL here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go way to support a specific symbol in URLs, and to avoid string concat like scheme + "://" + host

@shepilov
Copy link
Contributor Author

shepilov commented Feb 4, 2026

Looks like tryUpdateMemberInstance(err, m, opts) is called only if the error status is 410 Gone while we update opts.Domain if the error status is 401 Unauthorized. Should we change the condition on line (newly) 666?

Sorry, didn't get your question. Why do we need to change this condition?

@shepilov shepilov force-pushed the fix/http_redirects_on_sharing_sync branch from d8e9035 to 93a0c66 Compare February 4, 2026 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants