mirror of
https://github.com/linagora/tmail-flutter.git
synced 2026-12-06 16:04:20 +00:00
fix(network): never send credentials with WebFinger discovery
AuthorizationInterceptors attaches the user's Basic or Bearer credentials to every request of the shared Dio, whatever the host. WebFinger requests are public discovery and are also sent to hosts guessed from the email address domain (autodiscover., jmap., ...), which may be third parties; after a session expiry the stale credentials were still attached. Add a per-request opt-out (RequestOptions.extra skipAuthorization) and use it for WebFinger. A general host allow-list (JMAP origin, session capability hosts, OIDC userinfo host) is left for a follow-up: it needs to be kept in sync across the main, isolate and FCM background paths. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
931eeac3c6
commit
88ae784f5f
4 files changed
+76
-5
No files matched your search
@@ -29,6 +29,11 @@ import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
|
||||
class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
|
||||
static const String _refreshAttemptedKey = '_authInterceptorRefreshAttempted';
|
||||
|
||||
/// Set to `true` in [RequestOptions.extra] for requests that must never
|
||||
/// carry the user's credentials, e.g. discovery requests sent to hosts
|
||||
/// guessed from the email address domain.
|
||||
static const String skipAuthorizationKey = 'skipAuthorization';
|
||||
|
||||
final Dio _dio;
|
||||
final AuthenticationClientBase _authenticationClient;
|
||||
final TokenOidcCacheManager _tokenOidcCacheManager;
|
||||
@@ -153,6 +158,12 @@ class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
|
||||
|
||||
@override
|
||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
||||
if (options.extra[skipAuthorizationKey] == true) {
|
||||
options.headers.remove(HttpHeaders.authorizationHeader);
|
||||
super.onRequest(options, handler);
|
||||
return;
|
||||
}
|
||||
|
||||
switch(_authenticationType) {
|
||||
case AuthenticationType.basic:
|
||||
if (_authorization != null) {
|
||||
|
||||
@@ -5,7 +5,7 @@ import 'dart:io';
|
||||
import 'package:core/data/model/query/query_parameter.dart';
|
||||
import 'package:core/data/network/dio_client.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:dio/dio.dart' show DioException;
|
||||
import 'package:dio/dio.dart' show DioException, Options;
|
||||
import 'package:get/get_connect/http/src/exceptions/exceptions.dart';
|
||||
import 'package:model/oidc/oidc_configuration.dart';
|
||||
import 'package:model/oidc/request/oidc_request.dart';
|
||||
@@ -15,6 +15,7 @@ import 'package:model/oidc/response/oidc_user_info.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/extensions/service_path_extension.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/config/oidc_constant.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/endpoint.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/interceptors/authorization_interceptors.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/oidc_error.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/exceptions/login_exception.dart';
|
||||
import 'package:tmail_ui_user/main/utils/app_config.dart';
|
||||
@@ -34,7 +35,12 @@ class OIDCHttpClient {
|
||||
StringQueryParameter('resource', oidcRequest.resourceUrl),
|
||||
StringQueryParameter('rel', OIDCRequest.relUrl),
|
||||
])
|
||||
.generateEndpointPath()
|
||||
.generateEndpointPath(),
|
||||
// WebFinger is public discovery, possibly sent to hosts guessed from
|
||||
// the email address domain: never attach the user's credentials.
|
||||
options: Options(
|
||||
extra: {AuthorizationInterceptors.skipAuthorizationKey: true},
|
||||
),
|
||||
);
|
||||
log('OIDCHttpClient::checkOIDCIsAvailable(): RESULT: $result');
|
||||
if (result is Map<String, dynamic>) {
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
||||
import 'package:model/account/password.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/local/account_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/authentication_client/authentication_client_base.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/network/interceptors/authorization_interceptors.dart';
|
||||
import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
|
||||
|
||||
class _FakeAuthenticationClient extends Fake implements AuthenticationClientBase {}
|
||||
|
||||
class _FakeTokenOidcCacheManager extends Fake implements TokenOidcCacheManager {}
|
||||
|
||||
class _FakeAccountCacheManager extends Fake implements AccountCacheManager {}
|
||||
|
||||
class _FakeIOSSharingManager extends Fake implements IOSSharingManager {}
|
||||
|
||||
void main() {
|
||||
late AuthorizationInterceptors interceptors;
|
||||
|
||||
setUp(() {
|
||||
interceptors = AuthorizationInterceptors(
|
||||
Dio(),
|
||||
_FakeAuthenticationClient(),
|
||||
_FakeTokenOidcCacheManager(),
|
||||
_FakeAccountCacheManager(),
|
||||
_FakeIOSSharingManager(),
|
||||
);
|
||||
interceptors.setBasicAuthorization(UserName('alice'), Password('secret'));
|
||||
});
|
||||
|
||||
RequestOptions intercept(RequestOptions options) {
|
||||
interceptors.onRequest(options, RequestInterceptorHandler());
|
||||
return options;
|
||||
}
|
||||
|
||||
test('SHOULD attach credentials to regular requests', () {
|
||||
final options = intercept(RequestOptions(path: 'https://jmap.example.com/jmap'));
|
||||
|
||||
expect(options.headers[HttpHeaders.authorizationHeader], startsWith('Basic '));
|
||||
});
|
||||
|
||||
test('SHOULD NOT attach credentials when the request opts out', () {
|
||||
final options = intercept(RequestOptions(
|
||||
path: 'https://guessed.example.org/.well-known/webfinger',
|
||||
extra: {AuthorizationInterceptors.skipAuthorizationKey: true},
|
||||
));
|
||||
|
||||
expect(options.headers.containsKey(HttpHeaders.authorizationHeader), isFalse);
|
||||
});
|
||||
}
|
||||
@@ -24,7 +24,7 @@ void main() {
|
||||
'and status code is 404',
|
||||
() {
|
||||
// arrange
|
||||
when(dioClient.get(any)).thenThrow(DioException(
|
||||
when(dioClient.get(any, options: anyNamed('options'))).thenThrow(DioException(
|
||||
requestOptions: requestOptions,
|
||||
response: Response(requestOptions: requestOptions, statusCode: 404)));
|
||||
|
||||
@@ -41,7 +41,7 @@ void main() {
|
||||
'and status code is not 404',
|
||||
() {
|
||||
// arrange
|
||||
when(dioClient.get(any)).thenThrow(DioException(
|
||||
when(dioClient.get(any, options: anyNamed('options'))).thenThrow(DioException(
|
||||
requestOptions: requestOptions,
|
||||
response: Response(requestOptions: requestOptions, statusCode: 403)));
|
||||
|
||||
@@ -57,7 +57,7 @@ void main() {
|
||||
'and dioClient throw exception that is not DioException',
|
||||
() {
|
||||
// arrange
|
||||
when(dioClient.get(any)).thenThrow(Exception());
|
||||
when(dioClient.get(any, options: anyNamed('options'))).thenThrow(Exception());
|
||||
|
||||
// assert
|
||||
expect(
|
||||
|
||||
Reference in new issue
Block a user