Terminplaner: Zaehler je IP getrennt und Abgelaufene zuerst, Notantwort beim Schreiben, Pruefung aller Antworten auf interne IDs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2b0c05b8e9
commit
ae1886c030
4 files changed
+226
-31
No files matched your search
@@ -127,9 +127,38 @@ let log
|
||||
let clientIp = 0
|
||||
let ip
|
||||
|
||||
/** Anfrage an die oeffentliche Schnittstelle: Pfad ab /v1, Origin wie im Browser (Vorgabe: Seite des Betriebs), Client-Adresse je Test. */
|
||||
function api(method, pfad, { body, origin = O_LIVE, ip: adresse = ip, headers = {}, cookie } = {}) {
|
||||
return send(method, BASIS + pfad, { body, cookie, headers: { 'x-forwarded-for': adresse, ...(origin ? { origin } : {}), ...headers } })
|
||||
// Nichts Internes in irgendeiner Antwort: weder Felder noch Werte mit IDs des Portals oder Appwrite-Metadaten.
|
||||
// Erlaubt sind die Leistungs-/Ressourcen-IDs der Einstellung und die Buchungsnummer (8 Zeichen); Buchungs-IDs sind UUIDs.
|
||||
const VERBOTENE_FELDER = new Set([
|
||||
'customerId', 'projectId', 'featureId', 'bookingId', '$id', '$collectionId', '$databaseId', '$createdAt', '$updatedAt', '$permissions',
|
||||
'verifyHash', 'manageHash', 'feedToken',
|
||||
])
|
||||
const INTERNE_IDS = [KUNDE, PROJEKT, FEATURE]
|
||||
const UUID_RE = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/i
|
||||
|
||||
/** Wirft, wenn irgendwo im (JSON-)Wert ein verbotenes Feld, eine interne ID oder eine Buchungs-UUID steht. */
|
||||
function pruefeOhneInternes(wert, pfad = '$', { uuid = true } = {}) {
|
||||
if (typeof wert === 'string') {
|
||||
for (const id of INTERNE_IDS) assert.ok(!wert.includes(id), `${pfad}: interne ID ${id} in der Antwort`)
|
||||
if (uuid) assert.ok(!UUID_RE.test(wert), `${pfad}: UUID (Buchungs-ID?) in der Antwort`)
|
||||
} else if (Array.isArray(wert)) {
|
||||
wert.forEach((w, i) => pruefeOhneInternes(w, `${pfad}[${i}]`, { uuid }))
|
||||
} else if (wert !== null && typeof wert === 'object') {
|
||||
for (const [feld, w] of Object.entries(wert)) {
|
||||
assert.ok(!VERBOTENE_FELDER.has(feld), `${pfad}.${feld}: internes Feld in der Antwort`)
|
||||
pruefeOhneInternes(w, `${pfad}.${feld}`, { uuid })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Anfrage an die oeffentliche Schnittstelle: Pfad ab /v1, Origin wie im Browser (Vorgabe: Seite des Betriebs), Client-Adresse je Test.
|
||||
* JEDE Antwort (Erfolg wie Fehler) wird auf interne IDs geprueft; der Kalender-Text (UID = Buchungs-ID ist dort Pflicht) nur auf Portal-IDs.
|
||||
*/
|
||||
async function api(method, pfad, { body, origin = O_LIVE, ip: adresse = ip, headers = {}, cookie } = {}) {
|
||||
const res = await send(method, BASIS + pfad, { body, cookie, headers: { 'x-forwarded-for': adresse, ...(origin ? { origin } : {}), ...headers } })
|
||||
pruefeOhneInternes(res.body, `${method} ${pfad}`, { uuid: typeof res.body !== 'string' })
|
||||
return res
|
||||
}
|
||||
const buchen = (over, opts) => api('POST', `/${SITE_KEY}/buchungen`, { body: buchungBody(over), ...opts })
|
||||
const tokenAnruf = (aktion, body, opts = {}) => api('POST', `/termin/${aktion}`, { body, origin: O_PORTAL, ...opts })
|
||||
@@ -999,3 +1028,42 @@ test('Antworten tragen kein Set-Cookie und fuer Browser nosniff', async () => {
|
||||
assert.equal(res.headers.get('x-content-type-options'), 'nosniff')
|
||||
}
|
||||
})
|
||||
|
||||
test('der Pruefer fuer interne IDs hat Zaehne: Felder, Werte, verschachtelt, UUIDs - die Buchungsnummer ist erlaubt', () => {
|
||||
assert.doesNotThrow(() => pruefeOhneInternes({ ok: true, termin: { nummer: 'AB12CD34', leistung: 'Damenschnitt' }, ressourcen: [{ id: R1 }] }))
|
||||
for (const schlecht of [
|
||||
{ featureId: 'x' }, { a: { b: [{ customerId: 'x' }] } }, { $id: 'x' }, { $collectionId: 'termineBookings' }, { text: `Funktion ${FEATURE}` },
|
||||
{ liste: [KUNDE] }, { id: '0b2a3c4d-5e6f-4a7b-8c9d-0e1f2a3b4c5d' }, { projectId: PROJEKT },
|
||||
]) {
|
||||
assert.throws(() => pruefeOhneInternes(schlecht), assert.AssertionError, JSON.stringify(schlecht))
|
||||
}
|
||||
})
|
||||
|
||||
test('schreibt die Antwort selbst nicht (Serialisierung wirft): 503 { fehler } statt haengender Anfrage und unbehandelter Ablehnung', async () => {
|
||||
await speichere()
|
||||
const unbehandelt = []
|
||||
const merke = (grund) => unbehandelt.push(grund)
|
||||
process.on('unhandledRejection', merke)
|
||||
const original = Buffer.byteLength
|
||||
const ersatz = mock.method(Buffer, 'byteLength', function byteLength(text, ...rest) {
|
||||
if (typeof text === 'string' && text.includes('"formToken"')) throw new Error('Nachbau kaputt: geheimer Rohtext')
|
||||
return original.call(this, text, ...rest)
|
||||
})
|
||||
let res
|
||||
try {
|
||||
res = await Promise.race([
|
||||
api('GET', `/${SITE_KEY}/config`),
|
||||
new Promise((_, ab) => setTimeout(() => ab(new Error('Anfrage haengt: keine Antwort')), 3000).unref()),
|
||||
])
|
||||
await new Promise((resolve) => setImmediate(resolve)) // eine unbehandelte Ablehnung meldet sich erst danach
|
||||
} finally {
|
||||
ersatz.mock.restore()
|
||||
process.off('unhandledRejection', merke)
|
||||
}
|
||||
assert.deepEqual([res.status, res.body], [503, NICHT_ERREICHBAR])
|
||||
assert.equal(res.headers.get('cache-control'), 'no-store')
|
||||
assert.deepEqual(unbehandelt, [])
|
||||
assert.ok(!JSON.stringify(res.body).includes('geheimer'))
|
||||
assert.ok(log.error.mock.calls.some((c) => String(c.arguments[0]).includes('Antwort nicht geschrieben')), 'steht im Log')
|
||||
assert.equal((await api('GET', `/${SITE_KEY}/config`)).status, 200, 'danach geht es normal weiter')
|
||||
})
|
||||
Reference in new issue
Block a user