Enhance JWT handling with CSRF token support.

- Introduced `csrfToken` variable to store the CSRF token.
- Modified `fetchJWT` to extract and store CSRF token from the response.
- Updated `refreshJWT` to include `X-CSRF-TOKEN` in headers when present.
- Ensured consistent `Accept` and `Content-Type` headers for fetch requests.
- Added `referrerPolicy: 'no-referrer-when-downgrade'` for improved compatibility.

This improves security by ensuring CSRF protection for token refresh requests.
This commit is contained in:
Gerardo O 2025-03-31 19:06:41 -05:00
parent 7f115f8cfa
commit 6ff6aecb61

View File

@ -56,6 +56,7 @@ window.GoAccess = window.GoAccess || {
}; };
this.AppPrefs = GoAccess.Util.merge(this.AppPrefs, this.opts.prefs); this.AppPrefs = GoAccess.Util.merge(this.AppPrefs, this.opts.prefs);
this.currentJWT = null; this.currentJWT = null;
this.csrfToken = null;
// WebSocket reconnection settings // WebSocket reconnection settings
this.wsDelay = this.currDelay = 1E3; this.wsDelay = this.currDelay = 1E3;
@ -191,16 +192,33 @@ window.GoAccess = window.GoAccess || {
fetchJWT: function (url) { fetchJWT: function (url) {
return fetch(url, { return fetch(url, {
method: 'GET', method: 'GET',
credentials: 'include' credentials: 'include',
}).then(response => response.json()); headers: { 'Accept': 'application/json' },
referrerPolicy: 'no-referrer-when-downgrade'
})
.then(response => response.json())
.then(data => {
if (data.status === 'success' && data.csrf_token) {
this.csrfToken = data.csrf_token;
}
return data;
});
}, },
refreshJWT: function (url, refreshToken) { refreshJWT: function (url, refreshToken) {
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
};
if (this.csrfToken) {
headers['X-CSRF-TOKEN'] = this.csrfToken;
}
return fetch(url, { return fetch(url, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, credentials: 'include',
body: JSON.stringify({ refresh_token: refreshToken }), headers: headers,
credentials: 'include' referrerPolicy: 'no-referrer-when-downgrade',
body: JSON.stringify({ refresh_token: refreshToken })
}).then(response => response.json()); }).then(response => response.json());
}, },