Musik: abgelaufenes CSRF-Token als JSON melden ("bitte noch einmal klicken") statt Klartext, Token kommt mit jedem Zustand mit; Nicht-JSON-Antworten verstaendlich statt "Unexpected token"
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
c86c4620a2
commit
293aa3cb75
2 changed files with 20 additions and 3 deletions
|
|
@ -73,7 +73,16 @@
|
|||
return fetch('/musik_api.php', {
|
||||
method: 'POST', body: body, credentials: 'same-origin',
|
||||
headers: { 'X-Requested-With': 'fetch', 'Accept': 'application/json' }
|
||||
}).then(function (response) { return response.json(); });
|
||||
}).then(parseJson);
|
||||
};
|
||||
// Antwort lesen - kommt kein JSON (Fehlerseite, abgelaufene Sitzung), eine verstaendliche Meldung statt "Unexpected token"
|
||||
var parseJson = function (response) {
|
||||
return response.text().then(function (text) {
|
||||
try { return JSON.parse(text); } catch (e) {
|
||||
if (response.status === 401 || /anmeld/i.test(text)) return { ok: false, error: 'Deine Anmeldung ist abgelaufen – bitte die Seite neu laden.' };
|
||||
return { ok: false, error: 'Unerwartete Antwort vom Server (' + response.status + ') – bitte die Seite neu laden.' };
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var act = function (fields, file) {
|
||||
|
|
@ -82,6 +91,7 @@
|
|||
var forBot = bot;
|
||||
return request(fields, file)
|
||||
.then(function (result) {
|
||||
if (result.csrf) csrf = result.csrf;
|
||||
if (!result.ok) throw new Error(result.error || 'Hat nicht geklappt.');
|
||||
if (result.music && forBot === bot) render(result.music);
|
||||
toast(result.message);
|
||||
|
|
@ -107,8 +117,9 @@
|
|||
if (document.hidden || busy) return;
|
||||
var forBot = bot;
|
||||
fetch('/musik_api.php?action=state&bot=' + encodeURIComponent(bot), { credentials: 'same-origin', headers: { 'Accept': 'application/json' } })
|
||||
.then(function (response) { return response.json(); })
|
||||
.then(parseJson)
|
||||
.then(function (result) {
|
||||
if (result.csrf) csrf = result.csrf; // Token der aktuellen Sitzung (falls sie zwischendurch erneuert wurde)
|
||||
if (!result.ok) throw new Error(result.error || 'Bot nicht erreichbar');
|
||||
if (forBot === bot) render(result.music);
|
||||
})
|
||||
|
|
|
|||
|
|
@ -35,6 +35,12 @@ if (!user_has_permission('music.control')) {
|
|||
|
||||
$action = (string) ($_POST['action'] ?? $_GET['action'] ?? 'state');
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// Passt das Token nicht (Sitzung zwischendurch erneuert, Seite lange offen): als JSON melden, nicht als Klartext -
|
||||
// das Skript holt sich mit dem naechsten Zustand das aktuelle Token (siehe 'csrf' unten) und bittet um einen neuen Versuch
|
||||
if (!hash_equals($_SESSION['csrf_token'] ?? '', (string) ($_POST['csrf_token'] ?? ''))) {
|
||||
http_response_code(400);
|
||||
$reply(false, 'Die Seite war zu lange offen – bitte noch einmal klicken.', ['stale' => true, 'csrf' => csrf_token()]);
|
||||
}
|
||||
verify_csrf();
|
||||
} elseif ($action !== 'state') {
|
||||
http_response_code(405);
|
||||
|
|
@ -52,7 +58,7 @@ $guild = '/music/' . events_guild_id();
|
|||
try {
|
||||
switch ($action) {
|
||||
case 'state':
|
||||
$reply(true, '', ['music' => music_state($bot), 'bot' => $bot]);
|
||||
$reply(true, '', ['music' => music_state($bot), 'bot' => $bot, 'csrf' => csrf_token()]);
|
||||
|
||||
case 'join':
|
||||
$channel = (string) ($_POST['channel_id'] ?? '');
|
||||
|
|
|
|||
Loading…
Reference in a new issue