Repository navigation
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
@blocknote/ariakit
@blocknote/code-block
@blocknote/core
@blocknote/diagram-block
@blocknote/mantine
@blocknote/math-block
@blocknote/react
@blocknote/server-util
@blocknote/shadcn
@blocknote/xl-ai
@blocknote/xl-docx-exporter
@blocknote/xl-email-exporter
@blocknote/xl-multi-column
@blocknote/xl-odt-exporter
@blocknote/xl-pdf-exporter
@blocknote/xl-typst-exporter
commit: |
|
nperez0111
left a comment
There was a problem hiding this comment.
I like enforcing all the callsites, let's go one step further into a lint rule
|
|
||
| // Cloned so merging doesn't modify the element being parsed. | ||
| const clone = (node as HTMLElement).cloneNode(true) as HTMLElement; | ||
| // Not `cloneNode`: see the rule in `util/detachedDoc.ts`. |
There was a problem hiding this comment.
Could we add a lint rule for this?
| * HTML or markdown string, the HTML of an export). It has no browsing context: | ||
| * content in it cannot load a resource or run an event handler. | ||
| * | ||
| * Do all DOM work on such content in this document, never in the global |
There was a problem hiding this comment.
let's do a lint rule for this stuff
| // | Where | Not allowed | | ||
| // | ---------------------------------------- | ----------------------------------- | | ||
| // | 1. each file | `.cloneNode`, `.createHTMLDocument` | | ||
| // | 2. a file that only handles such content | global `document`, `.ownerDocument` | |
There was a problem hiding this comment.
@nperez0111 this is a bit tricky. We can have a lint rule that prvents rule 1 on every file. But we can't block all access to document or ownerDocument, because in some cases (e.g.: render() or toExternalHTML this is valid). We only want to prevent it on code that parses outside content.
Rules 2 and 3 try to prevent that (by blocking document for specific files and for any file inside a parse* function; but maybe we should drop rule 2 and 3 altogether as we already know it can't be 100% accurate (it would miss other filenames or function names). wdyt?
There was a problem hiding this comment.
I think we've been bitten by the whole accessing document thing a couple of times (relative to shadow-dom & iframe usage), so I actually would not be opposed to a blanket rule against touching document & then suppressing the rule on the line for the things we know that need them. This would sort of encourage us to be more intentional about when to use document & give a little friction which would be good to second guess it's usage if we can do it another way.
So maybe just apply the rule everywhere & suppress where needed
There was a problem hiding this comment.
check. even if it's like 30 places?
There was a problem hiding this comment.
render & toExternalHTML & parse can be rewritten (which I think is actually more valid). For example:
render (block, editor) {
document.createElement('h1')
// vs.
editor.domElement.ownerDocument.createElement('h1')
}
// we should make that less verbose
parse (el) {
el.querySelector()
el.ownerDocument.createElement()
}I think it is more accurate since it will always be in the correct "realm" if done like this since they will be relative to their inputs rather than having to pull in globals. But maybe this is too pedantic? I'll leave it to you. I would prefer if we could remove certain classes of errors (since it can be hard to know when it is important for shadow-dom or not, and we don't really have that many tests for shadow-dom).
There was a problem hiding this comment.
Not sure your exact example holds up (for example editor.domElement doesn't exist in headless editors), so it would require more investigation. I agree it would be great to better standardize how we access document safely. Atm though I think it'd be at least a day of work to come up with a good pattern and fix 135 spots (my 30 places earlier was too low).
I'd propose to get this PR in as it fixes some current potential security issues. I would drop lint rule 2 and 3 for now and create an engineering task to investigate all uses of document
There was a problem hiding this comment.
alright sure, fine with deferring
Content from outside the editor must not get into the active document. The code had two forms for this: `detachedDoc()` in `nestedLists.ts`, and a new `createHTMLDocument` for each call in `normalizeOfficeHTML.ts`. `detachedDoc()` moved to `util/detachedDoc.ts`, and the two files use it. `normalizeOfficeHTML.ts` makes all its elements with it: the tree that it changes is in that document. The JSDoc of `detachedDoc` states the rule for content from outside. No behavior changes: the documents are of the same kind, and a caller only makes elements with the shared document.
The HTML parser reads HTML in a document without a browsing context, so that an element of the HTML cannot load a resource or run an event handler. `getListItemContent` made its work `div` with the active `document` and put clones of the children of the `li` element into it. Thus these clones were in the active document. The work `div` is now an element of `detachedDoc()`, and each copy is made with `detachedDoc().importNode`. A clone is in the document of its source, and the caller controls that document. One result of this: the URL of an image in a list item is the URL as the HTML has it. It was the URL after a resolve with the URL of the page. Five parse snapshots had that form.
… document The HTML parser reads HTML in a document without a browsing context, so that an element of the HTML cannot load a resource or run an event handler. `getDetailsContent` made its work `div` with the active `document` and put clones of the children of the `details` element into it. Thus these clones were in the active document. This is the same fault as in `getListItemContent`, for a toggle list item and for a toggle heading. The work `div` is now an element of `detachedDoc()`, and each copy (also the copy of the `summary` element) is made with `detachedDoc().importNode`. One result of this: the URL of an image in a `details` element is the URL as the HTML has it. It was the URL after a resolve with the URL of the page. One parse snapshot had that form.
…ment Before the parse, `nestedListsToBlockNoteStructure` changes the list structure of the HTML. The HTML is in a document without a browsing context, so that an element of the HTML cannot load a resource or run an event handler. For content that comes after a nested list in a list item, the function made an `li` with the active `document`, put the content into it, and then put the `li` into the HTML. Thus the content was in the active document for a moment. An image in that content started its request at that time: an `img` element in WebKit, an SVG `image` element and an image `input` element in Firefox. No event handler ran. The `li` is now an element of `detachedDoc()`. The two `div` elements of `createGroups` did not have this effect, because they go into the HTML before they get content. They are now also elements of `detachedDoc()`, so that the file has one rule. That rule is only correct for a tree that is in `detachedDoc()`. Thus `nestedListsToBlockNoteStructure` now takes only the HTML string, and parses it itself. No caller gave it an element.
…ment `htmlToMarkdown` parses the HTML of the external HTML exporter and makes markdown from its elements. It set the HTML as the `innerHTML` of a `div` of the active `document`. In the active document, an element of the HTML can load a resource and run an event handler. Thus each `video` and each `audio` element of the export started a second request, on a copy, on a drag of a block and on a markdown export. `cleanHTMLToMarkdown` is an export of the package: with HTML that does not come from the exporter, an element could also run its event handler. The `div` is now an element of `detachedDoc()`, as in the HTML parser. The markdown is the same.
A parse rule does not control the document of the node that it gets: the caller does. The content parser of a block spec made its work copy with `cloneNode`, and a clone is in the document of its source. For a node of the active document, the clone loads its resource and runs its event handler one more time. The content parser now makes the copy with `detachedDoc().importNode(node, true)`, as `getListItemContent` and `getDetailsContent` do. The result of a parse is the same. The test gives the parse rules HTML that is in the active document: they make no element and no clone in that document.
The rule of `util/detachedDoc.ts` was only in its JSDoc. Now the lint enforces it for the source files of `@blocknote/core`, with one rule of a local Oxlint plugin: `blocknote-dom/no-active-document` in `scripts/lint/detached-doc.mjs`. The rule has three checks: - In each file: no `cloneNode` and no `createHTMLDocument`. A parse rule gets a node from outside, and a parse rule can be in each file. - In the files that only handle content from outside (the parsers, the paste handlers, the markdown export, the parse helpers of the blocks): also no global `document` and no `ownerDocument`. Other files render, and make elements in the active document on purpose. - In a parse function of each file (a function with a name that starts with `parse` or contains `Parse`, and the functions in it): also no global `document` and no `ownerDocument`. Oxlint has no built-in rule that a configuration can limit to a function, and an override replaces the options of a built-in rule. Thus the whole policy is in the plugin: `vite.config.ts` has only the plugin entry and one override that turns the rule on. The explanation is in the header of the plugin and in `scripts/lint/README.md`. The plugin has a test for `node --test`. Seven lines are correct and have an exception with its reason: the one `createHTMLDocument` of `detachedDoc()`, the copies of the editor's own DOM for the drag preview and for the HTML export, and two TreeWalkers (they only read).
31f2407 to
ac056a2
Compare
Summary
The HTML parser of
@blocknote/corereads HTML in a document without a browsing context. There, an element cannot load a resource or run an event handler. In four places, the code put content of that HTML into the active document of the page, where both can occur. Thus HTML from a paste, or given totryParseHTMLToBlocks,pasteHTMLorpasteMarkdown, could run script in the page of the editor.Each fix is one commit. The element that gets the content is now in a document without a browsing context.
getListItemContent).detailselement (getDetailsContent): toggle list items and toggle headings.nestedListsToBlockNoteStructure). This place only started an image request, in WebKit and Firefox.htmlToMarkdown). This place started a second request for each video and audio block on a copy, a block drag and a markdown export.The last two commits make the pattern one rule, and the result of a parse stays the same. All DOM work on content from outside is in
detachedDoc(), one shared document without a browsing context (an internal helper, with the rule in its JSDoc): the code parses a string there, makes more elements there, and copies a node that it gets withdetachedDoc().importNode. Thus a parse helper does not depend on the document of the node that its caller gives it.Testing
@blocknote/corein Chromium, Firefox and WebKit: 2,228 passed (main: 2,213). The browser integration tests (2,343) and the Node tests pass as onmain.Notes
detailselement stays as the HTML has it. Before, it became an absolute URL with the URL of the page. Six parse snapshots change for this reason.blocknote-dom/no-active-document, inscripts/lint/detached-doc.mjs) enforces the rule ofutil/detachedDoc.tsfor the source of@blocknote/core. It has three checks: nocloneNodeand nocreateHTMLDocumentin each file; no globaldocumentand noownerDocumentin the files that only handle content from outside (parsers, paste handlers, markdown export, parse helpers); and the same in each parse function, which is a function with a name that starts withparseor containsParse. Seven correct uses have an exception with its reason.vite.config.tsonly turns the rule on.Summary by CodeRabbit