From 7ce426e775b49c7e76b373ef207f6b256f9ede5d Mon Sep 17 00:00:00 2001 From: vibhanshu-s <2k24.cs1q.2413927@gmail.com> Date: Tue, 1 Dec 2026 23:48:31 +0530 Subject: [PATCH] fix(better_networking): normalise URL before OAuth 1.0 signing (#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. --- .../lib/services/http_service.dart | 7 ++- .../lib/utils/auth/handle_auth.dart | 6 +- .../lib/utils/auth/oauth1_utils.dart | 17 +++++- .../test/utils/auth/auth_handling_test.dart | 59 +++++++++++++++++++ 4 files changed, 83 insertions(+), 6 deletions(-) diff --git a/packages/better_networking/lib/services/http_service.dart b/packages/better_networking/lib/services/http_service.dart index 16d740f578..df2b3c6a7e 100644 --- a/packages/better_networking/lib/services/http_service.dart +++ b/packages/better_networking/lib/services/http_service.dart @@ -37,7 +37,11 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequestV1( try { if (authData != null && authData.type != APIAuthType.none) { - authenticatedRequestModel = await handleAuth(requestModel, authData); + authenticatedRequestModel = await handleAuth( + requestModel, + authData, + defaultUriScheme: defaultUriScheme, + ); } } catch (e) { return (null, null, e.toString()); @@ -267,6 +271,7 @@ Future> streamHttpRequest( authenticatedHttpRequestModel = await handleAuth( httpRequestModel, authData, + defaultUriScheme: defaultUriScheme, ); } } catch (e) { diff --git a/packages/better_networking/lib/utils/auth/handle_auth.dart b/packages/better_networking/lib/utils/auth/handle_auth.dart index e965d78450..2c9ec29056 100644 --- a/packages/better_networking/lib/utils/auth/handle_auth.dart +++ b/packages/better_networking/lib/utils/auth/handle_auth.dart @@ -11,8 +11,9 @@ import 'oauth1_utils.dart'; Future handleAuth( HttpRequestModel httpRequestModel, - AuthModel? authData, -) async { + AuthModel? authData, { + SupportedUriSchemes defaultUriScheme = kDefaultUriScheme, +}) async { if (authData == null || authData.type == APIAuthType.none) { return httpRequestModel; } @@ -165,6 +166,7 @@ Future handleAuth( final client = generateOAuth1AuthHeader( oauth1Model, httpRequestModel, + defaultUriScheme: defaultUriScheme, ); updatedHeaders.add( diff --git a/packages/better_networking/lib/utils/auth/oauth1_utils.dart b/packages/better_networking/lib/utils/auth/oauth1_utils.dart index e634ae12a6..6b9d9e53be 100644 --- a/packages/better_networking/lib/utils/auth/oauth1_utils.dart +++ b/packages/better_networking/lib/utils/auth/oauth1_utils.dart @@ -3,6 +3,7 @@ import 'dart:math' as math; import 'package:crypto/crypto.dart'; import '../../models/models.dart'; import '../../consts.dart'; +import '../uri_utils.dart'; /// Generates a simple OAuth 1.0a Authorization header directly from AuthOAuth1Model model /// @@ -17,8 +18,9 @@ import '../../consts.dart'; /// - Formats the Authorization header String generateOAuth1AuthHeader( AuthOAuth1Model oauth1Model, - HttpRequestModel request, -) { + HttpRequestModel request, { + SupportedUriSchemes defaultUriScheme = kDefaultUriScheme, +}) { // Generate OAuth parameters final timestamp = (DateTime.now().millisecondsSinceEpoch ~/ 1000).toString(); final nonce = _generateNonce(); @@ -40,7 +42,16 @@ String generateOAuth1AuthHeader( // Create signature base string and signing key final method = request.method.name.toUpperCase(); - final uri = Uri.parse(request.url); + // Normalise the URL exactly like the request itself will be (default scheme, + // localhost / IP handling) so the signed base URI matches what is sent. + final (uri, uriError) = getValidRequestUri( + request.url, + null, + defaultUriScheme: defaultUriScheme, + ); + if (uri == null) { + throw ArgumentError(uriError ?? 'Invalid request URL'); + } final baseString = _createSignatureBaseString(method, uri, oauthParams); // Generate signature based on signature method diff --git a/packages/better_networking/test/utils/auth/auth_handling_test.dart b/packages/better_networking/test/utils/auth/auth_handling_test.dart index 9b8857f136..1f77038d70 100644 --- a/packages/better_networking/test/utils/auth/auth_handling_test.dart +++ b/packages/better_networking/test/utils/auth/auth_handling_test.dart @@ -439,6 +439,65 @@ void main() { }, ); + group('OAuth1 URL normalisation', () { + const oauth1Auth = AuthModel( + type: APIAuthType.oauth1, + oauth1: AuthOAuth1Model( + consumerKey: 'consumer-key', + consumerSecret: 'consumer-secret', + signatureMethod: OAuth1SignatureMethod.hmacSha1, + ), + ); + + for (final url in [ + 'api.apidash.dev/users', + 'localhost:8080/users', + '127.0.0.1:8080/users', + ]) { + test( + 'given handleAuth when OAuth1 is used with scheme-less URL $url then it should add an OAuth Authorization header', + () async { + final httpRequestModel = HttpRequestModel( + method: HTTPVerb.get, + url: url, + ); + + final result = await handleAuth(httpRequestModel, oauth1Auth); + + final header = result.headers?.firstWhere( + (h) => h.name == 'Authorization', + ); + expect(header?.value, startsWith('OAuth ')); + expect(header?.value, contains('oauth_signature="')); + }, + ); + } + + test( + 'given handleAuth when OAuth1 is used with an unsupported scheme then it should throw a descriptive Exception', + () async { + const httpRequestModel = HttpRequestModel( + method: HTTPVerb.get, + url: 'ftp://api.apidash.dev/users', + ); + + expect( + () async => await handleAuth(httpRequestModel, oauth1Auth), + throwsA( + isA().having( + (e) => e.toString(), + 'message', + allOf( + contains('OAuth 1.0 authentication failed'), + contains('Unsupported URL Scheme'), + ), + ), + ), + ); + }, + ); + }); + test( 'given handleAuth when OAuth2 authentication is provided but oauth2 data is null then it should throw Exception', () async {