Skip to content

Android: don't attach the auth token to presigned object-storage URLs - #413

Merged
Dvorinka merged 1 commit into
Dvorinka:masterfrom
dxts:fix/android-presigned-auth-header
Nov 2, 2026
Merged

Dvorinka merged 1 commit into
Dvorinka:masterfrom
dxts:fix/android-presigned-auth-header

Conversation

@dxts

@dxts dxts commented Nov 1, 2026

Copy link
Copy Markdown

Problem

The okhttp authInterceptor in ApiClient.kt attaches Authorization: Bearer <token> to every request made on the client, including the direct chunk PUT/GET to the presigned object-storage URLs returned for S3-style storage policies (S3, OSS, COS, OneDrive, …).

S3-compatible stores reject a presigned request that also carries an Authorization header — the presigned query parameters are already a complete auth mechanism. The result is 400 InvalidArgument ("only one auth mechanism allowed") on every part upload:

  • the server opens the multipart upload and hands the app presigned part URLs,
  • the app PUTs each part to object storage and gets a 400,
  • zero parts ever land, uploads appear stuck/slow, and nothing reaches the bucket.

Direct downloads/original previews hit the same wall for the same reason.

Web clients are unaffected, because a browser's cross-origin fetch never sends the app's Authorization header — which is exactly why this reproduces only on Android.

Reproduction

Against Hetzner Object Storage (Ceph RGW), an identical presigned UploadPart PUT:

  • without an Authorization header → 200, ETag returned;
  • with Authorization: Bearer <token> added → 400 InvalidArgument.

Fix

Scope the bearer token to the API host only; never attach it to off-host (presigned) URLs. This fixes direct uploads and downloads for every presigned-URL policy and is a no-op for same-origin API calls.

val apiHost = runCatching { java.net.URI(baseUrl).host }.getOrNull()
val authInterceptor = Interceptor { chain ->
    val token = runBlocking { session.accessTokenNow() }
    val sameHost = chain.request().url.host.equals(apiHost, ignoreCase = true)
    val request = if (token.isNotEmpty() && sameHost) {
        chain.request().newBuilder().header("Authorization", "Bearer $token").build()
    } else {
        chain.request()
    }
    chain.proceed(request)
}

The okhttp authInterceptor added Authorization: Bearer <token> to every
request, including the direct chunk PUT/GET to presigned S3 URLs for S3-style
storage policies. Hetzner's Ceph (and AWS S3) reject a presigned request that
also carries an Authorization header with 400 InvalidArgument, so every direct
upload failed: the server opened the multipart upload, the phone PUT each part
and got a 400, and zero parts ever landed. Browsers never hit this because a
cross-origin fetch does not send the app's Authorization header.

Scope the header to the API host only. Fixes direct uploads and downloads for
S3/OSS/COS/OneDrive presigned-URL policies.
@mira-tdvorak

mira-tdvorak Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Mira PR Walkthrough

This PR fixes a critical Android upload/download issue where the okhttp auth interceptor was incorrectly attaching Authorization headers to presigned object-storage URLs (S3, OSS, COS, OneDrive, etc.). Since presigned URLs already contain authentication via query parameters, the extra Authorization header causes 400 InvalidArgument errors. The fix scopes the bearer token to only the API host, skipping it for off-host presigned URLs.

1 file reviewed


Comment @mira-tdvorak help to get the list of available commands and usage tips.

@Dvorinka
Dvorinka merged commit 04f4ed6 into Dvorinka:master Nov 2, 2026
@Dvorinka Dvorinka mentioned this pull request Nov 2, 2026
@dxts
dxts deleted the fix/android-presigned-auth-header branch November 9, 2026 18:27
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