Skip to content

OIDC login fails with "The audience of the id_token did not contain …" when the id_token payload's base64url encoding contains - or _ #977

Closed

Description

@willyp713

Steps to reproduce

Deterministic, no IdP needed:

// A valid base64url-encoded JWT payload: {"aud":"OpenCloudDesktop","pic":"https://x.example/aa?v=1"}
// The '?' at zero-based payload offset 53 (≡ 2 mod 3) encodes to sextet 63 → '_' in base64url.
QByteArray part = "eyJhdWQiOiJPcGVuQ2xvdWREZXNrdG9wIiwicGljIjoiaHR0cHM6Ly94LmV4YW1wbGUvYWE_dj0xIn0";
qDebug() << QByteArray::fromBase64(part);                                // JWT::JWT() does this: garbage after ".../aa"
qDebug() << QByteArray::fromBase64(part, QByteArray::Base64UrlEncoding); // correct JSON

QJsonDocument::fromJson on the first decode fails, so JWT::payload() is an empty object.

Real-world path (how I hit it):

  1. OpenCloud with an external IdP (PocketID 2.11.0 here) and a public OIDC client with ID OpenCloudDesktop.
  2. Add the account in the desktop client, complete the browser login. The IdP includes a claim whose base64url encoding contains - or _ (in my case a gravatar_url claim whose ?v=… query string lands at the right byte alignment).
  3. Token exchange succeeds (IdP returns 200 with valid tokens), but the client shows: "Login Error — The audience of the id_token did not contain "OpenCloudDesktop"", although the id_token's actual audience is exactly ["OpenCloudDesktop"] (verified by performing the same PKCE exchange manually and decoding with a spec-compliant decoder).

Expected behavior

  • JWT segments are decoded as Base64url (RFC 7515 §2) and login succeeds.
  • If an id_token genuinely cannot be parsed, the error says so, rather than reporting an audience mismatch.

Actual behavior

  • JWT::JWT() decodes segments with QByteArray::fromBase64(part) standard-alphabet Base64. Qt's default IgnoreBase64DecodingErrors mode skips -/_ as invalid characters (Qt docs), dropping one 6-bit sextet each; all following bytes are regrouped and decode to garbage. The payload JSON no longer parses, IdToken::aud() returns {""}, and the check in oauth.cpp reports the audience error.

Analysis

  • Decoder: src/libsync/creds/jwt.cpp line 23 @ v3.0.3; unchanged on main @ c23507b. Introduced in e3f0a0f ("Verify login of correct user by reading the id_token").
  • Error site: src/libsync/creds/oauth.cpp line 373 @ v3.0.3.
  • Note JWT::serialize() in the same file already encodes with Base64UrlEncoding | OmitTrailingEquals and only the decode direction uses the wrong alphabet.
  • Believe only the payload segment matters for this failure: the signature is stored, not decoded, and the header isn't consulted on this path.
  • Why I think this isn't failing for everyone: sextets 62/63 (-/_) are rare in base64url of plain-ASCII JSON. For ASCII payloads they mainly arise from ? or > at payload offset ≡ 2 (mod 3), or from non-ASCII UTF-8 bytes. Compact tokens usually decode fine by luck; tokens with URL-with-query-string claims (picture/avatar URLs), display names with non-ASCII characters, etc. hit it depending on alignment. That makes the failure look random/IdP-specific while actually being content-dependent.
  • Verified with Qt 6.11.1 (behavior matches documented semantics of QByteArray::Base64Option, stable across Qt 6).

Suggested fix

Decode as Base64url and surface parse failures instead of letting them masquerade as audience mismatches:

auto parse = [](const QByteArray &part) {
    const auto result = QByteArray::fromBase64Encoding(part,
        QByteArray::Base64UrlEncoding | QByteArray::AbortOnBase64DecodingErrors);
    if (!result) {
        return QJsonObject{};
    }
    return QJsonDocument::fromJson(*result).object();
};

plus (ideally) a distinct "the id_token could not be parsed" error in oauth.cpp before the aud() check. With only the decoder fixed, a genuinely malformed token would still produce the misleading audience message. (Since the JWT is constructed as a temporary there, the simplest check is on the resulting IdToken's payload being empty.)

One deliberate interoperability consequence worth noting: strict Base64url decoding will reject tokens from IdPs that (non-compliantly) emit standard-alphabet +// in JWT segments. That matches RFC 7515, but it is a behavior change for such IdPs.

Possibly related: #217 reports the audience error with Authelia when roles are configured. Plausibly the same decode failure triggered by different payload content, though I haven't looked into it.

Setup

  • Windows 11 25H2 (OS build 26200.8875), OpenCloud desktop 3.0.3.2073 (mirall/3.0.3)
  • Server: OpenCloud (Docker, external IdP: PocketID 2.11.0)
  • Log: IdP-side log shows POST /api/oidc/token → 200 with valid tokens; client then reports the audience error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions