forked from CredenceOrg/Credence-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafeOpenExternal.test.ts
More file actions
229 lines (196 loc) · 8.13 KB
/
Copy pathsafeOpenExternal.test.ts
File metadata and controls
229 lines (196 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { safeOpenExternal } from './safeOpenExternal'
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Capture every window.open call without actually opening a tab. */
function mockWindowOpen() {
const spy = vi.fn().mockReturnValue({} as WindowProxy)
vi.stubGlobal('open', spy)
return spy
}
// ---------------------------------------------------------------------------
// Suite
// ---------------------------------------------------------------------------
describe('safeOpenExternal', () => {
let openSpy: ReturnType<typeof vi.fn>
beforeEach(() => {
openSpy = mockWindowOpen()
})
afterEach(() => {
vi.unstubAllGlobals()
})
// -------------------------------------------------------------------------
// Happy path — allowed protocols
// -------------------------------------------------------------------------
describe('allowed protocols', () => {
it('opens an https URL and returns ok:true', () => {
const result = safeOpenExternal('https://stellar.expert/explorer/public/tx/abc')
expect(result.ok).toBe(true)
expect(openSpy).toHaveBeenCalledOnce()
})
it('opens an http URL', () => {
const result = safeOpenExternal('http://example.com')
expect(result.ok).toBe(true)
expect(openSpy).toHaveBeenCalledOnce()
})
it('opens a mailto URL', () => {
const result = safeOpenExternal('mailto:support@credence.org')
expect(result.ok).toBe(true)
expect(openSpy).toHaveBeenCalledOnce()
})
it('passes the URL as the first argument to window.open', () => {
const url = 'https://example.com/path?q=1'
safeOpenExternal(url)
expect(openSpy).toHaveBeenCalledWith(url, '_blank', expect.any(String))
})
it('always opens in _blank target', () => {
safeOpenExternal('https://example.com')
const [, target] = openSpy.mock.calls[0]
expect(target).toBe('_blank')
})
})
// -------------------------------------------------------------------------
// Security: noopener / noreferrer enforcement
// -------------------------------------------------------------------------
describe('noopener and noreferrer enforcement', () => {
it('injects noopener and noreferrer when no features are supplied', () => {
safeOpenExternal('https://example.com')
const [, , features] = openSpy.mock.calls[0]
expect(features).toContain('noopener')
expect(features).toContain('noreferrer')
})
it('injects noopener and noreferrer when the caller omits them', () => {
safeOpenExternal('https://example.com', 'width=800,height=600')
const [, , features] = openSpy.mock.calls[0]
expect(features).toContain('noopener')
expect(features).toContain('noreferrer')
})
it('does not duplicate noopener when caller already includes it', () => {
safeOpenExternal('https://example.com', 'noopener')
const [, , features] = openSpy.mock.calls[0]
const count = features.split(',').filter((f: string) => f === 'noopener').length
expect(count).toBe(1)
})
it('does not duplicate noreferrer when caller already includes it', () => {
safeOpenExternal('https://example.com', 'noreferrer')
const [, , features] = openSpy.mock.calls[0]
const count = features.split(',').filter((f: string) => f === 'noreferrer').length
expect(count).toBe(1)
})
it('preserves extra caller-supplied feature tokens alongside the security tokens', () => {
safeOpenExternal('https://example.com', 'width=800,height=600')
const [, , features] = openSpy.mock.calls[0]
expect(features).toContain('width=800')
expect(features).toContain('height=600')
})
})
// -------------------------------------------------------------------------
// Negative path — BLOCKED protocols (these tests fail without the fix)
// -------------------------------------------------------------------------
describe('blocked protocols — negative tests', () => {
/**
* NEGATIVE TEST: javascript: URI
*
* Without the protocol allowlist, `window.open('javascript:alert(1)')` would
* execute arbitrary script in the opener's context, enabling credential theft
* or DOM manipulation. This test was written RED (failing) before the fix
* and PASSES after the allowlist is in place.
*/
it('NEGATIVE — blocks javascript: URI and does NOT call window.open', () => {
const result = safeOpenExternal('javascript:alert(1)')
expect(result.ok).toBe(false)
expect(openSpy).not.toHaveBeenCalled()
})
it('returns blocked_protocol error kind for javascript:', () => {
const result = safeOpenExternal('javascript:void(0)')
expect(result.ok).toBe(false)
if (!result.ok) {
expect(result.error.kind).toBe('blocked_protocol')
expect(result.error.protocol).toBe('javascript:')
}
})
it('NEGATIVE — blocks data: URI and does NOT call window.open', () => {
const result = safeOpenExternal('data:text/html,<script>alert(1)</script>')
expect(result.ok).toBe(false)
expect(openSpy).not.toHaveBeenCalled()
})
it('returns blocked_protocol error kind for data:', () => {
const result = safeOpenExternal('data:text/html,hello')
expect(result.ok).toBe(false)
if (!result.ok) {
expect(result.error.kind).toBe('blocked_protocol')
expect(result.error.protocol).toBe('data:')
}
})
it('NEGATIVE — blocks vbscript: URI', () => {
const result = safeOpenExternal('vbscript:msgbox(1)')
expect(result.ok).toBe(false)
expect(openSpy).not.toHaveBeenCalled()
})
it('NEGATIVE — blocks blob: URI', () => {
const result = safeOpenExternal('blob:https://example.com/fake')
expect(result.ok).toBe(false)
expect(openSpy).not.toHaveBeenCalled()
})
it('includes the original URL in the error object', () => {
const url = 'javascript:alert(document.cookie)'
const result = safeOpenExternal(url)
expect(result.ok).toBe(false)
if (!result.ok) {
expect(result.error.url).toBe(url)
}
})
})
// -------------------------------------------------------------------------
// Invalid / malformed inputs
// -------------------------------------------------------------------------
describe('invalid or malformed inputs', () => {
it('returns invalid_url for a non-URL string', () => {
const result = safeOpenExternal('not a url at all!!')
expect(result.ok).toBe(false)
if (!result.ok) {
expect(result.error.kind).toBe('invalid_url')
}
expect(openSpy).not.toHaveBeenCalled()
})
it('returns invalid_url for an empty string', () => {
const result = safeOpenExternal('')
expect(result.ok).toBe(false)
if (!result.ok) {
expect(result.error.kind).toBe('invalid_url')
}
expect(openSpy).not.toHaveBeenCalled()
})
it('includes the invalid string in the error object', () => {
const url = 'not-a-url'
const result = safeOpenExternal(url)
expect(result.ok).toBe(false)
if (!result.ok) {
expect(result.error.url).toBe(url)
}
})
})
// -------------------------------------------------------------------------
// Return value
// -------------------------------------------------------------------------
describe('return value', () => {
it('surfaces the WindowProxy handle on success', () => {
const fakeHandle = { focus: vi.fn() } as unknown as WindowProxy
openSpy.mockReturnValue(fakeHandle)
const result = safeOpenExternal('https://example.com')
expect(result.ok).toBe(true)
if (result.ok) {
expect(result.handle).toBe(fakeHandle)
}
})
it('surfaces null handle when window.open returns null (popup blocked)', () => {
openSpy.mockReturnValue(null)
const result = safeOpenExternal('https://example.com')
expect(result.ok).toBe(true)
if (result.ok) {
expect(result.handle).toBeNull()
}
})
})
})