File size: 4,381 Bytes
b8fbd8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import assert from 'node:assert/strict';
import test, { after, beforeEach } from 'node:test';
import express from 'express';
import { isSafeIServPath, registerIServNotes } from '../src/api/iserv-notes.js';

const KEY = 'k'.repeat(20);
process.env.NOTES_ACCESS_TOKEN = KEY;
process.env.ISERV_BASE_URL = 'https://schule.example';
process.env.ISERV_USERNAME = 'user';
process.env.ISERV_PASSWORD = 'pass';

let overviewCalls = 0;
let overviewImpl;
let fileImpl;
const app = express();
registerIServNotes(app, {
  overview: async (...args) => {
    overviewCalls += 1;
    return overviewImpl(...args);
  },
  file: async (...args) => fileImpl(...args),
});
const server = app.listen(0);
const base = `http://127.0.0.1:${server.address().port}/api/notes/iserv`;
const get = (path, key = KEY) => fetch(`${base}${path}`, { headers: key ? { 'x-app-key': key } : {} });
after(() => server.close());
beforeEach(() => {
  overviewCalls = 0;
  overviewImpl = async () => [{ id: 1, url: 'https://schule.example/iserv/exercise/show/1', title: 'Blatt', done: false }];
  fileImpl = async () => ({ contentType: 'application/pdf', buffer: Buffer.from('PDF') });
});

test('isSafeIServPath only lets plain /iserv/ paths through', () => {
  assert.equal(isSafeIServPath('/iserv/fs/file/exercise/1'), true);
  assert.equal(isSafeIServPath('/iserv/fs/download/9?x=1'), true);
  for (const bad of ['/etc/passwd', '/iserv/../etc', '//evil.example/iserv/x', 'https://evil.example/iserv/x', '/iserv/a//b', '/iserv/a\\b', '', undefined]) {
    assert.equal(isSafeIServPath(bad), false, String(bad));
  }
});

test('no or short NOTES_ACCESS_TOKEN: closed with 503', async () => {
  const saved = process.env.NOTES_ACCESS_TOKEN;
  process.env.NOTES_ACCESS_TOKEN = 'short';
  try {
    assert.equal((await get('/tasks', 'short')).status, 503);
  } finally {
    process.env.NOTES_ACCESS_TOKEN = saved;
  }
});

test('missing IServ secrets: closed with 503', async () => {
  const saved = process.env.ISERV_PASSWORD;
  delete process.env.ISERV_PASSWORD;
  try {
    assert.equal((await get('/tasks')).status, 503);
  } finally {
    process.env.ISERV_PASSWORD = saved;
  }
});

test('missing or wrong key is refused on both routes', async () => {
  for (const path of ['/tasks', '/file?path=/iserv/x']) {
    assert.equal((await get(path, '')).status, 401);
    assert.equal((await get(path, 'x'.repeat(20))).status, 401);
  }
});

test('tasks: right key returns the list and a second call comes from the cache', async () => {
  const first = await get('/tasks');
  assert.equal(first.status, 200);
  assert.equal(first.headers.get('cache-control'), 'no-store');
  const body = await first.json();
  assert.equal(body.tasks[0].title, 'Blatt');
  assert.match(body.fetchedAt, /^\d{4}-\d{2}-\d{2}T/);
  await get('/tasks');
  assert.equal(overviewCalls, 1);
});

test('tasks: a failing fetch gives only the code, never the message', async () => {
  const failing = express();
  registerIServNotes(failing, {
    overview: async () => {
      throw new Error('https://schule.example secret');
    },
  });
  const s = failing.listen(0);
  try {
    const r = await fetch(`http://127.0.0.1:${s.address().port}/api/notes/iserv/tasks`, { headers: { 'x-app-key': KEY } });
    assert.equal(r.status, 502);
    assert.equal(await r.text(), '{"error":"unreachable"}');
  } finally {
    s.close();
  }
});

test('file: unsafe path is 400, safe path streams the bytes with the content type', async () => {
  assert.equal((await get('/file?path=/etc/passwd')).status, 400);
  const ok = await get('/file?path=%2Fiserv%2Ffs%2Ffile%2Fexercise%2F1');
  assert.equal(ok.status, 200);
  assert.equal(ok.headers.get('content-type'), 'application/pdf');
  assert.equal(Buffer.from(await ok.arrayBuffer()).toString(), 'PDF');
});

test('file: too_large is 413, refused is 502 with its code', async () => {
  fileImpl = async () => {
    throw Object.assign(new Error('x'), { code: 'too_large' });
  };
  const big = await get('/file?path=/iserv/fs/file/exercise/1');
  assert.equal(big.status, 413);
  assert.deepEqual(await big.json(), { error: 'too_large' });
  fileImpl = async () => {
    throw Object.assign(new Error('x'), { code: 'refused' });
  };
  const refused = await get('/file?path=/iserv/fs/file/exercise/1');
  assert.equal(refused.status, 502);
  assert.deepEqual(await refused.json(), { error: 'refused' });
});