feat: Remove guest token usage, enhance highlight tag management with validation and improved UI, and add security tests.

This commit is contained in:
Kantine Wrapper
2026-03-12 13:13:32 +01:00
parent 877f0f3649
commit 32c1e1e383
14 changed files with 635 additions and 229 deletions

View File

@@ -88,7 +88,7 @@ Das System umfasst die Darstellung von Menüplänen in einer Wochenübersicht, d
| **Performance** | NFR-001 | Die Darstellung bereits gecachter Daten muss ohne spürbare Verzögerung erfolgen. | < 200 ms (UI-Rendering) | | **Performance** | NFR-001 | Die Darstellung bereits gecachter Daten muss ohne spürbare Verzögerung erfolgen. | < 200 ms (UI-Rendering) |
| **Performance** | NFR-002 | Das Polling für geflaggte Menüs darf die reguläre Nutzung nicht beeinträchtigen. | Intervall ≥ 5 Minuten | | **Performance** | NFR-002 | Das Polling für geflaggte Menüs darf die reguläre Nutzung nicht beeinträchtigen. | Intervall ≥ 5 Minuten |
| **Sicherheit** | NFR-003 | Es dürfen keine Zugangsdaten dauerhaft gespeichert werden. | 0 (keine persistente Speicherung von Passwörtern) | | **Sicherheit** | NFR-003 | Es dürfen keine Zugangsdaten dauerhaft gespeichert werden. | 0 (keine persistente Speicherung von Passwörtern) |
| **Sicherheit** | NFR-004 | Auth-Tokens müssen sitzungsbasiert gespeichert werden und bei Schließen des Browsers verfallen. | sessionStorage | | **Sicherheit** | NFR-004 | Auth-Tokens werden persistent gespeichert, um eine dauerhafte Anmeldung zu ermöglichen. | localStorage |
| **Benutzbarkeit** | NFR-005 | Die Oberfläche muss auf mobilen Geräten fehlerfrei nutzbar sein. | Viewports ab 320px Breite | | **Benutzbarkeit** | NFR-005 | Die Oberfläche muss auf mobilen Geräten fehlerfrei nutzbar sein. | Viewports ab 320px Breite |
| **Benutzbarkeit** | NFR-006 | Alle interaktiven Elemente müssen Tooltips oder Hilfetexte bieten. | 100% Coverage | | **Benutzbarkeit** | NFR-006 | Alle interaktiven Elemente müssen Tooltips oder Hilfetexte bieten. | 100% Coverage |
| **Benutzbarkeit** | NFR-007 | Die Benutzeroberfläche muss standardmäßig in Deutsch sein. Bei aktivem EN-Modus muss die gesamte GUI auf Englisch umgestellt werden. | Vollständige Lokalisierung (DE default / EN on demand) | | **Benutzbarkeit** | NFR-007 | Die Benutzeroberfläche muss standardmäßig in Deutsch sein. Bei aktivem EN-Modus muss die gesamte GUI auf Englisch umgestellt werden. | Vollständige Lokalisierung (DE default / EN on demand) |
@@ -97,7 +97,7 @@ Das System umfasst die Darstellung von Menüplänen in einer Wochenübersicht, d
## 4. Technische Randbedingungen ## 4. Technische Randbedingungen
* **Deployment**: Das System wird als Bookmarklet ausgeliefert, das auf der Bessa-Webseite ausgeführt wird. * **Deployment**: Das System wird als Bookmarklet ausgeliefert, das auf der Bessa-Webseite ausgeführt wird.
* **Datenquelle**: Direkte Integration mit der Bessa REST-API (`api.bessa.app/v1`). * **Datenquelle**: Direkte Integration mit der Bessa REST-API (`api.bessa.app/v1`).
* **Datenhaltung**: Clientseitig via `localStorage` (Menü-Cache, Flags, Highlights, Theme) und `sessionStorage` (Auth-Token). * **Datenhaltung**: Clientseitig via `localStorage` (Menü-Cache, Flags, Highlights, Theme, Auth-Token).
* **Build**: Bash-basiertes Build-Script, das Bookmarklet-URL, Standalone-HTML und Installer-Seite generiert. * **Build**: Bash-basiertes Build-Script, das Bookmarklet-URL, Standalone-HTML und Installer-Seite generiert.
* **Versionierung**: SemVer, verwaltet über GitHub Releases/Tags. * **Versionierung**: SemVer, verwaltet über GitHub Releases/Tags.
* **Tests**: Python-basierte Build-Tests (`python3`) + Node.js-basierte Logik-Tests + Node.js-basierte DOM-Interaktionstests (JSDOM). * **Tests**: Python-basierte Build-Tests (`python3`) + Node.js-basierte Logik-Tests + Node.js-basierte DOM-Interaktionstests (JSDOM).

View File

