Skip to content

Commit 1f79b47

Browse files
authored
Fix client set-cookie extraction (#391)
1 parent 47b97a0 commit 1f79b47

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/i18n/en/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const en = {
5454
'Configuration for instance {instance: string} has changed. Disconnect from all locations to apply changes.',
5555
deadConDropped:
5656
'Detected that the {con_type: string} {interface_name: string} has disconnected, trying to reconnect...',
57+
noCookie: 'No defguard_proxy set-cookie received',
5758
},
5859
},
5960
components: {

src/i18n/i18n-types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ type RootTranslation = {
165165
* @param {string} interface_name
166166
*/
167167
deadConDropped: RequiredParams<'con_type' | 'interface_name'>
168+
/**
169+
* N​o​ ​d​e​f​g​u​a​r​d​_​p​r​o​x​y​ ​s​e​t​-​c​o​o​k​i​e​ ​r​e​c​e​i​v​e​d
170+
*/
171+
noCookie: string
168172
}
169173
}
170174
components: {
@@ -1762,6 +1766,10 @@ export type TranslationFunctions = {
17621766
* Detected that the {con_type} {interface_name} has disconnected, trying to reconnect...
17631767
*/
17641768
deadConDropped: (arg: { con_type: string, interface_name: string }) => LocalizedString
1769+
/**
1770+
* No defguard_proxy set-cookie received
1771+
*/
1772+
noCookie: () => LocalizedString
17651773
}
17661774
}
17671775
components: {

src/pages/client/pages/ClientAddInstancePage/components/AddInstanceFormCard/components/AddInstanceInitForm/AddInstanceInitForm.tsx

+18-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ export const AddInstanceInitForm = ({ nextStep }: Props) => {
102102
body: Body.json(data),
103103
})
104104
.then(async (res: Response<EnrollmentStartResponse | EnrollmentError>) => {
105-
const authCookie = res.headers['set-cookie'];
106105
if (!res.ok) {
107106
setIsLoading(false);
108107
error(JSON.stringify(res.data));
@@ -122,6 +121,24 @@ export const AddInstanceInitForm = ({ nextStep }: Props) => {
122121
}
123122
}
124123
}
124+
// There may be other set-cookies, set by e.g. a proxy
125+
// Get only the defguard_proxy cookie
126+
const authCookie = res.rawHeaders['set-cookie'].find((cookie) =>
127+
cookie.startsWith('defguard_proxy='),
128+
);
129+
if (!authCookie) {
130+
setIsLoading(false);
131+
error(
132+
LL.common.messages.errorWithMessage({
133+
message: LL.common.messages.noCookie(),
134+
}),
135+
);
136+
throw Error(
137+
LL.common.messages.errorWithMessage({
138+
message: LL.common.messages.noCookie(),
139+
}),
140+
);
141+
}
125142
debug('Response received with status OK');
126143
const r = res.data as EnrollmentStartResponse;
127144
// get client registered instances

src/pages/client/pages/ClientInstancePage/modals/UpdateInstanceModal/components/UpdateInstanceModalForm.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,17 @@ export const UpdateInstanceModalForm = () => {
108108
proxy_api_url = proxy_api_url + '/api/v1';
109109
const instance = clientInstances.find((i) => i.uuid === enrollmentData.instance.id);
110110
if (instance) {
111-
const authCookie = res.headers['set-cookie'];
111+
const authCookie = res.rawHeaders['set-cookie'].find((cookie) =>
112+
cookie.startsWith('defguard_proxy='),
113+
);
114+
if (!authCookie) {
115+
toaster.error(
116+
LL.common.messages.errorWithMessage({
117+
message: LL.common.messages.noCookie(),
118+
}),
119+
);
120+
return;
121+
}
112122
headers['Cookie'] = authCookie;
113123
const instanceInfoResponse = await fetch<CreateDeviceResponse>(
114124
`${proxy_api_url}/enrollment/network_info`,

0 commit comments

Comments
 (0)