Repository navigation
fix(auth): fail with an AuthException when a redirect response has no url - #1728
Conversation
… url getLinkIdentityUrl passed the decoded body straight into OAuthResponse, and getSSOSignInUrl cast it with a bare 'as String'. The body is untyped, so a server that omits 'url' or sends the wrong type for it surfaced as a TypeError from whichever line consumed the value first, outside this package's exception hierarchy. Read the field through one helper that checks it and throws an AuthException instead, matching how the client already reports missing response fields such as 'No access_token detected.'.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: Your plan includes up to 4 reviews per rolling hour; 1 remains after this review. 📝 WalkthroughWalkthroughSSO and identity-link URL responses now use shared validation. Invalid or missing URL values throw ChangesRedirect URL validation
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to The change converts malformed redirect responses into the package's expected AuthException instead of an unhandled type error, with focused tests covering the affected cases; no actionable merge-blocking risk remains after normal checks and review. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
A server that answers with a literal JSON null decodes to null, reaches the same guard and reports the same AuthException, so cover it for both methods. This also settles DCM's avoid-unnecessary-nullable-parameters warning on the test helper honestly: the parameter is nullable because a null body is now a case under test, rather than dropping the '?' to quiet the rule.
…ing (#1729) ## What kind of change does this PR introduce? Breaking change, refactor. Closes SDK-1496. > [!NOTE] > This was originally stacked on #1728. That has landed, so this branch is > rebased directly onto `main` and stands on its own. ## What is the current behavior? #1722 changed `getSSOSignInUrl` to return a `Future<Uri>`, which left the two sibling URL getters disagreeing with it: | Method | Returns | | --- | --- | | `AuthClient.getSSOSignInUrl` | `Future<Uri>` | | `AuthClient.getOAuthSignInUrl` | `Future<OAuthResponse>` with `String url` | | `AuthClient.getLinkIdentityUrl` | `Future<OAuthResponse>` with `String url` | Shipping v3 that way means three URL-returning auth methods with two different types for the same concept, and callers still writing `Uri.parse` for two of them. ## What is the new behavior? `OAuthResponse.url` is a `Uri`. Both getters share that type, so this covers them in one change and makes all three consistent. Two internal tidy-ups fall out of it: - `_getUrlForProvider` returns the `Uri` it builds rather than a whole `OAuthResponse`. `getLinkIdentityUrl` only ever wanted the URL to send a request to, and the response type belongs at the public boundary, so it is now constructed there by both public methods. - `supabase_flutter`'s `_launchAuthUrl` takes a `Uri` and drops its internal `Uri.parse`. It hands the value straight to `launchUrl`. ## The wire format is unchanged This is the part worth scrutinising, since the URL is built by string interpolation and is now parsed before it leaves the client. The query is still built with `Uri(queryParameters: ...)` exactly as before, and `Uri.parse` round-trips the result byte for byte. Verified against the shapes this code actually produces: ``` SAME ...?provider=google&redirect_to=https%3A%2F%2Fsupabase.com SAME ...?provider=github&scopes=repo&redirect_to=redirectToURL SAME ...?provider=custom%3Amy-oidc-provider&scopes=openid+profile+email SAME ...?provider=google&redirect_to=my-app%3A%2F%2Fcallback%3Fa%3Db%26c%3Dd SAME ...?provider=google&code_challenge=abc-_~.123&flow_type=pkce ``` Percent-encoded redirect targets, nested query strings inside `redirect_to`, plus-encoded spaces in `scopes` and the PKCE challenge characters all survive unchanged. `client_test.dart` also still asserts one full URL by exact string equality, including `%3A%2F%2F`, and passes against a real GoTrue. ## Tests Existing coverage was updated rather than replaced. Three tests in `client_test.dart` named "can append a redirectUrl", "can append scopes" and "can append options" only asserted `isA<String>()` and never checked the thing their names promise. Now that `url` is a `Uri` they read the parameters directly: ```dart expect(response.url.queryParameters['scopes'], 'repo'); ``` The remaining `isA<String>()` assertions became `isA<Uri>()`. Those were tautological before this PR and still are, so they are left alone rather than rewritten here. Verified with the local Supabase stack running: - `packages/supabase_auth`: `dart test -j 1`, 472 passing, which includes `getLinkIdentityUrl` against real GoTrue returning a real Google URL - `packages/supabase_flutter`: `flutter test`, 76 passing - `dart analyze packages`: no issues - `dart format`: no changes ## Migration `MIGRATION.md` gets an `OAuthResponse.url` section next to the `getSSOSignInUrl` one, and the affected `sdk-compliance.yaml` notes are updated. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Enhancements** * OAuth sign-in and identity-linking URLs are now returned as `Uri` values. * URL query parameters can be inspected directly without manual parsing. * Flutter launches OAuth URLs directly and preserves the provided URI. * **Documentation** * Added migration guidance for upgrading from v2 to v3, including converting URIs to text with `toString()` when needed. * Documented Dart and Flutter OAuth URL and return-type differences. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
What kind of change does this PR introduce?
Bug fix. Closes SDK-1497.
What is the current behavior?
Two methods read a
urlout of an untyped response body without checking it.getLinkIdentityUrlhanded the value straight toOAuthResponse:getSSOSignInUrlcast it:strict-castsis not enabled, so the first line compiles as an implicitdowncast. Both forms behave the same at runtime: if the server omits
url, orsends a number or a list for it, the caller gets a bare
TypeErrorfromwhichever line consumes the value first, with nothing pointing at the response
that caused it. A
TypeErroris also outside this package's exceptionhierarchy, so callers that catch
AuthExceptiondo not see it.Worth being explicit about: adding an
as Stringto the first line would havechanged nothing at runtime. The implicit downcast already throws the same
TypeErrorin the same place. Only a real check changes the behaviour.What is the new behavior?
Both call sites read the field through one helper that validates it and throws
an
AuthExceptionat the response boundary:The message follows the convention the client already uses for missing response
fields (
No access_token detected.,No refresh_token detected., and so on).The
response is Mapguard means a body that is not an object at all, a JSONlist for instance, reports the same error rather than a
NoSuchMethodError.Tests
packages/supabase_auth/test/redirect_url_mock_test.dartcovers both methodsagainst three malformed bodies each: field absent, field present with the wrong
type, and a body that is not an object. All six fail with a
TypeErroron theold code and pass on the new code, so they pin down the behaviour rather than
just the happy path.
The mock is a small reusable
JsonResponseMockClientintest/mocks/, sinceevery existing mocked test in this package hand-rolls its own client.
No backend needed:
dart test test/redirect_url_mock_test.dartruns standalone.Notes
Not a breaking change, so this does not need to wait for v3.
SDK-1496 changes
OAuthResponse.urlto aUriand touches the samegetLinkIdentityUrlline. That PR is stacked on this branch, so merge this onefirst.
Summary by CodeRabbit
Bug Fixes
Tests