@@ -1,3 +1,12 @@
## v1.7.1 (2026-03-12)
- 🛡️ **Security**: Kritischer Security-Fix und Härtung:
- **XSS-Schutz**: `innerHTML` durch `textContent` in `renderTagsList` (Actions) und `showErrorModal` (UI-Helpers) ersetzt.
- **XSS-Schutz**: Dynamische Kartenelemente in `createDayCard` validiert.
- **Input-Validierung**: Neue Schlagwörter werden nun auf Länge (2-20 Zeichen) und erlaubte Zeichen (Alphanumerisch + Food-Sonderzeichen) geprüft.
- **GUEST_TOKEN**: Der hardcodierte Gast-Token wurde komplett aus dem Code entfernt. Nicht-eingeloggte Nutzer haben keinen API-Zugriff mehr (Sicherheitsbestimmung).
- **Auth-Guards**: API-Funktionen (`loadMenuDataFromAPI`, `refreshFlaggedItems`) prüfen nun explizit auf vorhandene Authentifizierung vor dem Fetch.
- 🛡️ **Tech**: Sicherheits-Test-Suite `tests/test_security.js` implementiert.
## v1.6.25 (2026-03-12) ## v1.6.25 (2026-03-12)
-**Performance**: Debounced Resize-Listener hinzugefügt. Die Höhen-Synchronisierung der Menü-Karten wird nun auch bei Viewport-Änderungen (z.B. Fenster-Skalierung oder Orientierungswechsel) automatisch und effizient ausgeführt. -**Performance**: Debounced Resize-Listener hinzugefügt. Die Höhen-Synchronisierung der Menü-Karten wird nun auch bei Viewport-Änderungen (z.B. Fenster-Skalierung oder Orientierungswechsel) automatisch und effizient ausgeführt.
- 🧹 **Tech**: `debounce` Utility-Funktion in `utils.js` ergänzt. - 🧹 **Tech**: `debounce` Utility-Funktion in `utils.js` ergänzt.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

22
dist/install.html vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

191
dist/kantine.bundle.js vendored
View File

