diff --git a/server/src/groups.ts b/server/src/groups.ts index 77536a9..a2e4fce 100644 --- a/server/src/groups.ts +++ b/server/src/groups.ts @@ -43,6 +43,7 @@ function isChefieGroup(group: OrderGroup, stores: Store[] = []): boolean { /** * Ohlásí členům skupiny z Pizza Chefie, že je pizza objednaná (dřívější notifikace Pizza day). + * Posílá se jednou za skupinu (viz `pizzaOrderedNotified`), aby přepínání stavů nespamovalo. * Bez čekání na výsledek — selhání notifikace nesmí shodit akci. */ function notifyPizzaOrdered(group: OrderGroup): void { @@ -225,10 +226,11 @@ export async function setGroupState(login: string, groupId: string, newState: Gr } } group.state = newState; + // Příznak se ukládá spolu se stavem a návrat do „uzamčeno“ ho nemaže — notifikace jde jen jednou + const notifyOrdered = newState === GroupState.ORDERED && !group.pizzaOrderedNotified && isChefieGroup(group, data.stores); + if (notifyOrdered) group.pizzaOrderedNotified = true; const saved = await saveExtraData(data, date); - if (newState === GroupState.ORDERED && isChefieGroup(group, data.stores)) { - notifyPizzaOrdered(group); - } + if (notifyOrdered) notifyPizzaOrdered(group); return saved; } diff --git a/server/src/notifikace.ts b/server/src/notifikace.ts index 4f4f6cc..f0f2070 100644 --- a/server/src/notifikace.ts +++ b/server/src/notifikace.ts @@ -29,6 +29,15 @@ export async function saveNotificationSettings(login: string, settings: Notifica return settings; } +/** + * Platné téma ntfy (jen písmena, číslice, „-“ a „_“, nejvýše 64 znaků). Témata vznikají z loginů + * a uživatelských nastavení — cokoli jiného by se do URL ntfy dostalo jako cesta nebo parametry + * (např. „x?email=…“), a požadavek přitom nese přihlašovací údaje aplikace. + */ +export function isValidNtfyTopic(topic: string): boolean { + return /^[-_A-Za-z0-9]{1,64}$/.test(topic); +} + /** Odešle ntfy notifikaci na dané téma. */ async function ntfyCallToTopic(topic: string, message: string) { const url = process.env.NTFY_HOST; @@ -37,10 +46,14 @@ async function ntfyCallToTopic(topic: string, message: string) { if (!url || !username || !password) { return; } + if (!isValidNtfyTopic(topic)) { + console.warn(`Neplatné téma ntfy "${topic}" — notifikace se neposílá`); + return; + } const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64'); try { const response = await axios({ - url: `${url}/${topic}`, + url: `${url}/${encodeURIComponent(topic)}`, method: 'POST', data: message, headers: { @@ -80,8 +93,13 @@ export const ntfyCall = async (data: NotifikaceInput, recipients?: string[]) => try { // Odstraníme mezery a diakritiku a převedeme na lowercase const topic = user.normalize('NFD').replace(' ', '').replace(/[\u0300-\u036f]/g, '').toLowerCase(); + // Login je volný text — téma mimo povolené znaky by se do URL dostalo jako cesta/parametry + if (!isValidNtfyTopic(topic)) { + console.warn(`Login "${user}" nedává platné téma ntfy — notifikace se neposílá`); + return; + } const response = await axios({ - url: `${url}/${topic}`, + url: `${url}/${encodeURIComponent(topic)}`, method: 'POST', data: `${data.udalost} - spustil:${data.user}`, headers: { diff --git a/server/src/tests/groups.test.ts b/server/src/tests/groups.test.ts index 02d97af..8bb9e6a 100644 --- a/server/src/tests/groups.test.ts +++ b/server/src/tests/groups.test.ts @@ -382,6 +382,18 @@ describe('notifikace skupin z Pizza Chefie', () => { ); }); + test('přepínání Objednáno ⇄ Uzamčeno notifikaci znovu neposílá', async () => { + const created = await createGroup(CREATOR, CHEFIE_STORE, TODAY); + const groupId = created.groups![0].id; + await order(groupId); + expect(mockedNotifikace).toHaveBeenCalledTimes(1); + const reverted = await setGroupState(CREATOR, groupId, GroupState.LOCKED, TODAY); + // Návrat do uzamčeno maže QR i časy, ale příznak odeslané notifikace ponechá + expect(reverted.groups![0].pizzaOrderedNotified).toBe(true); + await setGroupState(CREATOR, groupId, GroupState.ORDERED, TODAY); + expect(mockedNotifikace).toHaveBeenCalledTimes(1); + }); + test('Doručeno žádnou další notifikaci neposílá', async () => { const created = await createGroup(CREATOR, CHEFIE_STORE, TODAY); const groupId = created.groups![0].id; diff --git a/server/src/tests/notifikace.test.ts b/server/src/tests/notifikace.test.ts index cc57312..b609130 100644 --- a/server/src/tests/notifikace.test.ts +++ b/server/src/tests/notifikace.test.ts @@ -1,6 +1,6 @@ import axios from 'axios'; import { resetMemoryStorage } from '../storage/memory'; -import { callNotifikace, saveNotificationSettings } from '../notifikace'; +import { callNotifikace, isValidNtfyTopic, saveNotificationSettings } from '../notifikace'; import { addChoice } from '../service'; import { LunchChoice, UdalostEnum } from '../../../types/gen/types.gen'; @@ -55,3 +55,23 @@ describe('callNotifikace — příjemci', () => { expect(ntfyTopics().sort()).toEqual(['petr', 'petr-osobni', 'tomaszakladatel']); }); }); + +describe('ochrana URL ntfy', () => { + test('platné téma ntfy', () => { + expect(isValidNtfyTopic('tomaszakladatel')).toBe(true); + expect(isValidNtfyTopic('petr-osobni_2')).toBe(true); + expect(isValidNtfyTopic('x?email=a@b.cz')).toBe(false); + expect(isValidNtfyTopic('a/../v1/account')).toBe(false); + expect(isValidNtfyTopic('')).toBe(false); + expect(isValidNtfyTopic('a'.repeat(65))).toBe(false); + }); + + test('login, který nedává platné téma, se přeskočí — ostatní příjemci notifikaci dostanou', async () => { + const warn = jest.spyOn(console, 'warn').mockImplementation(() => { }); + await saveNotificationSettings('Petr', { ntfyTopic: 'x?call=yes', enabledEvents: [UdalostEnum.OBJEDNANA_PIZZA] }); + await callNotifikace({ input: INPUT }, ['Tomáš Zakladatel', 'x?email=a@b.cz', 'a/../v1/account', 'Petr']); + // Globální ntfy jen pro platná témata; Petrovo osobní téma s parametrem se zahodí + expect(ntfyTopics().sort()).toEqual(['petr', 'tomaszakladatel']); + expect(warn).toHaveBeenCalledTimes(3); + }); +}); diff --git a/types/schemas/_index.yml b/types/schemas/_index.yml index d9a9922..25c8f30 100644 --- a/types/schemas/_index.yml +++ b/types/schemas/_index.yml @@ -1271,6 +1271,9 @@ OrderGroup: qrGenerated: description: Příznak, zda byly pro skupinu vygenerovány QR kódy (blokuje opakované generování) type: boolean + pizzaOrderedNotified: + description: Členům skupiny z Pizza Chefie už byla odeslána notifikace „Objednána pizza“ (posílá se jednou za skupinu, ani návrat do stavu uzamčeno ji neobnoví) + type: boolean trackingProvider: $ref: "#/TrackingProvider" trackingCode: