import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_url.dart'; void main() { group('server-relative paths', () { test( 'resolve against the configured endpoint origin, dropping its path', () { final resolved = resolveArtifactUrl( 'ws://example.test:8081/connect?token=x', '/artifacts/a%20b?download=1', ); expect(resolved, isNotNull); expect( resolved!.uri.toString(), 'http://example.test:8081/artifacts/a%20b?download=1', ); expect(resolved.external, isFalse); }, ); test('map wss to https and omit the default port', () { final resolved = resolveArtifactUrl( 'wss://example.test/connect', 'artifacts/shot.png', ); expect( resolved!.uri.toString(), 'https://example.test/artifacts/shot.png', ); expect(resolved.external, isFalse); }); test('keep IPv6 brackets and encoded paths intact', () { final resolved = resolveArtifactUrl( 'ws://[::1]:8081/connect', '/artifacts/a%2Fb?next=%2Fnext', ); expect( resolved!.uri.toString(), 'http://[::1]:8081/artifacts/a%2Fb?next=%2Fnext', ); expect(resolved.uri.host, '::1'); }); test('a blank endpoint leaves a relative artifact unresolved', () { expect(resolveArtifactUrl('', '/artifacts/a'), isNull); expect(resolveArtifactUrl('not a url', '/artifacts/a'), isNull); expect( resolveArtifactUrl('https://example.test', '/artifacts/a'), isNull, ); }); }); group('absolute http(s) artifacts', () { test('a same-origin absolute url stays internal', () { final resolved = resolveArtifactUrl( 'ws://example.test:8081/connect', 'http://example.test:8081/artifacts/a%20b?download=1', ); expect( resolved!.uri.toString(), 'http://example.test:8081/artifacts/a%20b?download=1', ); expect(resolved.external, isFalse); }); test('cross-host, cross-port and cross-scheme urls are external', () { const configured = 'wss://example.test/connect'; for (final url in [ 'https://cdn.test/artifacts/a', 'https://example.test:8443/artifacts/a', 'http://example.test/artifacts/a', ]) { final resolved = resolveArtifactUrl(configured, url); expect(resolved, isNotNull, reason: url); expect(resolved!.external, isTrue, reason: url); } }); test('an absolute url without a usable configured origin is external', () { final resolved = resolveArtifactUrl('', 'https://cdn.test/a%2Fb?q=1'); expect(resolved!.uri.toString(), 'https://cdn.test/a%2Fb?q=1'); expect(resolved.external, isTrue); }); }); group('unsafe urls', () { test('rejects blank, protocol-relative and hostless urls', () { for (final url in [ '', ' ', '//evil.test/artifacts/a', 'http:///artifacts/a', 'http://', 'https://?x=1', ]) { expect( resolveArtifactUrl('ws://example.test:8081', url), isNull, reason: url, ); } }); test('rejects credentials in any absolute url', () { for (final url in [ 'http://user:pass@example.test/artifacts/a', 'https://user@example.test/artifacts/a', 'http://example.test:8081@evil.test/artifacts/a', ]) { expect( resolveArtifactUrl('ws://example.test:8081', url), isNull, reason: url, ); } }); test('rejects unsafe and non-http schemes', () { for (final url in [ 'javascript:alert(1)', 'data:text/html,hello', 'file:///etc/passwd', 'ftp://example.test/a', 'intent://example.test/a#Intent;end', 'mailto:ops@example.test', ]) { expect( resolveArtifactUrl('ws://example.test:8081', url), isNull, reason: url, ); } }); test('never produces a non-http(s) output', () { final resolved = resolveArtifactUrl( 'ws://example.test:8081/connect', '/artifacts/a', ); expect(resolved!.uri.scheme, 'http'); expect(resolved.uri.hasAuthority, isTrue); expect(resolved.uri.userInfo, isEmpty); }); }); }