Files
bookmarkmagic/tests/unit/browser/open-url.test.ts
T
poli0981andClaude Opus 5 106f0ad337 refactor(edit): split EditTab under the size limit and test what came out
EditTab.svelte was 589 lines against the 500-line hard limit in docs/10 §1 --
the one place in the codebase that broke it. Now 459.

Extracted, in each case because the thing was genuinely cohesive rather than to
hit a number:

- src/lib/edit/edit-node.ts -- toEditNode, flattenTree, isEditable,
  resolveNewFolderParent, countDescendants, extraCopyIds. All pure, all
  previously inline and therefore untestable. resolveNewFolderParent in
  particular encodes a fixed bug (a focused root resolving to the synthetic
  root "0", which every create rejects) that nothing was pinning.
- src/lib/browser/open-url.ts -- the docs/09 T3 scheme allowlist, which is the
  only thing between a crafted `javascript:` bookmark in someone's import file
  and the browser acting on it. It had NO test. It does now, including the
  case-shouting dodge and data:/file:/chrome:.
- EditToolbar.svelte and MoveToDialog.svelte -- presentational, with their
  styles copied verbatim. This is a move, not a redesign.

The script block is still 279 lines against docs/10 §1's 150-line guidance. The
remainder is the mutation layer (guardEdit and the six operations), which is
coupled to reactive state and would need accessor indirection to move. Adding
that indirection to the app's delete paths immediately before a release is a
worse trade than the line count, so it stays and is recorded in CLAUDE.md
rather than quietly ignored.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-26 14:05:50 +07:00

63 lines
2.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fakeBrowser } from 'wxt/testing';
import { openBookmarkUrl } from '@/lib/browser/open-url';
/**
* The docs/09 T3 control: bookmark URLs come from someone's import file and are
* stored verbatim, so this allowlist is the only thing between a crafted
* `javascript:` bookmark and the browser acting on it. It had no test.
*/
let created: { url?: string }[] = [];
beforeEach(() => {
fakeBrowser.reset();
vi.restoreAllMocks();
created = [];
// fakeBrowser stubs tabs.* with "not implemented" throws.
vi.spyOn(fakeBrowser.tabs, 'create').mockImplementation(async (props) => {
created.push(props);
// SAFETY: the caller ignores the return value; only the recorded call
// matters, and building a full Tab here would assert nothing.
return {} as Awaited<ReturnType<typeof fakeBrowser.tabs.create>>;
});
});
describe('openBookmarkUrl', () => {
it.each([
'https://example.com/',
'http://example.com/path?q=1',
'https://example.com:8443/#frag',
])('opens %s', async (url) => {
expect(await openBookmarkUrl(url)).toBe(true);
expect(created).toEqual([{ url }]);
});
it.each([
['javascript:alert(1)', 'the classic bookmarklet payload'],
['data:text/html,<script>alert(1)</script>', 'inline document'],
['file:///etc/passwd', 'local file read'],
['chrome://settings', 'privileged browser page'],
['ftp://example.com/', 'not in the allowlist'],
['vbscript:msgbox(1)', 'legacy script scheme'],
])('refuses %s (%s)', async (url) => {
expect(await openBookmarkUrl(url)).toBe(false);
expect(created).toEqual([]);
});
it('refuses an unparseable URL without throwing', async () => {
for (const url of ['', 'not a url', '///', 'http://']) {
await expect(openBookmarkUrl(url)).resolves.toBe(false);
}
expect(created).toEqual([]);
});
it('is case-insensitive about the scheme, as the URL parser is', async () => {
// `new URL('JavaScript:…').protocol` normalizes to lowercase, so an
// attacker cannot dodge the allowlist by shouting.
expect(await openBookmarkUrl('JaVaScRiPt:alert(1)')).toBe(false);
expect(await openBookmarkUrl('HTTPS://example.com/')).toBe(true);
expect(created).toEqual([{ url: 'HTTPS://example.com/' }]);
});
});