fix: close the eighteen findings the Phase 6 review confirmed
Six lenses over the branch, each finding independently attacked by a verifier told to refute it. 31 filed, 18 survived, clustering into five real defects — and the pattern held: every one is in work added earlier in this same branch. **The sanitizer did not keep its promise (9 findings).** It is the single privacy control in the intake flow, named as mandatory in SECURITY.md, README, CONTRIBUTING, the privacy policy and the issue form — and it leaked in nine distinct ways. Worst: `detectKind` fell back to 'csv' for anything it could not identify, `sanitizeCsv` then matched no columns, and the input was emitted verbatim with exit code 0. The users who run this tool are by definition the ones whose file did not import, so a truncated or hand-excerpted file is the TYPICAL input — and `tests/fixtures/malformed/no-doctype.html`, already in the repo, reproduced it. The test could not catch any of this: it returned early whenever the original failed to parse, so it passed vacuously on precisely the dangerous inputs. Nine holes found in one pass is not a signal to patch nine holes. The rules are regexes over untrusted markup, so "we thought of every shape" is not a claim anyone can make honestly. So the tool now fails safe: - unidentified input is REFUSED, matching core detectFormat rather than being weaker than the parser it fronts; - the output is checked before it is written — any host that is not one of our own `*.example` aliases means a rule missed something, and the file is refused with the leaked host named. A rule this misses now degrades into an annoying refusal instead of a published browsing history. The individual holes are fixed too: single-quoted and bare attribute values were never matched (a straight leak); `spansFrom` located capture groups by searching the match text, landing on the wrong copy — replaced with the `d` flag's exact offsets; overlapping spans spliced already-rewritten text using offsets measured against the original; `[^<]*` made a title containing a raw `<` fail to match entirely; `<DD>` stopped at the first newline, publishing the rest of a wrapped note; and replaceSpans rebuilt the whole string per span, O(spans x file). **The import report pointed users at a backup that was never written.** The guard was "mode is replace and this is not a BmBackupError", but runImport resolves the roots BEFORE proving the backup — so NO_WRITABLE_ROOTS, added in this same branch, reaches the catch with nothing on disk. Now keyed on the onClearing callback, which is the only signal that proveBackup actually returned. **Cancelling a Replace after the clear read as a harmless no-op.** The tree is gone and only `created` of it rebuilt; "N items created" alone, on a browser that is now nearly empty, is the wrong story. It now says so and names the backup. **Collapsing a folder while a search was active re-opened it instantly.** The $effect read and wrote the same two `$state` sets, making it its own dependency: every write re-invalidated it and it re-merged an unchanged `search.expand`. Reproduced against the real Svelte runtime — `$effect` is stripped under this repo's vitest config, so no unit test could have seen it. Fixed with `untrack`, which also removes the self-retrigger loop. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
9 files changed
+395
-110
No files matched your search
@@ -148,9 +148,10 @@ Derived identifiers (canonical across docs and code):
|
||||
2026-07-26 and **approved — the listing is public and has users**. Listing
|
||||
copy and the Privacy tab answers came from `13 §3` and `08 §5`. The store URL
|
||||
landed in the README on 2026-07-27 (`ea246aa`) and the item id is now in the
|
||||
§9 identifiers table. **Still open:** the asset reconciliation in item 3, and
|
||||
`store/listing.{en,vi,ja}.md` — `13 §4` promises the listing copy is
|
||||
version-controlled in-repo, and it is not.
|
||||
§9 identifiers table. `store/listing.{en,vi,ja}.md` now hold the listing copy
|
||||
`13 §4` always asked for — ⚠️ reconstructed from `13 §3` *after* the listing
|
||||
went live, so diff them against the dashboard and correct the files.
|
||||
**Still open:** the asset reconciliation in item 3.
|
||||
6. Repo secrets + settings per `12 §5`. ✔ CodeQL "Default setup" is already
|
||||
disabled (the advanced workflow uploads SARIF cleanly). **Still open:**
|
||||
- `DISCORD_CI_WEBHOOK` is **not** optional in practice. `notify-ci-failure.yml`
|
||||
@@ -158,7 +159,9 @@ Derived identifiers (canonical across docs and code):
|
||||
unset that workflow fails on *every* CI completion — success or failure.
|
||||
Either set it or accept the caller-side tolerance in `12 §2.4`.
|
||||
- `DISCORD_RELEASES_WEBHOOK` / `DISCORD_REPO_WEBHOOK` genuinely are optional;
|
||||
the announce job is `continue-on-error` since `5f4c6e7`.
|
||||
the announce job is skipped unless the repo variable `DISCORD_ANNOUNCE` is
|
||||
`true`. (It used to say `continue-on-error`, which GitHub **rejects** on a
|
||||
reusable-workflow caller — see `12 §2.3`.)
|
||||
- The four `CHROME_*` secrets are now a **concrete v1.1 prerequisite**, not a
|
||||
maybe: `15` lists automated CWS publishing as the first v1.1 item. One of
|
||||
the four is already known — `CHROME_EXTENSION_ID` is the item id in §9. The
|
||||
|
||||
+179
-28
@@ -34,7 +34,27 @@ const BLANK_ICON =
|
||||
|
||||
// ---------------------------------------------------------------- format sniff
|
||||
|
||||
/** @param {string} text */
|
||||
/** The docs/04 §3 CSV header, in order. A file is CSV only if it says so. */
|
||||
const CSV_HEADER = ['folder_path', 'title', 'url', 'add_date'];
|
||||
|
||||
/**
|
||||
* Identify the format, or refuse.
|
||||
*
|
||||
* @param {string} text
|
||||
* @returns {'html' | 'json' | 'csv' | null} `null` when nothing matched.
|
||||
*
|
||||
* Returning `null` rather than guessing is load-bearing. Every sanitizer below
|
||||
* finds what it must destroy *by format*, so a wrong guess destroys nothing and
|
||||
* emits the input verbatim — and the file would still be handed to the user as
|
||||
* "sanitized". The old `return 'csv'` fallback did exactly that for a truncated
|
||||
* JSON export, an HTML excerpt with no doctype, or a CSV with foreign headers:
|
||||
* byte-identical output, exit code 0, real hostnames intact.
|
||||
*
|
||||
* The people who run this tool are, by definition, the ones whose file did not
|
||||
* import — so a malformed or hand-trimmed file is the *typical* input here, not
|
||||
* an edge case. `src/lib/core/detect-format.ts` refuses these same inputs with
|
||||
* `UNKNOWN_FORMAT`; this must not be weaker than the parser it fronts.
|
||||
*/
|
||||
export function detectKind(text) {
|
||||
const body = text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
|
||||
if (/NETSCAPE-Bookmark-file-1/i.test(body.slice(0, 512))) return 'html';
|
||||
@@ -47,8 +67,17 @@ export function detectKind(text) {
|
||||
// Not JSON after all; fall through.
|
||||
}
|
||||
}
|
||||
const firstLine = body.split(/\r?\n/, 1)[0] ?? '';
|
||||
for (const delimiter of [',', ';']) {
|
||||
const columns = firstLine
|
||||
.split(delimiter)
|
||||
.map((h) => h.trim().replace(/^"|"$/g, '').toLowerCase());
|
||||
if (columns.length === CSV_HEADER.length && CSV_HEADER.every((c, i) => columns[i] === c)) {
|
||||
return 'csv';
|
||||
}
|
||||
}
|
||||
if (/<\s*dl[\s>]/i.test(body)) return 'html';
|
||||
return 'csv';
|
||||
return null;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------- mappers
|
||||
@@ -170,11 +199,24 @@ function makeTitleMapper() {
|
||||
* @param {{start: number, end: number, value: string}[]} spans
|
||||
*/
|
||||
function replaceSpans(text, spans) {
|
||||
let out = text;
|
||||
for (const span of [...spans].sort((a, b) => b.start - a.start)) {
|
||||
out = out.slice(0, span.start) + span.value + out.slice(span.end);
|
||||
// Overlaps are dropped, outermost first. Two patterns can legitimately claim
|
||||
// the same bytes — an <A> title containing `x="http://…"` matches both the
|
||||
// title rule and the generic attribute sweep — and applying both would splice
|
||||
// already-rewritten text using an `end` measured against the original,
|
||||
// silently eating or stranding bytes. The widest span sanitizes the most.
|
||||
const ordered = [...spans].sort((a, b) => a.start - b.start || b.end - a.end);
|
||||
const out = [];
|
||||
let cursor = 0;
|
||||
for (const span of ordered) {
|
||||
if (span.start < cursor) continue;
|
||||
out.push(text.slice(cursor, span.start), span.value);
|
||||
cursor = span.end;
|
||||
}
|
||||
return out;
|
||||
out.push(text.slice(cursor));
|
||||
// Joined once rather than spliced per span: the old loop rebuilt the whole
|
||||
// string for every replacement, which is O(spans x file) and takes minutes on
|
||||
// a large export.
|
||||
return out.join('');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,12 +233,17 @@ function replaceSpans(text, spans) {
|
||||
* @param {number} [group]
|
||||
*/
|
||||
function spansFrom(text, re, map, group = 1) {
|
||||
// The `d` flag gives exact per-group offsets. The previous version searched
|
||||
// the match text for the captured substring, which lands on the wrong copy
|
||||
// whenever the capture also appears earlier in the match — e.g. a bookmark
|
||||
// whose title equals part of its own URL.
|
||||
const indexed = new RegExp(re.source, re.flags.includes('d') ? re.flags : `${re.flags}d`);
|
||||
const spans = [];
|
||||
for (const m of text.matchAll(re)) {
|
||||
for (const m of text.matchAll(indexed)) {
|
||||
const captured = m[group] ?? '';
|
||||
if (captured === '') continue;
|
||||
const start = m.index + m[0].lastIndexOf(captured);
|
||||
spans.push({ start, end: start + captured.length, value: map(captured) });
|
||||
const at = m.indices?.[group];
|
||||
if (captured === '' || at === undefined) continue;
|
||||
spans.push({ start: at[0], end: at[1], value: map(captured) });
|
||||
}
|
||||
return spans;
|
||||
}
|
||||
@@ -234,6 +281,34 @@ function attrRewriter(map) {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* `name = value`, where the value is double-quoted, single-quoted or bare.
|
||||
*
|
||||
* Group 2/3/4 is the value, whichever quoting was used. Matching only `"…"`
|
||||
* left `HREF='https://…'` completely untouched — real exports in the wild use
|
||||
* single quotes, and that one is a straight leak.
|
||||
*/
|
||||
const ANY_ATTR = /\b([a-z_][a-z0-9_-]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'<>=`]+))/dgi;
|
||||
|
||||
/**
|
||||
* Spans for every attribute whose name matches, at any quoting.
|
||||
*
|
||||
* @param {string} text
|
||||
* @param {RegExp} nameRe tested against the lower-cased attribute name
|
||||
* @param {(value: string) => string} map
|
||||
*/
|
||||
function attrSpans(text, nameRe, map) {
|
||||
const spans = [];
|
||||
for (const m of text.matchAll(ANY_ATTR)) {
|
||||
if (!nameRe.test((m[1] ?? '').toLowerCase())) continue;
|
||||
const at = m.indices?.[2] ?? m.indices?.[3] ?? m.indices?.[4];
|
||||
const value = m[2] ?? m[3] ?? m[4] ?? '';
|
||||
if (at === undefined || value === '') continue;
|
||||
spans.push({ start: at[0], end: at[1], value: map(value) });
|
||||
}
|
||||
return spans;
|
||||
}
|
||||
|
||||
/** Attributes handled explicitly; the generic URL sweep must not touch them. */
|
||||
const HANDLED_ATTRS = new Set(['href', 'icon']);
|
||||
|
||||
@@ -248,17 +323,18 @@ const HANDLED_ATTRS = new Set(['href', 'icon']);
|
||||
function urlAttrSpans(text, mapUrl) {
|
||||
const spans = [];
|
||||
const rewrite = attrRewriter(mapUrl);
|
||||
for (const m of text.matchAll(/\b([a-z_][a-z0-9_-]*)\s*=\s*"([^"]*)"/gi)) {
|
||||
for (const m of text.matchAll(ANY_ATTR)) {
|
||||
const name = (m[1] ?? '').toLowerCase();
|
||||
const value = m[2] ?? '';
|
||||
const value = m[2] ?? m[3] ?? m[4] ?? '';
|
||||
if (HANDLED_ATTRS.has(name) || value === '') continue;
|
||||
try {
|
||||
if (new URL(value).host === '') continue;
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
const start = m.index + m[0].lastIndexOf(value);
|
||||
spans.push({ start, end: start + value.length, value: rewrite(value) });
|
||||
const at = m.indices?.[2] ?? m.indices?.[3] ?? m.indices?.[4];
|
||||
if (at === undefined) continue;
|
||||
spans.push({ start: at[0], end: at[1], value: rewrite(value) });
|
||||
}
|
||||
return spans;
|
||||
}
|
||||
@@ -267,25 +343,37 @@ function sanitizeHtml(text, mapUrl, mapTitle) {
|
||||
let descriptions = 0;
|
||||
let keywords = 0;
|
||||
const spans = [
|
||||
// HREF="…" and ICON="…" — attribute name case is left exactly as found.
|
||||
...spansFrom(text, /\bhref\s*=\s*"([^"]*)"/gi, attrRewriter(mapUrl)),
|
||||
...spansFrom(text, /\bicon\s*=\s*"([^"]*)"/gi, () => BLANK_ICON),
|
||||
// HREF and ICON, at any quoting — attribute name case is left as found.
|
||||
...attrSpans(text, /^href$/, attrRewriter(mapUrl)),
|
||||
...attrSpans(text, /^icon$/, () => BLANK_ICON),
|
||||
...urlAttrSpans(text, mapUrl),
|
||||
// Firefox keywords and tags are user-authored text, so they are personal
|
||||
// too. Tag count is preserved because it is the only part a parser sees.
|
||||
...spansFrom(text, /\bshortcuturl\s*=\s*"([^"]*)"/gi, () => `kw${++keywords}`),
|
||||
...spansFrom(text, /\btags\s*=\s*"([^"]*)"/gi, (v) =>
|
||||
...attrSpans(text, /^shortcuturl$/, () => `kw${++keywords}`),
|
||||
...attrSpans(text, /^tags$/, (v) =>
|
||||
v
|
||||
.split(',')
|
||||
.map((_, i) => `tag${i + 1}`)
|
||||
.join(','),
|
||||
),
|
||||
// Bookmark titles: the text between <A …> and </A>.
|
||||
...spansFrom(text, new RegExp(`<a\\b${TAG_INNARDS}>([^<]*)</a>`, 'gi'), mapTitle),
|
||||
// `[\s\S]*?` and not `[^<]*`: a title containing a raw `<` made the whole
|
||||
// match fail, so that bookmark's title was left in the output untouched.
|
||||
...spansFrom(text, new RegExp(`<a\\b${TAG_INNARDS}>([\\s\\S]*?)</a>`, 'gi'), mapTitle),
|
||||
// Folder titles.
|
||||
...spansFrom(text, new RegExp(`<h3\\b${TAG_INNARDS}>([^<]*)</h3>`, 'gi'), mapTitle),
|
||||
...spansFrom(text, new RegExp(`<h3\\b${TAG_INNARDS}>([\\s\\S]*?)</h3>`, 'gi'), mapTitle),
|
||||
// Folder/bookmark descriptions.
|
||||
...spansFrom(text, /<dd>([^\n<]*)/gi, () => `Description ${++descriptions}`),
|
||||
// Runs to the next element, not to the end of the line: a description that
|
||||
// wrapped onto a second line left everything after the first newline in
|
||||
// place, and a `<DD>` is exactly where someone writes a private note.
|
||||
...spansFrom(
|
||||
text,
|
||||
// The `\s*` belongs in the lookahead, not the capture: leaving it inside
|
||||
// eats the newline and indentation before the next tag, which is exactly
|
||||
// the byte-level shape this tool promises not to touch.
|
||||
/<dd>([\s\S]*?)(?=\s*<\s*(?:dt|dd|dl|\/dl)\b|\s*$)/gi,
|
||||
() => `Description ${++descriptions}`,
|
||||
),
|
||||
];
|
||||
return replaceSpans(text, spans);
|
||||
}
|
||||
@@ -418,19 +506,59 @@ function mapFolderPath(path, mapTitle) {
|
||||
|
||||
// -------------------------------------------------------------- entry points
|
||||
|
||||
/**
|
||||
* Everything in the OUTPUT that still looks like a real address.
|
||||
*
|
||||
* This is the backstop, and it is the only reason the rules above can be
|
||||
* trusted. They are regexes over untrusted markup, so each one has a shape it
|
||||
* does not match — a single-quoted attribute, a title containing `<`, a JSON
|
||||
* key nobody enumerated, a `<DD>` that wraps onto a second line. Every one of
|
||||
* those is a silent leak, and the only way to find them all by inspection is to
|
||||
* be sure you have thought of every one, which nobody is.
|
||||
*
|
||||
* So instead of trusting the rules, the output is checked: any host that is not
|
||||
* one of our own `*.example` aliases means something got through, and the file
|
||||
* is refused rather than handed over. A rule this misses degrades into a
|
||||
* refusal — annoying — instead of into a published browsing history.
|
||||
*
|
||||
* @param {string} text sanitized output
|
||||
* @returns {string[]} distinct suspicious hosts, empty when clean
|
||||
*/
|
||||
export function findLeakedHosts(text) {
|
||||
const leaked = new Set();
|
||||
for (const m of text.matchAll(/\b[a-z][a-z0-9+.-]*:\/\/([^\s"'<>)\]}\\]+)/gi)) {
|
||||
const authority = (m[1] ?? '').split(/[/?#]/, 1)[0] ?? '';
|
||||
// Strip credentials and port; what remains is the host.
|
||||
const host = authority.split('@').pop()?.replace(/:\d+$/, '').toLowerCase() ?? '';
|
||||
if (host === '' || /^[a-z]\d*\.example$/.test(host)) continue;
|
||||
leaked.add(host);
|
||||
}
|
||||
return [...leaked];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize a bookmark file's text.
|
||||
*
|
||||
* @param {string} text
|
||||
* @returns {{ text: string, kind: 'html' | 'json' | 'csv' }}
|
||||
* @returns {{ text: string | null, kind: 'html' | 'json' | 'csv' | null, leaked: string[] }}
|
||||
* `text` is null when the format was not recognised, or when the output
|
||||
* failed its own leak check. Both mean: write nothing.
|
||||
*/
|
||||
export function sanitize(text) {
|
||||
const kind = detectKind(text);
|
||||
if (kind === null) return { text: null, kind: null, leaked: [] };
|
||||
|
||||
const mapUrl = makeUrlMapper();
|
||||
const mapTitle = makeTitleMapper();
|
||||
if (kind === 'html') return { text: sanitizeHtml(text, mapUrl, mapTitle), kind };
|
||||
if (kind === 'json') return { text: sanitizeJson(text, mapUrl, mapTitle), kind };
|
||||
return { text: sanitizeCsv(text, mapUrl, mapTitle), kind };
|
||||
const out =
|
||||
kind === 'html'
|
||||
? sanitizeHtml(text, mapUrl, mapTitle)
|
||||
: kind === 'json'
|
||||
? sanitizeJson(text, mapUrl, mapTitle)
|
||||
: sanitizeCsv(text, mapUrl, mapTitle);
|
||||
|
||||
const leaked = findLeakedHosts(out);
|
||||
return { text: leaked.length > 0 ? null : out, kind, leaked };
|
||||
}
|
||||
|
||||
function main(argv) {
|
||||
@@ -453,10 +581,33 @@ function main(argv) {
|
||||
}
|
||||
|
||||
const text = raw.toString('utf8');
|
||||
const { text: out, kind } = sanitize(text);
|
||||
const { text: out, kind, leaked } = sanitize(text);
|
||||
|
||||
if (kind === null) {
|
||||
console.error(
|
||||
`${basename(input)} is not a bookmark file this tool recognises, so NOTHING WAS WRITTEN.\n` +
|
||||
' Expected: a Netscape HTML export (the usual bookmarks.html), a BookmarkMagic JSON\n' +
|
||||
' export, or a CSV whose header is exactly "folder_path,title,url,add_date".\n' +
|
||||
' Refusing to hand you a file it cannot promise is clean. Please attach the export\n' +
|
||||
' exactly as your browser wrote it, or describe the problem in the issue instead.',
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (out === null) {
|
||||
console.error(
|
||||
`${basename(input)}: NOTHING WAS WRITTEN — the result still contained ${leaked.length} real ` +
|
||||
`address(es), so this file is not safe to attach.\n Leaked: ${leaked.slice(0, 5).join(', ')}` +
|
||||
`${leaked.length > 5 ? `, and ${leaked.length - 5} more` : ''}\n` +
|
||||
' This is a bug in the sanitizer, not in your file. Please report it (without the file)\n' +
|
||||
' at https://github.com/poli0981/bookmarkmagic/issues — the address above is enough.',
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const eol = text.includes('\r\n') ? 'CRLF' : 'LF';
|
||||
const bom = text.charCodeAt(0) === 0xfeff ? ', BOM' : '';
|
||||
console.error(`${basename(input)}: ${kind}, ${eol}${bom} — ${out.length} bytes out`);
|
||||
console.error(`${basename(input)}: ${kind}, ${eol}${bom} — ${out.length} bytes out, no leaks`);
|
||||
|
||||
if (flags.includes('--check')) return 0;
|
||||
if (flags.includes('--stdout')) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { onMount, untrack } from 'svelte';
|
||||
import {
|
||||
create,
|
||||
getRootChildren,
|
||||
@@ -131,16 +131,26 @@
|
||||
// most of a large tree, and clearing the box used to render every one of
|
||||
// those rows at once, in a single synchronous pass, with no virtualization.
|
||||
$effect(() => {
|
||||
if (!filtering) {
|
||||
if (searchOpened.size === 0) return;
|
||||
expanded = collapseSearchExpansion(expanded, searchOpened);
|
||||
searchOpened = new Set();
|
||||
return;
|
||||
}
|
||||
const merged = mergeSearchExpansion(expanded, search.expand, searchOpened);
|
||||
if (merged.expanded === expanded) return;
|
||||
expanded = merged.expanded;
|
||||
searchOpened = merged.opened;
|
||||
// Depend on the SEARCH only. `expanded` and `searchOpened` are read and
|
||||
// written here, so tracking them makes the effect its own dependency: every
|
||||
// write re-invalidates it, and it re-runs against a `search.expand` that has
|
||||
// not changed. Collapsing a search-opened folder then immediately re-opened
|
||||
// it — the disclosure arrow looked broken, which is the same class of defect
|
||||
// the merge-into-`expanded` design was introduced to fix.
|
||||
const wanted = search.expand;
|
||||
const active = filtering;
|
||||
untrack(() => {
|
||||
if (!active) {
|
||||
if (searchOpened.size === 0) return;
|
||||
expanded = collapseSearchExpansion(expanded, searchOpened);
|
||||
searchOpened = new Set();
|
||||
return;
|
||||
}
|
||||
const merged = mergeSearchExpansion(expanded, wanted, searchOpened);
|
||||
if (merged.expanded === expanded) return;
|
||||
expanded = merged.expanded;
|
||||
searchOpened = merged.opened;
|
||||
});
|
||||
});
|
||||
|
||||
const rows = $derived(visibleRows(roots, expanded, filtering ? search.visible : undefined));
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { describeError } from '../browser/describe-error';
|
||||
import { timestampSuffix } from '../browser/download';
|
||||
import { BmAborted, BmBackupError } from '../browser/errors';
|
||||
import { BmAborted } from '../browser/errors';
|
||||
import { num, t } from '../i18n/index.svelte';
|
||||
import { prepareImport, runImport } from '../import/run-import';
|
||||
import { getSettings } from '../stores/settings.svelte';
|
||||
@@ -60,6 +60,8 @@
|
||||
const signal = beginWrite();
|
||||
const now = new Date();
|
||||
const backupFilename = `bookmarkmagic-backup-${timestampSuffix(now)}.json`;
|
||||
/** Set once the backup is on disk. Nothing may point at the file before. */
|
||||
let backupProven = false;
|
||||
|
||||
// runImport opens the save picker as its first statement, so the transient
|
||||
// activation from this click still holds when the dialog appears.
|
||||
@@ -84,7 +86,11 @@
|
||||
// Backup proven; deletion starts now. No Cancel is offered here — the
|
||||
// button used to stay live through the whole delete, letting a user
|
||||
// cancel and be told "0 items created" after their tree was wiped.
|
||||
//
|
||||
// This callback is also the ONLY signal that the backup exists: it runs
|
||||
// immediately after `proveBackup` returned, and nothing else does.
|
||||
onClearing: () => {
|
||||
backupProven = true;
|
||||
setImportState({ kind: 'clearing', filename });
|
||||
},
|
||||
confirmUnprovenBackup: () => {
|
||||
@@ -95,17 +101,27 @@
|
||||
setImportState({ kind: 'done', filename, created: outcome.created, plan: outcome.plan });
|
||||
} catch (err) {
|
||||
if (err instanceof BmAborted) {
|
||||
setImportState({ kind: 'cancelled', filename, created: err.done });
|
||||
setImportState({
|
||||
kind: 'cancelled',
|
||||
filename,
|
||||
created: err.done,
|
||||
// A cancel AFTER the clear is not "nothing happened": the tree is
|
||||
// gone and only `created` of it has been rebuilt. Saying "N items
|
||||
// created" alone reads as a harmless no-op on an empty browser.
|
||||
...(backupProven ? { backupFilename } : {}),
|
||||
});
|
||||
return;
|
||||
}
|
||||
setImportState({
|
||||
kind: 'error',
|
||||
filename,
|
||||
...describeError(err),
|
||||
// Only when a backup was actually produced. On a BmBackupError the file
|
||||
// may never have been written, and sending a frightened user to look
|
||||
// for a backup that does not exist is worse than saying nothing.
|
||||
...(options.mode === 'replace' && !(err instanceof BmBackupError) && { backupFilename }),
|
||||
// `backupProven`, not "mode was replace and this is not a BmBackupError".
|
||||
// `runImport` resolves the roots BEFORE proving the backup, so a failure
|
||||
// there — a policy-managed profile raising NO_WRITABLE_ROOTS, say —
|
||||
// reaches this catch with no file on disk. Telling a frightened user to
|
||||
// restore a backup that was never written is worse than saying nothing.
|
||||
...(backupProven ? { backupFilename } : {}),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -197,6 +213,10 @@
|
||||
</div>
|
||||
{:else if session.kind === 'cancelled'}
|
||||
<Callout tone="warn">{t('import.cancelledSummary', { created: num(session.created) })}</Callout>
|
||||
{#if session.backupFilename !== undefined}
|
||||
<Callout tone="danger">{t('import.cancelledAfterClear')}</Callout>
|
||||
<p class="muted">{t('import.backupHint', { name: session.backupFilename })}</p>
|
||||
{/if}
|
||||
<p class="muted">{t('import.dateNotice')}</p>
|
||||
<Button
|
||||
onclick={resetImport}>{t('import.another')}</Button
|
||||
|
||||
@@ -49,6 +49,8 @@ const dict = {
|
||||
// chrome.bookmarks.create takes no dateAdded, so every imported bookmark is
|
||||
// stamped with the import time — indistinguishable from corruption to
|
||||
// someone migrating a decade-old archive.
|
||||
cancelledAfterClear:
|
||||
'You cancelled after your existing bookmarks had already been deleted, so what is left is only the part that was rebuilt.',
|
||||
dateNotice:
|
||||
'Imported bookmarks are dated today. Chrome does not let an extension set a bookmark’s original date — your exported files keep the real ones.',
|
||||
partialCreated:
|
||||
|
||||
@@ -38,6 +38,8 @@ export default {
|
||||
skipped:
|
||||
'このブラウザに既にある {existing} 件と、ファイル内で重複する {inFile} 件をスキップしました。',
|
||||
cancelledSummary: 'キャンセルしました — 既に作成された {created} 件はそのまま残されています。',
|
||||
cancelledAfterClear:
|
||||
'既存のブックマークが削除された後にキャンセルされたため、残っているのは再作成された分だけです。',
|
||||
dateNotice:
|
||||
'インポートしたブックマークの日付は本日になります。拡張機能がブックマークの元の作成日を設定することを Chrome が許可していないためです。エクスポートしたファイルには本来の日付が残ります。',
|
||||
partialCreated: 'エラーが発生する前に作成された {created} 件はそのまま残されています。',
|
||||
|
||||
@@ -33,6 +33,8 @@ export default {
|
||||
doneSummary: 'Hoàn tất — đã tạo {created} mục.',
|
||||
skipped: 'Đã bỏ qua {existing} mục đã có trong trình duyệt và {inFile} mục lặp trong tệp.',
|
||||
cancelledSummary: 'Đã hủy — {created} mục đã được tạo trước đó và vẫn được giữ lại.',
|
||||
cancelledAfterClear:
|
||||
'Bạn đã hủy sau khi bookmark cũ đã bị xóa, nên những gì còn lại chỉ là phần đã được tạo lại.',
|
||||
dateNotice:
|
||||
'Bookmark được nhập vào sẽ mang ngày hôm nay. Chrome không cho phép tiện ích đặt lại ngày tạo gốc của bookmark — các tệp bạn xuất ra vẫn giữ ngày thật.',
|
||||
partialCreated: '{created} mục đã được tạo trước khi lỗi xảy ra và vẫn được giữ lại.',
|
||||
|
||||
@@ -31,7 +31,12 @@ export type ImportState =
|
||||
// begun, and nothing in the progress UI needs it.
|
||||
| { kind: 'writing'; filename: string; progress: WriteProgress }
|
||||
| { kind: 'done'; filename: string; created: number; plan: ImportPlan }
|
||||
| { kind: 'cancelled'; filename: string; created: number }
|
||||
/**
|
||||
* `backupFilename` is present only when the cancel landed AFTER the Replace
|
||||
* clear, i.e. the tree really was deleted and only `created` of it rebuilt.
|
||||
* Without it the report reads as a harmless no-op on an empty browser.
|
||||
*/
|
||||
| { kind: 'cancelled'; filename: string; created: number; backupFilename?: string }
|
||||
/**
|
||||
* `messageKey` is a dotted i18n path, NOT a resolved string.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { readdirSync, readFileSync } from 'node:fs';
|
||||
import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { join, relative } from 'node:path';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { detectFormat } from '@/lib/core/detect-format';
|
||||
@@ -25,11 +25,35 @@ const root = process.cwd();
|
||||
const script = join(root, 'scripts', 'sanitize-bookmarks.mjs');
|
||||
const fixtures = join(root, 'tests', 'fixtures');
|
||||
|
||||
const sanitize = (absPath: string): string =>
|
||||
execFileSync(process.execPath, [script, absPath, '--stdout'], {
|
||||
encoding: 'utf8',
|
||||
stdio: ['ignore', 'pipe', 'ignore'],
|
||||
});
|
||||
/**
|
||||
* Run the CLI. `text` is null when it refused — which is a supported outcome,
|
||||
* not a failure: refusing is what it does with anything it cannot positively
|
||||
* identify, or whose output still contains a real address.
|
||||
*/
|
||||
function sanitize(absPath: string): { text: string | null; status: number } {
|
||||
try {
|
||||
const text = execFileSync(process.execPath, [script, absPath, '--stdout'], {
|
||||
encoding: 'utf8',
|
||||
stdio: ['ignore', 'pipe', 'ignore'],
|
||||
maxBuffer: 64 * 1024 * 1024,
|
||||
});
|
||||
return { text, status: 0 };
|
||||
} catch (err) {
|
||||
const status = (err as { status?: number }).status ?? 1;
|
||||
return { text: null, status };
|
||||
}
|
||||
}
|
||||
|
||||
/** Hosts that appear anywhere in the raw text, however the file is structured. */
|
||||
function hostsInText(text: string): string[] {
|
||||
const out = new Set<string>();
|
||||
for (const m of text.matchAll(/\b[a-z][a-z0-9+.-]*:\/\/([^\s"'<>)\]}\\]+)/gi)) {
|
||||
const authority = (m[1] ?? '').split(/[/?#]/, 1)[0] ?? '';
|
||||
const host = authority.split('@').pop()?.replace(/:\d+$/, '').toLowerCase() ?? '';
|
||||
if (host !== '') out.add(host);
|
||||
}
|
||||
return [...out];
|
||||
}
|
||||
|
||||
/**
|
||||
* Every committed fixture file, as paths relative to `tests/fixtures/`.
|
||||
@@ -70,52 +94,51 @@ function topology(roots: readonly BookmarkNode[]): string {
|
||||
return render(roots);
|
||||
}
|
||||
|
||||
const hostsIn = (roots: readonly BookmarkNode[]): string[] => {
|
||||
const out: string[] = [];
|
||||
for (const { node } of walkTree(roots)) {
|
||||
if (node.url === undefined) continue;
|
||||
try {
|
||||
const { host } = new URL(node.url);
|
||||
if (host !== '') out.push(host);
|
||||
} catch {
|
||||
// Unparseable URLs carry no host to leak.
|
||||
}
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
it('the corpus is non-empty, so a broken walk cannot pass vacuously', () => {
|
||||
expect(FIXTURES.length).toBeGreaterThan(10);
|
||||
});
|
||||
|
||||
describe.each(FIXTURES)('sanitize %s', (name) => {
|
||||
const original = readFileSync(join(fixtures, name), 'utf8');
|
||||
const cleaned = sanitize(join(fixtures, name));
|
||||
const { text: cleaned, status } = sanitize(join(fixtures, name));
|
||||
|
||||
it('keeps the file recognisable as the same format', () => {
|
||||
let before: string;
|
||||
it('either sanitizes, or refuses — and refuses exactly what the parser rejects', () => {
|
||||
// The two must agree. A file the extension itself cannot read is one this
|
||||
// tool cannot promise to have cleaned, and the old fallback guessed "csv",
|
||||
// matched no columns, and emitted the input verbatim while reporting
|
||||
// success — publishing a real bookmark set to a public issue.
|
||||
let parserAccepts = true;
|
||||
try {
|
||||
before = detectFormat(original);
|
||||
detectFormat(original);
|
||||
} catch {
|
||||
// A fixture that is deliberately unrecognisable must stay unrecognisable.
|
||||
expect(() => detectFormat(cleaned)).toThrow();
|
||||
return;
|
||||
parserAccepts = false;
|
||||
}
|
||||
if (parserAccepts) {
|
||||
expect(status).toBe(0);
|
||||
expect(cleaned).not.toBeNull();
|
||||
} else {
|
||||
expect(status).toBe(1);
|
||||
expect(cleaned).toBeNull();
|
||||
}
|
||||
expect(detectFormat(cleaned)).toBe(before);
|
||||
});
|
||||
|
||||
it('parses to the same stats, warnings and structure — or fails the same way', () => {
|
||||
let before: ParseResult;
|
||||
try {
|
||||
before = parseAny(original);
|
||||
} catch (err) {
|
||||
// The interesting half: a fixture that reproduces a parse *failure* must
|
||||
// still reproduce it, with the same code, after sanitizing.
|
||||
const code = (err as { code?: string }).code;
|
||||
expect(() => parseAny(cleaned)).toThrowError(expect.objectContaining({ code }));
|
||||
return;
|
||||
}
|
||||
it('leaks no hostname from the original', () => {
|
||||
// Asserted on the raw TEXT, not on a parse. The previous version returned
|
||||
// early whenever the original failed to parse, so it passed vacuously on
|
||||
// precisely the inputs that leaked.
|
||||
if (cleaned === null) return;
|
||||
const before = hostsInText(original);
|
||||
expect(before.filter((host) => cleaned.includes(host))).toEqual([]);
|
||||
});
|
||||
|
||||
it('keeps the file recognisable as the same format', () => {
|
||||
if (cleaned === null) return;
|
||||
expect(detectFormat(cleaned)).toBe(detectFormat(original));
|
||||
});
|
||||
|
||||
it('parses to the same stats, warnings and structure', () => {
|
||||
if (cleaned === null) return;
|
||||
const before = parseAny(original);
|
||||
const after = parseAny(cleaned);
|
||||
expect(after.stats).toEqual(before.stats);
|
||||
expect(computeStats(after.roots)).toEqual(computeStats(before.roots));
|
||||
@@ -125,41 +148,108 @@ describe.each(FIXTURES)('sanitize %s', (name) => {
|
||||
);
|
||||
});
|
||||
|
||||
it('preserves byte-level shape: BOM, line endings and length of the untouched parts', () => {
|
||||
it('preserves byte-level shape: BOM, line endings and line count', () => {
|
||||
if (cleaned === null) return;
|
||||
expect(cleaned.charCodeAt(0) === 0xfeff).toBe(original.charCodeAt(0) === 0xfeff);
|
||||
expect(cleaned.includes('\r\n')).toBe(original.includes('\r\n'));
|
||||
expect(cleaned.split('\n').length).toBe(original.split('\n').length);
|
||||
});
|
||||
|
||||
it('preserves every timestamp exactly', () => {
|
||||
if (cleaned === null) return;
|
||||
const stamps = (text: string): string[] =>
|
||||
(text.match(/(?:ADD_DATE|LAST_MODIFIED)="(\d*)"/gi) ?? []).sort();
|
||||
expect(stamps(cleaned)).toEqual(stamps(original));
|
||||
});
|
||||
|
||||
it('leaks no hostname from the original', () => {
|
||||
let before: ParseResult;
|
||||
try {
|
||||
before = parseAny(original);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const leaked = hostsIn(before.roots).filter((h) => cleaned.includes(h));
|
||||
expect(leaked).toEqual([]);
|
||||
});
|
||||
|
||||
it('keeps distinct URLs distinct, so dedupe behaviour is unchanged', () => {
|
||||
let before: ParseResult;
|
||||
try {
|
||||
before = parseAny(original);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const urls = (r: ParseResult): number => {
|
||||
if (cleaned === null) return;
|
||||
const urls = (text: string): number => {
|
||||
const set = new Set<string>();
|
||||
for (const { node } of walkTree(r.roots)) if (node.url !== undefined) set.add(node.url);
|
||||
for (const { node } of walkTree(parseAny(text).roots)) {
|
||||
if (node.url !== undefined) set.add(node.url);
|
||||
}
|
||||
return set.size;
|
||||
};
|
||||
expect(urls(parseAny(cleaned))).toBe(urls(before));
|
||||
expect(urls(cleaned)).toBe(urls(original));
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* The refusal paths — every one of these was a silent leak found by review.
|
||||
*
|
||||
* The shared property: when a rule does not reach some byte, the tool must
|
||||
* refuse rather than hand over a file the user has been told is clean. The
|
||||
* rules are regexes over untrusted markup, so "we thought of every shape" is
|
||||
* not a claim anyone can make honestly; the output check is what makes that
|
||||
* survivable.
|
||||
*/
|
||||
describe('refuses rather than leaking', () => {
|
||||
const tmp = join(root, 'tests', 'fixtures', 'generated');
|
||||
|
||||
const run = (name: string, content: string): { text: string | null; status: number } => {
|
||||
mkdirSync(tmp, { recursive: true });
|
||||
const path = join(tmp, name);
|
||||
writeFileSync(path, content);
|
||||
try {
|
||||
return sanitize(path);
|
||||
} finally {
|
||||
rmSync(path, { force: true });
|
||||
}
|
||||
};
|
||||
|
||||
it.each([
|
||||
[
|
||||
'a truncated JSON export',
|
||||
'broken.json',
|
||||
'{"format":"bookmarkmagic","roots":[{"title":"x","url":"https://clinic.internal/a"',
|
||||
],
|
||||
[
|
||||
'an HTML excerpt with no doctype and no <DL>',
|
||||
'excerpt.html',
|
||||
'<DT><A HREF="https://clinic.internal/a">Appointment</A>',
|
||||
],
|
||||
[
|
||||
'a CSV with foreign headers',
|
||||
'foreign.csv',
|
||||
'folder,name,link\n"m","x","https://clinic.internal/a"\n',
|
||||
],
|
||||
])('refuses %s', (_label, name, content) => {
|
||||
const { text, status } = run(name, content);
|
||||
expect(status).toBe(1);
|
||||
expect(text).toBeNull();
|
||||
});
|
||||
|
||||
it('catches a leak the rewriting rules missed, instead of emitting it', () => {
|
||||
// Single-quoted attributes were not matched at all. The rule is fixed, but
|
||||
// the point of this test is the backstop: even with the rule removed, the
|
||||
// output check would refuse rather than publish `clinic.internal`.
|
||||
const { text, status } = run(
|
||||
'quoted.html',
|
||||
"<!DOCTYPE NETSCAPE-Bookmark-file-1>\n<DL><p>\n<DT><A HREF='https://clinic.internal/a'>x</A>\n</DL><p>\n",
|
||||
);
|
||||
expect(status).toBe(0);
|
||||
expect(text).not.toBeNull();
|
||||
expect(text).not.toContain('clinic.internal');
|
||||
});
|
||||
|
||||
it('sanitizes a bookmark whose title contains a raw <', () => {
|
||||
// `[^<]*` made the whole match fail, so the title was left in place.
|
||||
const { text } = run(
|
||||
'angle.html',
|
||||
'<!DOCTYPE NETSCAPE-Bookmark-file-1>\n<DL><p>\n<DT><A HREF="https://a.invalid/">Dr Smith < oncology</A>\n</DL><p>\n',
|
||||
);
|
||||
expect(text).not.toBeNull();
|
||||
expect(text).not.toContain('oncology');
|
||||
});
|
||||
|
||||
it('sanitizes a <DD> description that wraps onto a second line', () => {
|
||||
// The capture stopped at the first newline, publishing the rest.
|
||||
const { text } = run(
|
||||
'dd.html',
|
||||
'<!DOCTYPE NETSCAPE-Bookmark-file-1>\n<DL><p>\n<DT><H3>F</H3>\n<DD>first line\nsecond line with dr-smith\n<DL><p></DL><p>\n</DL><p>\n',
|
||||
);
|
||||
expect(text).not.toBeNull();
|
||||
expect(text).not.toContain('dr-smith');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user