Repository navigation
Conversation
…e object payload too
## Description
`TOGGLE_LINK_COMMAND` accepts either a URL string or an object carrying the URL plus link attributes. The handler validates one of them:
```ts
} else if (typeof payload === 'string') {
if (validateUrl === undefined || validateUrl(payload)) {
$toggleLink(payload, attributes);
return true;
}
return false; // string: gated
} else {
const {url, ...payloadAttributes} = payload;
$toggleLink(url, {...attributes, ...payloadAttributes});
return true; // object: validateUrl never called
}
```
`$toggleLink` does no validation of its own — it only reshuffles `url`/`attributes` and edits the selection — so nothing downstream compensates.
The contract is stated in `LinkConfig` in this same file, and it is written against the command, not against one of its shapes:
> In the implementation of {@link TOGGLE_LINK_COMMAND} it will reject URLs that return false when specified. The default of `undefined` will always accept URLs.
The object payload is the shape you are pushed toward as soon as you need to set `target`, `rel` or `title` at the same time as the URL — which is the common case for a link dialog. An app that configures `validateUrl` to enforce its own policy (an allowlist of internal hosts, a scheme allowlist, a length or shape rule) has that policy silently applied to `dispatchCommand(TOGGLE_LINK_COMMAND, url)` and silently skipped for `dispatchCommand(TOGGLE_LINK_COMMAND, {url, target: '_blank'})`. The same rejected URL is accepted or refused depending only on which shape the caller used.
To be precise about severity: this is a policy bypass, not by itself an XSS. `LinkNode.updateLinkDOM` runs `sanitizeUrl` when it writes `href`, so a dangerous scheme still renders as `about:blank` regardless. What escapes is the application's own rule — typically an allowlist rather than a scheme blocklist. The node is created, and `exportJSON` persists the raw URL the app said it did not want.
The fix applies the same gate in the object branch. Nothing in the repo dispatches an object payload except the `PASTE_COMMAND` handler a few lines below, which already calls `validateUrl(clipboardText)` before dispatching, so it re-validates the same string and passes unchanged.
## Test plan
New unit test `packages/lexical-link/src/__tests__/unit/LinkExtensionValidateUrl.test.ts` (a separate file from `LinkExtension.test.ts`). Three of the four cases are controls that pass before and after — in particular the string-payload case, which pins the asymmetry: the same URL, the same config, only the payload shape differs.
### Before
```
❯ packages/lexical-link/src/__tests__/unit/LinkExtensionValidateUrl.test.ts (4 tests | 1 failed)
× rejects a URL the validator refuses, for an object payload
AssertionError: expected { handled: true, linked: true } to deeply equal { handled: false, linked: false }
✓ rejects a URL the validator refuses, for a string payload
✓ accepts a URL the validator allows, for an object payload
✓ accepts any URL when no validator is configured
Test Files 1 failed (1)
Tests 1 failed | 3 passed (4)
```
### After
```
Test Files 1 passed (1)
Tests 4 passed (4)
```
Package suite is unchanged, including the paste-to-link tests that route through the object payload:
```
$ npx vitest run packages/lexical-link
Test Files 7 passed (7)
Tests 162 passed (162)
```
|
@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel. A member of the Team first needs to authorize it. |
|
Confirmed the bug on |
| }); | ||
| }); | ||
|
|
||
| it('accepts a URL the validator allows, for an object payload', () => { |
There was a problem hiding this comment.
The truth table has string×rejected, object×rejected, object×accepted, and both×no-validator — but not string×accepted. The existing LinkExtension.test.ts uses validateUrl: () => true so it doesn't fill the gap either. Since this PR frames the string branch as the reference the object branch now mirrors, the accepted case for strings should be pinned here too:
it('accepts a URL the validator allows, for a string payload', () => {
expect(toggleLink(ALLOWED, isHttps)).toEqual({handled: true, linked: true});
});| /** | ||
| * Dispatch the given payload over a selection of "Hello" and report whether | ||
| * the command claimed it and whether a LinkNode was actually produced. | ||
| */ |
There was a problem hiding this comment.
The neighboring helpers in LinkExtension.test.ts ($selectHello, $getLink, dispatchPaste) have no JSDoc. The function name and signature are self-descriptive here too.
Description
TOGGLE_LINK_COMMANDaccepts either a URL string or an object carrying the URL plus link attributes. The handler validates one of them:$toggleLinkdoes no validation of its own — it only reshufflesurl/attributesand edits the selection — so nothing downstream compensates.The contract is stated in
LinkConfigin this same file, and it is written against the command, not against one of its shapes:The object payload is the shape you are pushed toward as soon as you need to set
target,relortitleat the same time as the URL — which is the common case for a link dialog. An app that configuresvalidateUrlto enforce its own policy (an allowlist of internal hosts, a scheme allowlist, a length or shape rule) has that policy silently applied todispatchCommand(TOGGLE_LINK_COMMAND, url)and silently skipped fordispatchCommand(TOGGLE_LINK_COMMAND, {url, target: '_blank'}). The same rejected URL is accepted or refused depending only on which shape the caller used.To be precise about severity: this is a policy bypass, not by itself an XSS.
LinkNode.updateLinkDOMrunssanitizeUrlwhen it writeshref, so a dangerous scheme still renders asabout:blankregardless. What escapes is the application's own rule — typically an allowlist rather than a scheme blocklist. The node is created, andexportJSONpersists the raw URL the app said it did not want.The fix applies the same gate in the object branch. Nothing in the repo dispatches an object payload except the
PASTE_COMMANDhandler a few lines below, which already callsvalidateUrl(clipboardText)before dispatching, so it re-validates the same string and passes unchanged.Test plan
New unit test
packages/lexical-link/src/__tests__/unit/LinkExtensionValidateUrl.test.ts(a separate file fromLinkExtension.test.ts). Three of the four cases are controls that pass before and after — in particular the string-payload case, which pins the asymmetry: the same URL, the same config, only the payload shape differs.Before
After
Package suite is unchanged, including the paste-to-link tests that route through the object payload: