1
- import { validateResourceUri , extractResourceUri , resourceUrlFromServerUrl } from './auth-utils.js' ;
1
+ import { resourceUrlFromServerUrl } from './auth-utils.js' ;
2
2
3
3
describe ( 'auth-utils' , ( ) => {
4
4
describe ( 'resourceUrlFromServerUrl' , ( ) => {
5
5
it ( 'should remove fragments' , ( ) => {
6
- expect ( resourceUrlFromServerUrl ( 'https://example.com/path#fragment' ) ) . toBe ( 'https://example.com/path' ) ;
7
- expect ( resourceUrlFromServerUrl ( 'https://example.com#fragment' ) ) . toBe ( 'https://example.com' ) ;
8
- expect ( resourceUrlFromServerUrl ( 'https://example.com/path?query=1#fragment' ) ) . toBe ( 'https://example.com/path?query=1' ) ;
6
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https://example.com/path#fragment' ) ) . href ) . toBe ( 'https://example.com/path' ) ;
7
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https://example.com#fragment' ) ) . href ) . toBe ( 'https://example.com/ ' ) ;
8
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https://example.com/path?query=1#fragment' ) ) . href ) . toBe ( 'https://example.com/path?query=1' ) ;
9
9
} ) ;
10
10
11
11
it ( 'should return URL unchanged if no fragment' , ( ) => {
12
- expect ( resourceUrlFromServerUrl ( 'https://example.com' ) ) . toBe ( 'https://example.com' ) ;
13
- expect ( resourceUrlFromServerUrl ( 'https://example.com/path' ) ) . toBe ( 'https://example.com/path' ) ;
14
- expect ( resourceUrlFromServerUrl ( 'https://example.com/path?query=1' ) ) . toBe ( 'https://example.com/path?query=1' ) ;
12
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https://example.com' ) ) . href ) . toBe ( 'https://example.com/ ' ) ;
13
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https://example.com/path' ) ) . href ) . toBe ( 'https://example.com/path' ) ;
14
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https://example.com/path?query=1' ) ) . href ) . toBe ( 'https://example.com/path?query=1' ) ;
15
15
} ) ;
16
16
17
17
it ( 'should keep everything else unchanged' , ( ) => {
18
18
// Case sensitivity preserved
19
- expect ( resourceUrlFromServerUrl ( 'HTTPS ://EXAMPLE.COM/PATH') ) . toBe ( 'HTTPS ://EXAMPLE.COM /PATH' ) ;
19
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https ://EXAMPLE.COM/PATH') ) . href ) . toBe ( 'https ://example.com /PATH' ) ;
20
20
// Ports preserved
21
- expect ( resourceUrlFromServerUrl ( 'https://example.com:443/path' ) ) . toBe ( 'https://example.com:443 /path' ) ;
22
- expect ( resourceUrlFromServerUrl ( 'https://example.com:8080/path' ) ) . toBe ( 'https://example.com:8080/path' ) ;
21
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https://example.com:443/path' ) ) . href ) . toBe ( 'https://example.com/path' ) ;
22
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https://example.com:8080/path' ) ) . href ) . toBe ( 'https://example.com:8080/path' ) ;
23
23
// Query parameters preserved
24
- expect ( resourceUrlFromServerUrl ( 'https://example.com?foo=bar&baz=qux' ) ) . toBe ( 'https://example.com?foo=bar&baz=qux' ) ;
24
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https://example.com?foo=bar&baz=qux' ) ) . href ) . toBe ( 'https://example.com/ ?foo=bar&baz=qux' ) ;
25
25
// Trailing slashes preserved
26
- expect ( resourceUrlFromServerUrl ( 'https://example.com/' ) ) . toBe ( 'https://example.com/' ) ;
27
- expect ( resourceUrlFromServerUrl ( 'https://example.com/path/' ) ) . toBe ( 'https://example.com/path/' ) ;
28
- } ) ;
29
- } ) ;
30
-
31
-
32
- describe ( 'validateResourceUri' , ( ) => {
33
- it ( 'should accept valid resource URIs without fragments' , ( ) => {
34
- expect ( ( ) => validateResourceUri ( 'https://example.com' ) ) . not . toThrow ( ) ;
35
- expect ( ( ) => validateResourceUri ( 'https://example.com/path' ) ) . not . toThrow ( ) ;
36
- expect ( ( ) => validateResourceUri ( 'http://example.com:8080' ) ) . not . toThrow ( ) ;
37
- expect ( ( ) => validateResourceUri ( 'https://example.com?query=1' ) ) . not . toThrow ( ) ;
38
- expect ( ( ) => validateResourceUri ( 'ftp://example.com' ) ) . not . toThrow ( ) ; // Only fragment check now
39
- } ) ;
40
-
41
- it ( 'should reject URIs with fragments' , ( ) => {
42
- expect ( ( ) => validateResourceUri ( 'https://example.com#fragment' ) ) . toThrow ( 'must not contain a fragment' ) ;
43
- expect ( ( ) => validateResourceUri ( 'https://example.com/path#section' ) ) . toThrow ( 'must not contain a fragment' ) ;
44
- expect ( ( ) => validateResourceUri ( 'https://example.com?query=1#anchor' ) ) . toThrow ( 'must not contain a fragment' ) ;
45
- } ) ;
46
-
47
- it ( 'should accept any URI without fragment' , ( ) => {
48
- // These are all valid now since we only check for fragments
49
- expect ( ( ) => validateResourceUri ( '//example.com' ) ) . not . toThrow ( ) ;
50
- expect ( ( ) => validateResourceUri ( 'https://user:[email protected] ' ) ) . not . toThrow ( ) ;
51
- expect ( ( ) => validateResourceUri ( '/path' ) ) . not . toThrow ( ) ;
52
- expect ( ( ) => validateResourceUri ( 'path' ) ) . not . toThrow ( ) ;
53
- } ) ;
54
- } ) ;
55
-
56
- describe ( 'extractResourceUri' , ( ) => {
57
- it ( 'should remove fragments from URLs' , ( ) => {
58
- expect ( extractResourceUri ( 'https://example.com/path#fragment' ) ) . toBe ( 'https://example.com/path' ) ;
59
- expect ( extractResourceUri ( 'https://example.com/path?query=1#fragment' ) ) . toBe ( 'https://example.com/path?query=1' ) ;
60
- } ) ;
61
-
62
- it ( 'should handle URL object' , ( ) => {
63
- const url = new URL ( 'https://example.com:8443/path?query=1#fragment' ) ;
64
- expect ( extractResourceUri ( url ) ) . toBe ( 'https://example.com:8443/path?query=1' ) ;
65
- } ) ;
66
-
67
- it ( 'should keep everything else unchanged' , ( ) => {
68
- // Preserves case
69
- expect ( extractResourceUri ( 'HTTPS://EXAMPLE.COM/path' ) ) . toBe ( 'HTTPS://EXAMPLE.COM/path' ) ;
70
- // Preserves all ports
71
- expect ( extractResourceUri ( 'https://example.com:443/path' ) ) . toBe ( 'https://example.com:443/path' ) ;
72
- expect ( extractResourceUri ( 'http://example.com:80/path' ) ) . toBe ( 'http://example.com:80/path' ) ;
73
- // Preserves query parameters
74
- expect ( extractResourceUri ( 'https://example.com/path?query=1' ) ) . toBe ( 'https://example.com/path?query=1' ) ;
75
- // Preserves trailing slashes
76
- expect ( extractResourceUri ( 'https://example.com/' ) ) . toBe ( 'https://example.com/' ) ;
77
- expect ( extractResourceUri ( 'https://example.com/app1/' ) ) . toBe ( 'https://example.com/app1/' ) ;
78
- } ) ;
79
-
80
- it ( 'should distinguish between different paths on same domain' , ( ) => {
81
- // This is the key test for the security concern mentioned
82
- const app1 = extractResourceUri ( 'https://api.example.com/mcp-server-1' ) ;
83
- const app2 = extractResourceUri ( 'https://api.example.com/mcp-server-2' ) ;
84
- expect ( app1 ) . not . toBe ( app2 ) ;
85
- expect ( app1 ) . toBe ( 'https://api.example.com/mcp-server-1' ) ;
86
- expect ( app2 ) . toBe ( 'https://api.example.com/mcp-server-2' ) ;
26
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https://example.com/' ) ) . href ) . toBe ( 'https://example.com/' ) ;
27
+ expect ( resourceUrlFromServerUrl ( new URL ( 'https://example.com/path/' ) ) . href ) . toBe ( 'https://example.com/path/' ) ;
87
28
} ) ;
88
29
} ) ;
89
30
} ) ;
0 commit comments