Spaces:
Paused
Paused
feat(iserv): /api/notes/iserv/tasks and /file for the notes app
Browse files- server.js +2 -0
- src/api/iserv-notes.js +68 -0
- tests/iserv-notes.test.mjs +117 -0
server.js
CHANGED
|
@@ -16,6 +16,7 @@ import { fileURLToPath } from 'url';
|
|
| 16 |
import { spawn } from 'child_process';
|
| 17 |
import fs from 'fs';
|
| 18 |
import { registerIServFiltered } from './src/api/iserv-filtered.js';
|
|
|
|
| 19 |
|
| 20 |
console.log("--- Imports loaded ---");
|
| 21 |
|
|
@@ -818,6 +819,7 @@ app.get('/api/youtube-transcript', async (req, res) => {
|
|
| 818 |
|
| 819 |
// IServ tasks without names for the assistant (token protected, credentials from the Space secrets)
|
| 820 |
registerIServFiltered(app);
|
|
|
|
| 821 |
|
| 822 |
// Fallback for SPA routing - only for navigation requests, NOT for assets
|
| 823 |
app.use((req, res) => {
|
|
|
|
| 16 |
import { spawn } from 'child_process';
|
| 17 |
import fs from 'fs';
|
| 18 |
import { registerIServFiltered } from './src/api/iserv-filtered.js';
|
| 19 |
+
import { registerIServNotes } from './src/api/iserv-notes.js';
|
| 20 |
|
| 21 |
console.log("--- Imports loaded ---");
|
| 22 |
|
|
|
|
| 819 |
|
| 820 |
// IServ tasks without names for the assistant (token protected, credentials from the Space secrets)
|
| 821 |
registerIServFiltered(app);
|
| 822 |
+
registerIServNotes(app);
|
| 823 |
|
| 824 |
// Fallback for SPA routing - only for navigation requests, NOT for assets
|
| 825 |
app.use((req, res) => {
|
src/api/iserv-notes.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// GET /api/notes/iserv/tasks and GET /api/notes/iserv/file?path=: the notes app's own view of IServ (tasks with
|
| 2 |
+
// status, description and provided files). IServ credentials come from the Space secrets (ISERV_BASE_URL,
|
| 3 |
+
// ISERV_USERNAME, ISERV_PASSWORD), never from the request. Unlike the other /api/notes routes these FAIL CLOSED:
|
| 4 |
+
// without a NOTES_ACCESS_TOKEN of at least 16 characters nothing is served. Errors carry a code only (the messages
|
| 5 |
+
// of Puppeteer and IServ quote the school domain and task text). Nothing is logged.
|
| 6 |
+
import { timingSafeEqual } from 'node:crypto';
|
| 7 |
+
import { downloadIServFile, fetchIServOverview } from './iserv.js';
|
| 8 |
+
|
| 9 |
+
const CACHE_MS = 3 * 60_000; // several opens of the app must not log in to IServ each time
|
| 10 |
+
|
| 11 |
+
/** A path on the school server: /iserv/…, no scheme or host, no dot segments, no double slash. */
|
| 12 |
+
export function isSafeIServPath(path) {
|
| 13 |
+
return typeof path === 'string' && path.startsWith('/iserv/') && path.length <= 2000 && !/[\\\s]|\.\.|\/\//.test(path);
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
function keyOk(req) {
|
| 17 |
+
const expected = Buffer.from(process.env.NOTES_ACCESS_TOKEN || '');
|
| 18 |
+
const offered = [req.headers['x-app-key'], (req.headers.authorization || '').replace(/^Bearer\s+/i, '')];
|
| 19 |
+
return offered.some((o) => o && Buffer.byteLength(o) === expected.length && timingSafeEqual(Buffer.from(o), expected));
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
/** true if the request may continue; otherwise the answer is already sent. */
|
| 23 |
+
function allowed(req, res) {
|
| 24 |
+
res.set('Cache-Control', 'no-store');
|
| 25 |
+
if ((process.env.NOTES_ACCESS_TOKEN || '').length < 16) return void res.status(503).json({ error: 'config' });
|
| 26 |
+
if (!keyOk(req)) return void res.status(401).json({ error: 'unauthorized' });
|
| 27 |
+
const { ISERV_BASE_URL: url, ISERV_USERNAME: user, ISERV_PASSWORD: pass } = process.env;
|
| 28 |
+
if (!url || !user || !pass || !url.startsWith('https://')) return void res.status(503).json({ error: 'config' });
|
| 29 |
+
return true;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
export function registerIServNotes(app, { overview = fetchIServOverview, file = downloadIServFile, now = Date.now } = {}) {
|
| 33 |
+
let running = Promise.resolve(); // one IServ session at a time
|
| 34 |
+
let cached = null; // { at, tasks }, memory only
|
| 35 |
+
const credentials = () => [process.env.ISERV_BASE_URL, process.env.ISERV_USERNAME, process.env.ISERV_PASSWORD];
|
| 36 |
+
const queued = (run) => {
|
| 37 |
+
const job = running.then(run, run);
|
| 38 |
+
running = job.catch(() => {});
|
| 39 |
+
return job;
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
app.get('/api/notes/iserv/tasks', async (req, res) => {
|
| 43 |
+
if (!allowed(req, res)) return;
|
| 44 |
+
try {
|
| 45 |
+
const { at, tasks } = await queued(async () => {
|
| 46 |
+
if (cached && now() - cached.at < CACHE_MS) return cached;
|
| 47 |
+
cached = { at: now(), tasks: await overview(...credentials()) };
|
| 48 |
+
return cached;
|
| 49 |
+
});
|
| 50 |
+
res.json({ tasks, fetchedAt: new Date(at).toISOString() });
|
| 51 |
+
} catch {
|
| 52 |
+
res.status(502).json({ error: 'unreachable' });
|
| 53 |
+
}
|
| 54 |
+
});
|
| 55 |
+
|
| 56 |
+
app.get('/api/notes/iserv/file', async (req, res) => {
|
| 57 |
+
if (!allowed(req, res)) return;
|
| 58 |
+
const path = req.query.path;
|
| 59 |
+
if (!isSafeIServPath(path)) return res.status(400).json({ error: 'bad_path' });
|
| 60 |
+
try {
|
| 61 |
+
const { contentType, buffer } = await queued(() => file(...credentials(), path));
|
| 62 |
+
res.set('Content-Type', contentType).send(buffer);
|
| 63 |
+
} catch (e) {
|
| 64 |
+
const code = e?.code === 'too_large' || e?.code === 'refused' ? e.code : 'unreachable';
|
| 65 |
+
res.status(code === 'too_large' ? 413 : 502).json({ error: code });
|
| 66 |
+
}
|
| 67 |
+
});
|
| 68 |
+
}
|
tests/iserv-notes.test.mjs
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import assert from 'node:assert/strict';
|
| 2 |
+
import test, { after, beforeEach } from 'node:test';
|
| 3 |
+
import express from 'express';
|
| 4 |
+
import { isSafeIServPath, registerIServNotes } from '../src/api/iserv-notes.js';
|
| 5 |
+
|
| 6 |
+
const KEY = 'k'.repeat(20);
|
| 7 |
+
process.env.NOTES_ACCESS_TOKEN = KEY;
|
| 8 |
+
process.env.ISERV_BASE_URL = 'https://schule.example';
|
| 9 |
+
process.env.ISERV_USERNAME = 'user';
|
| 10 |
+
process.env.ISERV_PASSWORD = 'pass';
|
| 11 |
+
|
| 12 |
+
let overviewCalls = 0;
|
| 13 |
+
let overviewImpl;
|
| 14 |
+
let fileImpl;
|
| 15 |
+
const app = express();
|
| 16 |
+
registerIServNotes(app, {
|
| 17 |
+
overview: async (...args) => {
|
| 18 |
+
overviewCalls += 1;
|
| 19 |
+
return overviewImpl(...args);
|
| 20 |
+
},
|
| 21 |
+
file: async (...args) => fileImpl(...args),
|
| 22 |
+
});
|
| 23 |
+
const server = app.listen(0);
|
| 24 |
+
const base = `http://127.0.0.1:${server.address().port}/api/notes/iserv`;
|
| 25 |
+
const get = (path, key = KEY) => fetch(`${base}${path}`, { headers: key ? { 'x-app-key': key } : {} });
|
| 26 |
+
after(() => server.close());
|
| 27 |
+
beforeEach(() => {
|
| 28 |
+
overviewCalls = 0;
|
| 29 |
+
overviewImpl = async () => [{ id: 1, url: 'https://schule.example/iserv/exercise/show/1', title: 'Blatt', done: false }];
|
| 30 |
+
fileImpl = async () => ({ contentType: 'application/pdf', buffer: Buffer.from('PDF') });
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
test('isSafeIServPath only lets plain /iserv/ paths through', () => {
|
| 34 |
+
assert.equal(isSafeIServPath('/iserv/fs/file/exercise/1'), true);
|
| 35 |
+
assert.equal(isSafeIServPath('/iserv/fs/download/9?x=1'), true);
|
| 36 |
+
for (const bad of ['/etc/passwd', '/iserv/../etc', '//evil.example/iserv/x', 'https://evil.example/iserv/x', '/iserv/a//b', '/iserv/a\\b', '', undefined]) {
|
| 37 |
+
assert.equal(isSafeIServPath(bad), false, String(bad));
|
| 38 |
+
}
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
test('no or short NOTES_ACCESS_TOKEN: closed with 503', async () => {
|
| 42 |
+
const saved = process.env.NOTES_ACCESS_TOKEN;
|
| 43 |
+
process.env.NOTES_ACCESS_TOKEN = 'short';
|
| 44 |
+
try {
|
| 45 |
+
assert.equal((await get('/tasks', 'short')).status, 503);
|
| 46 |
+
} finally {
|
| 47 |
+
process.env.NOTES_ACCESS_TOKEN = saved;
|
| 48 |
+
}
|
| 49 |
+
});
|
| 50 |
+
|
| 51 |
+
test('missing IServ secrets: closed with 503', async () => {
|
| 52 |
+
const saved = process.env.ISERV_PASSWORD;
|
| 53 |
+
delete process.env.ISERV_PASSWORD;
|
| 54 |
+
try {
|
| 55 |
+
assert.equal((await get('/tasks')).status, 503);
|
| 56 |
+
} finally {
|
| 57 |
+
process.env.ISERV_PASSWORD = saved;
|
| 58 |
+
}
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
test('missing or wrong key is refused on both routes', async () => {
|
| 62 |
+
for (const path of ['/tasks', '/file?path=/iserv/x']) {
|
| 63 |
+
assert.equal((await get(path, '')).status, 401);
|
| 64 |
+
assert.equal((await get(path, 'x'.repeat(20))).status, 401);
|
| 65 |
+
}
|
| 66 |
+
});
|
| 67 |
+
|
| 68 |
+
test('tasks: right key returns the list and a second call comes from the cache', async () => {
|
| 69 |
+
const first = await get('/tasks');
|
| 70 |
+
assert.equal(first.status, 200);
|
| 71 |
+
assert.equal(first.headers.get('cache-control'), 'no-store');
|
| 72 |
+
const body = await first.json();
|
| 73 |
+
assert.equal(body.tasks[0].title, 'Blatt');
|
| 74 |
+
assert.match(body.fetchedAt, /^\d{4}-\d{2}-\d{2}T/);
|
| 75 |
+
await get('/tasks');
|
| 76 |
+
assert.equal(overviewCalls, 1);
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
test('tasks: a failing fetch gives only the code, never the message', async () => {
|
| 80 |
+
const failing = express();
|
| 81 |
+
registerIServNotes(failing, {
|
| 82 |
+
overview: async () => {
|
| 83 |
+
throw new Error('https://schule.example secret');
|
| 84 |
+
},
|
| 85 |
+
});
|
| 86 |
+
const s = failing.listen(0);
|
| 87 |
+
try {
|
| 88 |
+
const r = await fetch(`http://127.0.0.1:${s.address().port}/api/notes/iserv/tasks`, { headers: { 'x-app-key': KEY } });
|
| 89 |
+
assert.equal(r.status, 502);
|
| 90 |
+
assert.equal(await r.text(), '{"error":"unreachable"}');
|
| 91 |
+
} finally {
|
| 92 |
+
s.close();
|
| 93 |
+
}
|
| 94 |
+
});
|
| 95 |
+
|
| 96 |
+
test('file: unsafe path is 400, safe path streams the bytes with the content type', async () => {
|
| 97 |
+
assert.equal((await get('/file?path=/etc/passwd')).status, 400);
|
| 98 |
+
const ok = await get('/file?path=%2Fiserv%2Ffs%2Ffile%2Fexercise%2F1');
|
| 99 |
+
assert.equal(ok.status, 200);
|
| 100 |
+
assert.equal(ok.headers.get('content-type'), 'application/pdf');
|
| 101 |
+
assert.equal(Buffer.from(await ok.arrayBuffer()).toString(), 'PDF');
|
| 102 |
+
});
|
| 103 |
+
|
| 104 |
+
test('file: too_large is 413, refused is 502 with its code', async () => {
|
| 105 |
+
fileImpl = async () => {
|
| 106 |
+
throw Object.assign(new Error('x'), { code: 'too_large' });
|
| 107 |
+
};
|
| 108 |
+
const big = await get('/file?path=/iserv/fs/file/exercise/1');
|
| 109 |
+
assert.equal(big.status, 413);
|
| 110 |
+
assert.deepEqual(await big.json(), { error: 'too_large' });
|
| 111 |
+
fileImpl = async () => {
|
| 112 |
+
throw Object.assign(new Error('x'), { code: 'refused' });
|
| 113 |
+
};
|
| 114 |
+
const refused = await get('/file?path=/iserv/fs/file/exercise/1');
|
| 115 |
+
assert.equal(refused.status, 502);
|
| 116 |
+
assert.deepEqual(await refused.json(), { error: 'refused' });
|
| 117 |
+
});
|