49 lines
2.7 KiB
JavaScript
49 lines
2.7 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { Store } from '../src/store.mjs';
|
|
test('profiles and bookmarks persist without accepting caller-provided paths', () => {
|
|
const directory = mkdtempSync(path.join(os.tmpdir(), 'runestone-store-'));
|
|
try {
|
|
const store = new Store(directory);
|
|
assert.equal(store.data.settings.adblock, true);
|
|
assert.equal(store.data.settings.search, 'duckduckgo');
|
|
assert.deepEqual(store.data.profiles, [{ id: 'user', name: 'User' }]);
|
|
const profile = store.createProfile('../name-is-only-a-label');
|
|
assert.match(profile.id, /^[a-f0-9-]{36}$/);
|
|
store.bookmark(profile.id, 'https://example.com/', '<script>test</script>');
|
|
const restored = new Store(directory);
|
|
assert.equal(restored.data.bookmarks[0].profileId, profile.id);
|
|
assert.throws(() => store.profile('../../other'));
|
|
assert.throws(() => store.bookmark('user', 'javascript:alert(1)', 'unsafe'));
|
|
} finally { rmSync(directory, { recursive: true, force: true }); }
|
|
});
|
|
|
|
test('browser settings migrate, persist and reject invalid homepages and lists atomically', () => {
|
|
const directory = mkdtempSync(path.join(os.tmpdir(), 'runestone-settings-'));
|
|
try {
|
|
const legacy = new Store(directory);
|
|
delete legacy.data.settings.homepage; delete legacy.data.settings.filterLists;
|
|
writeFileSync(legacy.file, JSON.stringify(legacy.data));
|
|
const store = new Store(directory);
|
|
assert.equal(store.data.settings.homepage, '');
|
|
assert.equal(store.data.settings.filterLists.length, 2);
|
|
store.configure({ ...store.data.settings, homepage: ' https://example.com/home ', filterLists: ['https://example.com/list.txt'], linkDestination: 'external', adblock: false });
|
|
const saved = structuredClone(store.data.settings);
|
|
assert.deepEqual(new Store(directory).data.settings, saved);
|
|
for (const homepage of ['javascript:alert(1)', 'file:///C:/secret', 'https://user:[email protected]', 'search words']) {
|
|
assert.throws(() => store.configure({ ...saved, homepage }));
|
|
assert.deepEqual(store.data.settings, saved);
|
|
}
|
|
for (const filterLists of [['http://example.com/list'], ['https://example.com:444/list'], Array(9).fill('https://example.com/list'), 'not-an-array']) {
|
|
assert.throws(() => store.configure({ ...saved, filterLists }));
|
|
assert.deepEqual(new Store(directory).data.settings, saved);
|
|
}
|
|
store.configure({ ...saved, homepage: '', filterLists: [] });
|
|
assert.equal(new Store(directory).data.settings.homepage, '');
|
|
assert.deepEqual(new Store(directory).data.settings.filterLists, []);
|
|
} finally { rmSync(directory, { recursive: true, force: true }); }
|
|
});
|