-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaml.controller.ee.test.ts
More file actions
243 lines (194 loc) · 7.66 KB
/
Copy pathsaml.controller.ee.test.ts
File metadata and controls
243 lines (194 loc) · 7.66 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { GLOBAL_OWNER_ROLE, type User } from '@n8n/db';
import { type Response } from 'express';
import { mock } from 'jest-mock-extended';
import type { AuthService } from '@/auth/auth.service';
import type { EventService } from '@/events/event.service';
import type { AuthlessRequest } from '@/requests';
import type { UrlService } from '@/services/url.service';
import { isSamlLicensedAndEnabled } from '@/sso.ee/sso-helpers';
import { isConnectionTestRequest } from '../saml-helpers';
import { SamlController } from '../saml.controller.ee';
import type { SamlService } from '../saml.service.ee';
import { getServiceProviderConfigTestReturnUrl } from '../service-provider.ee';
import type { SamlUserAttributes } from '../types';
// Mock the saml-helpers module
jest.mock('../saml-helpers', () => ({
isConnectionTestRequest: jest.fn(),
}));
jest.mock('@/sso.ee/sso-helpers', () => ({
isSamlLicensedAndEnabled: jest.fn(),
}));
const authService = mock<AuthService>();
const samlService = mock<SamlService>();
const urlService = mock<UrlService>();
const eventService = mock<EventService>();
const controller = new SamlController(authService, samlService, urlService, eventService);
const user = mock<User>({
id: '123',
password: 'password',
authIdentities: [],
role: GLOBAL_OWNER_ROLE,
});
const attributes: SamlUserAttributes = {
email: 'test@example.com',
firstName: 'Test',
lastName: 'User',
userPrincipalName: 'upn:test@example.com',
};
describe('Test views', () => {
const RelayState = getServiceProviderConfigTestReturnUrl();
beforeEach(() => {
// Mock the helper functions for test connection flow
(isConnectionTestRequest as jest.Mock).mockReturnValue(true);
});
test('Should render success with template', async () => {
const req = mock<AuthlessRequest>();
const res = mock<Response>({
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
});
samlService.handleSamlLogin.mockResolvedValueOnce({
authenticatedUser: user,
attributes,
onboardingRequired: false,
});
await controller.acsPost(req, res, { RelayState });
expect(res.render).toBeCalledWith('saml-connection-test-success', attributes);
});
test('Should render failure with template', async () => {
const req = mock<AuthlessRequest>();
const res = mock<Response>({
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
});
samlService.handleSamlLogin.mockResolvedValueOnce({
authenticatedUser: undefined,
attributes,
onboardingRequired: false,
});
await controller.acsPost(req, res, { RelayState });
expect(res.render).toBeCalledWith('saml-connection-test-failed', { message: '', attributes });
});
test('Should render error with template', async () => {
const req = mock<AuthlessRequest>();
const res = mock<Response>({
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
});
samlService.handleSamlLogin.mockRejectedValueOnce(new Error('Test Error'));
await controller.acsPost(req, res, { RelayState });
expect(res.render).toBeCalledWith('saml-connection-test-failed', { message: 'Test Error' });
});
});
describe('SAML Login Flow', () => {
beforeEach(() => {
jest.clearAllMocks();
// Mock the helper functions for actual login flow (not test connections)
(isConnectionTestRequest as jest.Mock).mockReturnValue(false);
(isSamlLicensedAndEnabled as jest.Mock).mockReturnValue(true);
// Mock URL service
urlService.getInstanceBaseUrl.mockReturnValue('http://localhost:5678');
});
test('Should issue cookie with MFA flag set to true on successful SAML login', async () => {
const req = mock<AuthlessRequest>({ browserId: 'test-browser-id' });
const res = mock<Response>();
samlService.handleSamlLogin.mockResolvedValueOnce({
authenticatedUser: user,
attributes,
onboardingRequired: false,
});
await controller.acsPost(req, res, { RelayState: '/' });
// Verify that issueCookie was called with MFA flag set to true
expect(authService.issueCookie).toHaveBeenCalledWith(res, user, true, 'test-browser-id');
expect(eventService.emit).toHaveBeenCalledWith('user-logged-in', {
user,
authenticationMethod: 'saml',
});
expect(res.redirect).toHaveBeenCalledWith('http://localhost:5678/');
});
test('Should issue cookie with MFA flag set to true when onboarding is required', async () => {
const req = mock<AuthlessRequest>({ browserId: 'test-browser-id' });
const res = mock<Response>();
samlService.handleSamlLogin.mockResolvedValueOnce({
authenticatedUser: user,
attributes,
onboardingRequired: true,
});
await controller.acsPost(req, res, { RelayState: '/' });
// Verify that issueCookie was called with MFA flag set to true
expect(authService.issueCookie).toHaveBeenCalledWith(res, user, true, 'test-browser-id');
expect(res.redirect).toHaveBeenCalledWith('http://localhost:5678/saml/onboarding');
});
test('Should respect custom RelayState redirect URL', async () => {
const req = mock<AuthlessRequest>({ browserId: 'test-browser-id' });
const res = mock<Response>();
const customRelayState = '/custom/redirect';
samlService.handleSamlLogin.mockResolvedValueOnce({
authenticatedUser: user,
attributes,
onboardingRequired: false,
});
await controller.acsPost(req, res, { RelayState: customRelayState });
expect(authService.issueCookie).toHaveBeenCalledWith(res, user, true, 'test-browser-id');
expect(res.redirect).toHaveBeenCalledWith('http://localhost:5678/custom/redirect');
});
describe('Redirect URL Validation', () => {
test('allows redirect to relative URls starting with slash', async () => {
const req = mock<AuthlessRequest>({ browserId: 'test-browser-id' });
const res = mock<Response>();
samlService.handleSamlLogin.mockResolvedValueOnce({
authenticatedUser: user,
attributes,
onboardingRequired: false,
});
await controller.acsPost(req, res, { RelayState: '/workflow/123' });
expect(res.redirect).toHaveBeenCalledWith('http://localhost:5678/workflow/123');
});
test('validates redirect URL that is passed in via URL parameter', async () => {
const req = mock<AuthlessRequest>({
query: { redirect: '//evil.com/phishing' },
headers: {},
});
const res = mock<Response>();
samlService.getLoginRequestUrl.mockResolvedValueOnce({
binding: 'redirect',
context: { context: 'http://idp.example.com/login' } as any,
});
await controller.initSsoGet(req, res);
expect(samlService.getLoginRequestUrl).toHaveBeenCalledWith('/', undefined, undefined);
});
test('validates redirect URL that is passed in via referrer header', async () => {
const req = mock<AuthlessRequest>({
query: {},
headers: {
referer: 'http://localhost:5678/login?redirect=%2F%2Fevil.com%2Fphishing',
},
});
const res = mock<Response>();
samlService.getLoginRequestUrl.mockResolvedValueOnce({
binding: 'redirect',
context: { context: 'http://idp.example.com/login' } as any,
});
await controller.initSsoGet(req, res);
expect(samlService.getLoginRequestUrl).toHaveBeenCalledWith('/', undefined, undefined);
});
const hostWithoutRedirect = 'http://localhost:5678/';
test.each([
['https://evil.com/phishing'],
['//evil.com/phishing'],
['javascript:alert(1)'],
['%2F%2Fevil.com/phishing'],
['workflows/123'],
])('does not allow redirect to %s', async (blockedUrl: string) => {
const req = mock<AuthlessRequest>({ browserId: 'test-browser-id' });
const res = mock<Response>();
samlService.handleSamlLogin.mockResolvedValueOnce({
authenticatedUser: user,
attributes,
onboardingRequired: false,
});
await controller.acsPost(req, res, { RelayState: blockedUrl });
expect(res.redirect).toHaveBeenCalledWith(hostWithoutRedirect);
});
});
});