Skip to content

fix(better_networking): normalise URL before OAuth 1.0 signing - #1970

Draft
vibhanshu-s wants to merge 1 commit into
foss42:mainfrom
vibhanshu-s:resolve-issue-1969-oauth1-scheme-less-url
Draft

vibhanshu-s wants to merge 1 commit into
foss42:mainfrom
vibhanshu-s:resolve-issue-1969-oauth1-scheme-less-url

Conversation

@vibhanshu-s

Copy link
Copy Markdown

Summary

OAuth 1.0 requests no longer fail for URLs without an explicit scheme (e.g. api.example.com/v1, localhost:8080/x). The URL is normalised exactly as the request itself is, before the signature base string is built.

Fixes #1969

Root cause

generateOAuth1AuthHeader did Uri.parse(request.url) on the raw user input and then read uri.origin, which Dart only defines for absolute http/https URIs. Scheme defaulting and localhost/IP handling happen in getValidRequestUri, which runs only after handleAuth. The result was OAuth 1.0 authentication failed: Bad state: Origin is only applicable to schemes http and https before any network call.

Implementation

generateOAuth1AuthHeader now calls getValidRequestUri(request.url, null, defaultUriScheme: ...) and throws an ArgumentError with the validator's message if the URL is invalid. handleAuth and generateOAuth1AuthHeader take an optional defaultUriScheme (default kDefaultUriScheme), and both http_service.dart call sites pass the request's configured default scheme, so the signed base URI matches the URI that is actually sent.

Design decisions

Changes

  • packages/better_networking/lib/utils/auth/oauth1_utils.dart: normalise URL via getValidRequestUri.
  • packages/better_networking/lib/utils/auth/handle_auth.dart: accept and forward defaultUriScheme.
  • packages/better_networking/lib/services/http_service.dart: pass defaultUriScheme to handleAuth (2 call sites).
  • packages/better_networking/test/utils/auth/auth_handling_test.dart: regression tests.

Tests added

  • handleAuth + OAuth1 with scheme-less URLs (api.apidash.dev/users, localhost:8080/users, 127.0.0.1:8080/users) adds an OAuth ... Authorization header with an oauth_signature.
  • handleAuth + OAuth1 with an unsupported scheme (ftp://) throws a descriptive OAuth 1.0 authentication failed ... Unsupported URL Scheme exception.

Verification performed

Generated from recorded command results, not from agent claims.

  • Full checks were not run (repository code is untrusted; review inspection then re-run with --trust)
  • Regression test failing-before/passing-after: not demonstrated
  • Warning: possible secret in added lines

Limitations / risks

  • The tests have not been run. I did not have the Dart/Flutter SDK available when preparing this change, so neither flutter test nor dart analyze/format has been run and the regression tests have not been shown to fail before the fix. This PR is therefore a draft; CI results should be treated as the first verification.
  • AI-assisted: this change was prepared with Claude Code, per the AI usage policy in CONTRIBUTING.md.
  • The test fixtures contain dummy values (consumer-key / consumer-secret), not real credentials.

Repository PR checklist

PR Description

Add your description

Related Issues

  • Closes #

Video Demo

  • Video demonstrating before and after the fix.

Checklist

  • I have gone through the contributing guide
  • I have updated my branch and synced it with project main branch before making this PR
  • I am using the latest Flutter stable branch (run flutter upgrade and verify)
  • I have run the tests (flutter test) and all tests are passing

Added/updated tests?

We encourage you to add relevant test cases.

  • Yes
  • No, and this is why: please replace this line with details on why tests have not been included

OS on which you have developed and tested the feature?

  • Windows
  • macOS
  • Linux

…2#1969)

generateOAuth1AuthHeader parsed the raw request URL with Uri.parse and read uri.origin, which throws for URLs without an http/https scheme (e.g. api.example.com/v1 or localhost:8080), even though the request path accepts them via getValidRequestUri. Normalise the URL with getValidRequestUri (honouring the configured default scheme) before building the signature base string.
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.

[Bug] OAuth 1.0 fails with "Origin is only applicable to schemes http and https" for URLs without a scheme

1 participant