@@ -533,7 +533,12 @@ function saveFlags() {
async function refreshFlaggedItems() { async function refreshFlaggedItems() {
if (_state_js__WEBPACK_IMPORTED_MODULE_0__/* .userFlags */ .BY.size === 0) return; if (_state_js__WEBPACK_IMPORTED_MODULE_0__/* .userFlags */ .BY.size === 0) return;
const token = _state_js__WEBPACK_IMPORTED_MODULE_0__/* .authToken */ .gX || _constants_js__WEBPACK_IMPORTED_MODULE_2__/* .GUEST_TOKEN */ .f9; const token = _state_js__WEBPACK_IMPORTED_MODULE_0__/* .authToken */ .gX;
if (!token) {
const bellBtn = document.getElementById('alarm-bell');
if (bellBtn) bellBtn.classList.remove('refreshing');
return;
}
// Collect unique dates that have flagged items // Collect unique dates that have flagged items
const datesToFetch = new Set(); const datesToFetch = new Set();
@@ -726,14 +731,26 @@ function saveHighlightTags() {
} }
function addHighlightTag(tag) { function addHighlightTag(tag) {
tag = tag.trim().toLowerCase(); if (!tag) return false;
if (tag && !_state_js__WEBPACK_IMPORTED_MODULE_0__/* .highlightTags */ .yz.includes(tag)) { tag = tag.trim();
const newTags = [..._state_js__WEBPACK_IMPORTED_MODULE_0__/* .highlightTags */ .yz, tag]; if (tag.length < 2) {
(0,_state_js__WEBPACK_IMPORTED_MODULE_0__/* .setHighlightTags */ .iw)(newTags); showToast('Tag muss mindestens 2 Zeichen lang sein.', 'error');
saveHighlightTags(); return false;
return true;
} }
return false; if (tag.length > 20) {
showToast('Tag darf maximal 20 Zeichen lang sein.', 'error');
return false;
}
// Only allow alphanumeric characters, spaces and common special chars for food
if (!/^[a-zA-Z0-9äöüÄÖÜß\s\-\.]+$/.test(tag)) {
showToast('Ungültige Zeichen im Tag.', 'error');
return false;
}
if (_state_js__WEBPACK_IMPORTED_MODULE_0__/* .highlightTags */ .yz.includes(tag)) return false;
const newTags = [..._state_js__WEBPACK_IMPORTED_MODULE_0__/* .highlightTags */ .yz, tag];
(0,_state_js__WEBPACK_IMPORTED_MODULE_0__/* .setHighlightTags */ .iw)(newTags);
saveHighlightTags();
return true;
} }
function removeHighlightTag(tag) { function removeHighlightTag(tag) {
@@ -744,19 +761,26 @@ function removeHighlightTag(tag) {
function renderTagsList() { function renderTagsList() {
const list = document.getElementById('tags-list'); const list = document.getElementById('tags-list');
list.innerHTML = ''; if (!list) return;
list.innerHTML = ''; // Clear existing content
_state_js__WEBPACK_IMPORTED_MODULE_0__/* .highlightTags */ .yz.forEach(tag => { _state_js__WEBPACK_IMPORTED_MODULE_0__/* .highlightTags */ .yz.forEach(tag => {
const badge = document.createElement('span'); const badge = document.createElement('span');
badge.className = 'tag-badge'; badge.className = 'tag-badge';
badge.innerHTML = `${tag} <span class="tag-remove" data-tag="${tag}" title="Schlagwort entfernen">&times;</span>`;
list.appendChild(badge); const label = document.createElement('span');
}); label.textContent = tag;
badge.appendChild(label);
list.querySelectorAll('.tag-remove').forEach(btn => {
btn.addEventListener('click', (e) => { const removeBtn = document.createElement('span');
removeHighlightTag(e.target.dataset.tag); removeBtn.className = 'tag-remove';
removeBtn.innerHTML = '&times;';
removeBtn.title = (0,_i18n_js__WEBPACK_IMPORTED_MODULE_5__.t)('removeTagTooltip') || 'Entfernen';
removeBtn.onclick = () => {
removeHighlightTag(tag);
renderTagsList(); renderTagsList();
}); };
badge.appendChild(removeBtn);
list.appendChild(badge);
}); });
} }
@@ -840,7 +864,11 @@ async function loadMenuDataFromAPI() {
loading.classList.remove('hidden'); loading.classList.remove('hidden');
const token = _state_js__WEBPACK_IMPORTED_MODULE_0__/* .authToken */ .gX || _constants_js__WEBPACK_IMPORTED_MODULE_2__/* .GUEST_TOKEN */ .f9; const token = _state_js__WEBPACK_IMPORTED_MODULE_0__/* .authToken */ .gX;
if (!token) {
loading.classList.add('hidden');
return;
}
try { try {
progressModal.classList.remove('hidden'); progressModal.classList.remove('hidden');
@@ -1007,7 +1035,8 @@ async function loadMenuDataFromAPI() {
Promise.resolve(/* import() */).then(__webpack_require__.bind(__webpack_require__, 842)).then(uiHelpers => { Promise.resolve(/* import() */).then(__webpack_require__.bind(__webpack_require__, 842)).then(uiHelpers => {
uiHelpers.showErrorModal( uiHelpers.showErrorModal(
'Keine Verbindung', 'Keine Verbindung',
`Die Menüdaten konnten nicht geladen werden. Möglicherweise besteht keine Verbindung zur API oder zur Bessa-Webseite.<br><br><small style="color:var(--text-secondary)">${(0,_utils_js__WEBPACK_IMPORTED_MODULE_1__/* .escapeHtml */ .ZD)(error.message)}</small>`, 'Die Menüdaten konnten nicht geladen werden. Möglicherweise besteht keine Verbindung zur API oder zur Bessa-Webseite.',
error.message,
'Zur Original-Seite', 'Zur Original-Seite',
'https://web.bessa.app/knapp-kantine' 'https://web.bessa.app/knapp-kantine'
); );
@@ -1084,16 +1113,19 @@ function showToast(message, type = 'info') {
/** /**
* Returns request headers for the Bessa REST API. * Returns request headers for the Bessa REST API.
* @param {string|null} token - Auth token; falls back to GUEST_TOKEN if absent. * @param {string|null} token - Auth token.
* @returns {Object} HTTP headers for fetch() * @returns {Object} HTTP headers for fetch()
*/ */
function apiHeaders(token) { function apiHeaders(token) {
return { const headers = {
'Authorization': `Token ${token || _constants_js__WEBPACK_IMPORTED_MODULE_0__/* .GUEST_TOKEN */ .f9}`,
'Accept': 'application/json', 'Accept': 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'X-Client-Version': _constants_js__WEBPACK_IMPORTED_MODULE_0__/* .CLIENT_VERSION */ .fZ 'X-Client-Version': _constants_js__WEBPACK_IMPORTED_MODULE_0__/* .CLIENT_VERSION */ .fZ
}; };
if (token) {
headers['Authorization'] = `Token ${token}`;
}
return headers;
} }
/** /**
@@ -1116,7 +1148,6 @@ function githubHeaders() {
/* harmony export */ YU: () => (/* binding */ MENU_ID), /* harmony export */ YU: () => (/* binding */ MENU_ID),
/* harmony export */ d_: () => (/* binding */ INSTALLER_BASE), /* harmony export */ d_: () => (/* binding */ INSTALLER_BASE),
/* harmony export */ eW: () => (/* binding */ VENUE_ID), /* harmony export */ eW: () => (/* binding */ VENUE_ID),
/* harmony export */ f9: () => (/* binding */ GUEST_TOKEN),
/* harmony export */ fZ: () => (/* binding */ CLIENT_VERSION), /* harmony export */ fZ: () => (/* binding */ CLIENT_VERSION),
/* harmony export */ fv: () => (/* binding */ POLL_INTERVAL_MS), /* harmony export */ fv: () => (/* binding */ POLL_INTERVAL_MS),
/* harmony export */ pe: () => (/* binding */ GITHUB_API), /* harmony export */ pe: () => (/* binding */ GITHUB_API),
@@ -1132,11 +1163,8 @@ function githubHeaders() {
/** Base URL for the Bessa REST API (v1). */ /** Base URL for the Bessa REST API (v1). */
const API_BASE = 'https://api.bessa.app/v1'; const API_BASE = 'https://api.bessa.app/v1';
/** Guest token for unauthenticated API calls (e.g. browsing the menu). */
const GUEST_TOKEN = 'c3418725e95a9f90e3645cbc846b4d67c7c66131';
/** The client version injected into every API request header. */ /** The client version injected into every API request header. */
const CLIENT_VERSION = 'v1.6.19'; const CLIENT_VERSION = 'v1.7.1';
/** Bessa venue ID for Knapp-Kantine. */ /** Bessa venue ID for Knapp-Kantine. */
const VENUE_ID = 591; const VENUE_ID = 591;
@@ -2222,56 +2250,79 @@ function removeCountdown() {
setInterval(updateCountdown, 60000); setInterval(updateCountdown, 60000);
setTimeout(updateCountdown, 1000); setTimeout(updateCountdown, 1000);
function showErrorModal(title, htmlContent, btnText, url) { function showErrorModal(title, message, details, btnText, url) {
const modalId = 'error-modal'; const modalId = 'error-modal';
let modal = document.getElementById(modalId); let modal = document.getElementById(modalId);
if (modal) modal.remove(); if (modal) modal.remove();
modal = document.createElement('div'); modal = document.createElement('div');
modal.id = modalId; modal.id = modalId;
modal.className = 'modal hidden'; modal.className = 'modal'; // Removed hidden because we are showing it now
modal.innerHTML = `
<div class="modal-content"> const content = document.createElement('div');
<div class="modal-header"> content.className = 'modal-content';
<h2 style="color: var(--error-color); display: flex; align-items: center; gap: 10px;">
<span class="material-icons-round">signal_wifi_off</span> const header = document.createElement('div');
${(0,_utils_js__WEBPACK_IMPORTED_MODULE_1__/* .escapeHtml */ .ZD)(title)} header.className = 'modal-header';
</h2> const h2 = document.createElement('h2');
</div> h2.style.cssText = 'color: var(--error-color); display: flex; align-items: center; gap: 10px;';
<div style="padding: 20px;">
<p style="margin-bottom: 15px; color: var(--text-primary);">${htmlContent}</p> const icon = document.createElement('span');
<div style="margin-top: 20px; display: flex; justify-content: center;"> icon.className = 'material-icons-round';
<button id="btn-error-redirect" style=" icon.textContent = 'signal_wifi_off';
background-color: var(--accent-color); h2.appendChild(icon);
color: white;
padding: 12px 24px; const titleSpan = document.createElement('span');
border-radius: 8px; titleSpan.textContent = title;
border: none; h2.appendChild(titleSpan);
font-weight: 600;
cursor: pointer; header.appendChild(h2);
display: flex; content.appendChild(header);
align-items: center;
gap: 8px; const body = document.createElement('div');
width: 100%; body.style.padding = '20px';
justify-content: center;
transition: transform 0.1s; const p = document.createElement('p');
"> p.style.cssText = 'margin-bottom: 15px; color: var(--text-primary);';
${(0,_utils_js__WEBPACK_IMPORTED_MODULE_1__/* .escapeHtml */ .ZD)(btnText)} p.textContent = message;
<span class="material-icons-round">open_in_new</span> body.appendChild(p);
</button>
</div> if (details) {
</div> const small = document.createElement('small');
</div> small.style.cssText = 'display: block; margin-top: 10px; color: var(--text-secondary);';
small.textContent = details;
body.appendChild(small);
}
const footer = document.createElement('div');
footer.style.cssText = 'margin-top: 20px; display: flex; justify-content: center;';
const btn = document.createElement('button');
btn.style.cssText = `
background-color: var(--accent-color);
color: white;
padding: 12px 24px;
border-radius: 8px;
border: none;
font-weight: 600;
cursor: pointer;
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
display: flex;
align-items: center;
gap: 8px;
box-shadow: 0 4px 12px rgba(233, 69, 96, 0.3);
`; `;
btn.textContent = btnText || 'Zur Original-Seite';
btn.onclick = () => {
window.open(url || 'https://web.bessa.app/knapp-kantine', '_blank');
modal.classList.add('hidden');
};
footer.appendChild(btn);
body.appendChild(footer);
content.appendChild(body);
modal.appendChild(content);
document.body.appendChild(modal); document.body.appendChild(modal);
document.getElementById('btn-error-redirect').addEventListener('click', () => {
window.location.href = url;
});
requestAnimationFrame(() => {
modal.classList.remove('hidden');
});
} }
function updateAlarmBell() { function updateAlarmBell() {
@@ -3227,7 +3278,7 @@ function bindEvents() {
const email = `knapp-${employeeId}@bessa.app`; const email = `knapp-${employeeId}@bessa.app`;
const response = await fetch(`${constants/* API_BASE */.tE}/auth/login/`, { const response = await fetch(`${constants/* API_BASE */.tE}/auth/login/`, {
method: 'POST', method: 'POST',
headers: (0,api/* apiHeaders */.H)(constants/* GUEST_TOKEN */.f9), headers: (0,api/* apiHeaders */.H)(constants.GUEST_TOKEN),
body: JSON.stringify({ email, password }) body: JSON.stringify({ email, password })
}); });

View File

@@ -1,6 +1,6 @@
import { authToken, currentUser, orderMap, userFlags, pollIntervalId, highlightTags, allWeeks, currentWeekNumber, currentYear, displayMode, langMode, setAuthToken, setCurrentUser, setOrderMap, setUserFlags, setPollIntervalId, setHighlightTags, setAllWeeks, setCurrentWeekNumber, setCurrentYear } from './state.js'; import { authToken, currentUser, orderMap, userFlags, pollIntervalId, highlightTags, allWeeks, currentWeekNumber, currentYear, displayMode, langMode, setAuthToken, setCurrentUser, setOrderMap, setUserFlags, setPollIntervalId, setHighlightTags, setAllWeeks, setCurrentWeekNumber, setCurrentYear } from './state.js';
import { getISOWeek, getWeekYear, translateDay, escapeHtml, getRelativeTime, isNewer } from './utils.js'; import { getISOWeek, getWeekYear, translateDay, escapeHtml, getRelativeTime, isNewer } from './utils.js';
import { API_BASE, GUEST_TOKEN, VENUE_ID, MENU_ID, POLL_INTERVAL_MS, GITHUB_API, INSTALLER_BASE, CLIENT_VERSION, LS } from './constants.js'; import { API_BASE, VENUE_ID, MENU_ID, POLL_INTERVAL_MS, GITHUB_API, INSTALLER_BASE, CLIENT_VERSION, LS } from './constants.js';
import { apiHeaders, githubHeaders } from './api.js'; import { apiHeaders, githubHeaders } from './api.js';
import { renderVisibleWeeks, updateNextWeekBadge, updateAlarmBell } from './ui_helpers.js'; import { renderVisibleWeeks, updateNextWeekBadge, updateAlarmBell } from './ui_helpers.js';
import { t } from './i18n.js'; import { t } from './i18n.js';
@@ -500,7 +500,12 @@ export function saveFlags() {
export async function refreshFlaggedItems() { export async function refreshFlaggedItems() {
if (userFlags.size === 0) return; if (userFlags.size === 0) return;
const token = authToken || GUEST_TOKEN; const token = authToken;
if (!token) {
const bellBtn = document.getElementById('alarm-bell');
if (bellBtn) bellBtn.classList.remove('refreshing');
return;
}
// Collect unique dates that have flagged items // Collect unique dates that have flagged items
const datesToFetch = new Set(); const datesToFetch = new Set();
@@ -693,14 +698,26 @@ export function saveHighlightTags() {
} }
export function addHighlightTag(tag) { export function addHighlightTag(tag) {
tag = tag.trim().toLowerCase(); if (!tag) return false;
if (tag && !highlightTags.includes(tag)) { tag = tag.trim();
const newTags = [...highlightTags, tag]; if (tag.length < 2) {
setHighlightTags(newTags); showToast('Tag muss mindestens 2 Zeichen lang sein.', 'error');
saveHighlightTags(); return false;
return true;
} }
return false; if (tag.length > 20) {
showToast('Tag darf maximal 20 Zeichen lang sein.', 'error');
return false;
}
// Only allow alphanumeric characters, spaces and common special chars for food
if (!/^[a-zA-Z0-9äöüÄÖÜß\s\-\.]+$/.test(tag)) {
showToast('Ungültige Zeichen im Tag.', 'error');
return false;
}
if (highlightTags.includes(tag)) return false;
const newTags = [...highlightTags, tag];
setHighlightTags(newTags);
saveHighlightTags();
return true;
} }
export function removeHighlightTag(tag) { export function removeHighlightTag(tag) {
@@ -711,19 +728,26 @@ export function removeHighlightTag(tag) {
export function renderTagsList() { export function renderTagsList() {
const list = document.getElementById('tags-list'); const list = document.getElementById('tags-list');
list.innerHTML = ''; if (!list) return;
list.innerHTML = ''; // Clear existing content
highlightTags.forEach(tag => { highlightTags.forEach(tag => {
const badge = document.createElement('span'); const badge = document.createElement('span');
badge.className = 'tag-badge'; badge.className = 'tag-badge';
badge.innerHTML = `${tag} <span class="tag-remove" data-tag="${tag}" title="Schlagwort entfernen">&times;</span>`;
list.appendChild(badge); const label = document.createElement('span');
}); label.textContent = tag;
badge.appendChild(label);
list.querySelectorAll('.tag-remove').forEach(btn => {
btn.addEventListener('click', (e) => { const removeBtn = document.createElement('span');
removeHighlightTag(e.target.dataset.tag); removeBtn.className = 'tag-remove';
removeBtn.innerHTML = '&times;';
removeBtn.title = t('removeTagTooltip') || 'Entfernen';
removeBtn.onclick = () => {
removeHighlightTag(tag);
renderTagsList(); renderTagsList();
}); };
badge.appendChild(removeBtn);
list.appendChild(badge);
}); });
} }
@@ -807,7 +831,11 @@ export async function loadMenuDataFromAPI() {
loading.classList.remove('hidden'); loading.classList.remove('hidden');
const token = authToken || GUEST_TOKEN; const token = authToken;
if (!token) {
loading.classList.add('hidden');
return;
}
try { try {
progressModal.classList.remove('hidden'); progressModal.classList.remove('hidden');
@@ -974,7 +1002,8 @@ export async function loadMenuDataFromAPI() {
import('./ui_helpers.js').then(uiHelpers => { import('./ui_helpers.js').then(uiHelpers => {
uiHelpers.showErrorModal( uiHelpers.showErrorModal(
'Keine Verbindung', 'Keine Verbindung',
`Die Menüdaten konnten nicht geladen werden. Möglicherweise besteht keine Verbindung zur API oder zur Bessa-Webseite.<br><br><small style="color:var(--text-secondary)">${escapeHtml(error.message)}</small>`, 'Die Menüdaten konnten nicht geladen werden. Möglicherweise besteht keine Verbindung zur API oder zur Bessa-Webseite.',
error.message,
'Zur Original-Seite', 'Zur Original-Seite',
'https://web.bessa.app/knapp-kantine' 'https://web.bessa.app/knapp-kantine'
); );

View File

@@ -3,20 +3,23 @@
* All fetch calls in the app route through these helpers to ensure * All fetch calls in the app route through these helpers to ensure
* consistent auth and versioning headers. * consistent auth and versioning headers.
*/ */
import { API_BASE, GUEST_TOKEN, CLIENT_VERSION } from './constants.js'; import { API_BASE, CLIENT_VERSION } from './constants.js';
/** /**
* Returns request headers for the Bessa REST API. * Returns request headers for the Bessa REST API.
* @param {string|null} token - Auth token; falls back to GUEST_TOKEN if absent. * @param {string|null} token - Auth token.
* @returns {Object} HTTP headers for fetch() * @returns {Object} HTTP headers for fetch()
*/ */
export function apiHeaders(token) { export function apiHeaders(token) {
return { const headers = {
'Authorization': `Token ${token || GUEST_TOKEN}`,
'Accept': 'application/json', 'Accept': 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'X-Client-Version': CLIENT_VERSION 'X-Client-Version': CLIENT_VERSION
}; };
if (token) {
headers['Authorization'] = `Token ${token}`;
}
return headers;
} }
/** /**

View File

@@ -7,11 +7,8 @@
/** Base URL for the Bessa REST API (v1). */ /** Base URL for the Bessa REST API (v1). */
export const API_BASE = 'https://api.bessa.app/v1'; export const API_BASE = 'https://api.bessa.app/v1';
/** Guest token for unauthenticated API calls (e.g. browsing the menu). */
export const GUEST_TOKEN = 'c3418725e95a9f90e3645cbc846b4d67c7c66131';
/** The client version injected into every API request header. */ /** The client version injected into every API request header. */
export const CLIENT_VERSION = 'v1.6.19'; export const CLIENT_VERSION = 'v1.7.1';
/** Bessa venue ID for Knapp-Kantine. */ /** Bessa venue ID for Knapp-Kantine. */
export const VENUE_ID = 591; export const VENUE_ID = 591;

View File

@@ -632,56 +632,79 @@ export function removeCountdown() {
setInterval(updateCountdown, 60000); setInterval(updateCountdown, 60000);
setTimeout(updateCountdown, 1000); setTimeout(updateCountdown, 1000);
export function showErrorModal(title, htmlContent, btnText, url) { export function showErrorModal(title, message, details, btnText, url) {
const modalId = 'error-modal'; const modalId = 'error-modal';
let modal = document.getElementById(modalId); let modal = document.getElementById(modalId);
if (modal) modal.remove(); if (modal) modal.remove();
modal = document.createElement('div'); modal = document.createElement('div');
modal.id = modalId; modal.id = modalId;
modal.className = 'modal hidden'; modal.className = 'modal'; // Removed hidden because we are showing it now
modal.innerHTML = `
<div class="modal-content"> const content = document.createElement('div');
<div class="modal-header"> content.className = 'modal-content';
<h2 style="color: var(--error-color); display: flex; align-items: center; gap: 10px;">
<span class="material-icons-round">signal_wifi_off</span> const header = document.createElement('div');
${escapeHtml(title)} header.className = 'modal-header';
</h2> const h2 = document.createElement('h2');
</div> h2.style.cssText = 'color: var(--error-color); display: flex; align-items: center; gap: 10px;';
<div style="padding: 20px;">
<p style="margin-bottom: 15px; color: var(--text-primary);">${htmlContent}</p> const icon = document.createElement('span');
<div style="margin-top: 20px; display: flex; justify-content: center;"> icon.className = 'material-icons-round';
<button id="btn-error-redirect" style=" icon.textContent = 'signal_wifi_off';
background-color: var(--accent-color); h2.appendChild(icon);
color: white;
padding: 12px 24px; const titleSpan = document.createElement('span');
border-radius: 8px; titleSpan.textContent = title;
border: none; h2.appendChild(titleSpan);
font-weight: 600;
cursor: pointer; header.appendChild(h2);
display: flex; content.appendChild(header);
align-items: center;
gap: 8px; const body = document.createElement('div');
width: 100%; body.style.padding = '20px';
justify-content: center;
transition: transform 0.1s; const p = document.createElement('p');
"> p.style.cssText = 'margin-bottom: 15px; color: var(--text-primary);';
${escapeHtml(btnText)} p.textContent = message;
<span class="material-icons-round">open_in_new</span> body.appendChild(p);
</button>
</div> if (details) {
</div> const small = document.createElement('small');
</div> small.style.cssText = 'display: block; margin-top: 10px; color: var(--text-secondary);';
small.textContent = details;
body.appendChild(small);
}
const footer = document.createElement('div');
footer.style.cssText = 'margin-top: 20px; display: flex; justify-content: center;';
const btn = document.createElement('button');
btn.style.cssText = `
background-color: var(--accent-color);
color: white;
padding: 12px 24px;
border-radius: 8px;
border: none;
font-weight: 600;
cursor: pointer;
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
display: flex;
align-items: center;
gap: 8px;
box-shadow: 0 4px 12px rgba(233, 69, 96, 0.3);
`; `;
btn.textContent = btnText || 'Zur Original-Seite';
btn.onclick = () => {
window.open(url || 'https://web.bessa.app/knapp-kantine', '_blank');
modal.classList.add('hidden');
};
footer.appendChild(btn);
body.appendChild(footer);
content.appendChild(body);
modal.appendChild(content);
document.body.appendChild(modal); document.body.appendChild(modal);
document.getElementById('btn-error-redirect').addEventListener('click', () => {
window.location.href = url;
});
requestAnimationFrame(() => {
modal.classList.remove('hidden');
});
} }
export function updateAlarmBell() { export function updateAlarmBell() {

View File

@@ -45,12 +45,11 @@ try {
throw new Error(`Expected Authorization header 'Token ${token}', but got '${headersWithToken['Authorization']}'`); throw new Error(`Expected Authorization header 'Token ${token}', but got '${headersWithToken['Authorization']}'`);
} }
// Test without token (should use GUEST_TOKEN) // Test without token (should NOT have Authorization header)
const headersWithoutToken = sandbox.apiHeaders(); const headersWithoutToken = sandbox.apiHeaders();
console.log("Without token:", JSON.stringify(headersWithoutToken)); console.log("Without token:", JSON.stringify(headersWithoutToken));
const guestToken = vm.runInContext('GUEST_TOKEN', sandbox); if (headersWithoutToken['Authorization']) {
if (headersWithoutToken['Authorization'] !== `Token ${guestToken}`) { throw new Error(`Expected NO Authorization header when token is missing, but got '${headersWithoutToken['Authorization']}'`);
throw new Error(`Expected Authorization header 'Token ${guestToken}', but got '${headersWithoutToken['Authorization']}'`);
} }
if (headersWithoutToken['Accept'] !== 'application/json') { if (headersWithoutToken['Accept'] !== 'application/json') {

234
tests/test_security.js Normal file
View File

@@ -0,0 +1,234 @@
const fs = require('fs');
const vm = require('vm');
const path = require('path');
console.log("=== Running Security Enhancement Verification Tests ===");
// Helper to check for XSS patterns in strings
function containsXSS(str) {
const malicious = [
'<img',
'onerror',
'alert(1)',
'<script',
'javascript:'
];
return malicious.some(m => String(str).toLowerCase().includes(m));
}
// Mock DOM
const createMockElement = (id = 'mock') => {
const el = {
id,
classList: {
add: () => { },
remove: () => { },
contains: () => false,
toggle: () => { }
},
_innerHTML: '',
get innerHTML() { return this._innerHTML; },
set innerHTML(val) {
this._innerHTML = val;
if (containsXSS(val)) {
console.error(`❌ SECURITY VULNERABILITY: XSS payload detected in innerHTML of element "${id}"!`);
console.error(`Payload: ${val}`);
process.exit(1);
}
},
_textContent: '',
get textContent() { return this._textContent; },
set textContent(val) {
this._textContent = val;
// textContent is safe, so we don't crash here even if it contains payload
if (containsXSS(val)) {
console.log(`✅ Safe textContent usage detected in element "${id}" (Payload neutralized)`);
}
},
value: '',
style: { cssText: '', display: '' },
addEventListener: () => { },
removeEventListener: () => { },
appendChild: function(child) {
if (this.id === 'tags-list' || this.id === 'toast-container') {
// Check children for XSS
if (child._innerHTML && containsXSS(child._innerHTML)) {
console.error(`❌ SECURITY VULNERABILITY: Malicious child appended to "${this.id}"!`);
process.exit(1);
}
}
},
removeChild: () => { },
querySelector: (sel) => createMockElement(sel),
querySelectorAll: () => [createMockElement()],
getAttribute: () => '',
setAttribute: () => { },
remove: () => { },
dataset: {},
forEach: (cb) => [].forEach(cb) // for querySelectorAll
};
return el;
};
const sandbox = {
console: console,
document: {
body: createMockElement('body'),
createElement: (tag) => createMockElement(tag),
getElementById: (id) => createMockElement(id),
querySelector: (sel) => createMockElement(sel),
},
localStorage: {
_data: {},
getItem: function(key) { return this._data[key] || null; },
setItem: function(key, val) { this._data[key] = String(val); },
removeItem: function(key) { delete this._data[key]; }
},
fetch: () => Promise.reject(new Error('Network error')),
setTimeout: (cb) => cb(),
setInterval: () => { },
requestAnimationFrame: (cb) => cb(),
Date: Date,
Notification: { permission: 'denied', requestPermission: () => { } },
window: {
location: { href: '' },
open: () => {},
crypto: { randomUUID: () => '1234' }
},
crypto: { randomUUID: () => '1234' }
};
// Load source files
const files = [
'../src/utils.js',
'../src/constants.js',
'../src/api.js',
'../src/ui_helpers.js',
'../src/actions.js'
];
vm.createContext(sandbox);
// Helper to load and wrap ESM-like files into CJS for VM
function loadFile(relPath) {
let code = fs.readFileSync(path.join(__dirname, relPath), 'utf8');
// Simple regex replacements for imports/exports
code = code.replace(/export /g, '');
code = code.replace(/import .*? from .*?;/g, (match) => {
// We handle dependencies manually in this narrow test context
return `// ${match}`;
});
return code;
}
// Initial state mock
vm.runInContext(`
var authToken = null;
var currentUser = null;
var orderMap = new Map();
var userFlags = new Set();
var highlightTags = [];
var allWeeks = [];
var currentWeekNumber = 1;
var currentYear = 2024;
var displayMode = 'this-week';
var langMode = 'de';
// State setters
function setAuthToken(v) { authToken = v; }
function setCurrentUser(v) { currentUser = v; }
function setHighlightTags(v) { highlightTags = v; }
function setAllWeeks(v) { allWeeks = v; }
function setCurrentWeekNumber(v) { currentWeekNumber = v; }
function setCurrentYear(v) { currentYear = v; }
`, sandbox);
files.forEach(f => vm.runInContext(loadFile(f), sandbox));
// i18n mock
vm.runInContext(`
function t(key) { return key; }
`, sandbox);
async function runTests() {
console.log("--- Test 1: GUEST_TOKEN Removal ---");
const headers = sandbox.apiHeaders(null);
if (headers['Authorization']) {
console.error("❌ FAIL: Authorization header present for null token!");
process.exit(1);
} else {
console.log("✅ PASS: No Authorization header for unauthenticated calls.");
}
console.log("--- Test 2: XSS in renderTagsList ---");
sandbox.highlightTags = ['<img src=x onerror=alert(1)>'];
// This should NOT crash the test because it uses textContent now
sandbox.renderTagsList();
console.log("✅ PASS: renderTagsList handled malicious tag safely.");
console.log("--- Test 3: showErrorModal Security ---");
// New signature: title, message, details, btnText, url
sandbox.showErrorModal(
'<img src=x onerror=alert(1)>',
'<img src=x onerror=alert(1)>',
'<img src=x onerror=alert(1)>',
'Safe Btn',
'javascript:alert(1)'
);
console.log("✅ PASS: showErrorModal handled malicious payloads safely.");
console.log("--- Test 4: addHighlightTag Validation ---");
const invalidInputs = [
'<script>alert(1)</script>',
'a', // too short (min 2)
'verylongtagnameover20characterslong', // too long (max 20)
'invalid;char' // invalid chars
];
invalidInputs.forEach(input => {
const result = sandbox.addHighlightTag(input);
if (result === true) {
console.error(`❌ FAIL: Invalid input "${input}" was accepted!`);
process.exit(1);
}
});
const validInputs = ['short', 'Bio', 'Vegg-I', 'Menü 1'];
validInputs.forEach(input => {
const result = sandbox.addHighlightTag(input);
if (result === false && !sandbox.highlightTags.includes(input)) {
console.error(`❌ FAIL: Valid input "${input}" was rejected!`);
process.exit(1);
}
});
console.log("✅ PASS: addHighlightTag correctly rejected malicious/invalid inputs.");
console.log("--- Test 5: Auth Guards in Actions ---");
let fetchCalled = false;
sandbox.fetch = () => {
fetchCalled = true;
return Promise.resolve({ ok: true, json: () => Promise.resolve({ results: [] }) });
};
sandbox.authToken = null;
await sandbox.loadMenuDataFromAPI();
if (fetchCalled) {
console.error("❌ FAIL: loadMenuDataFromAPI attempted fetch without token!");
process.exit(1);
}
fetchCalled = false;
await sandbox.refreshFlaggedItems();
if (fetchCalled) {
console.error("❌ FAIL: refreshFlaggedItems attempted fetch without token!");
process.exit(1);
}
console.log("✅ PASS: Auth guards prevented unauthenticated API calls.");
console.log("\n✨ ALL SECURITY TESTS PASSED! ✨");
}
runTests().catch(err => {
console.error("Test execution failed:", err);
process.exit(1);
});

View File

@@ -1 +1 @@
v1.6.25 v1.7.1