Skip to content

[lexical-link] Bug Fix: TOGGLE_LINK_COMMAND applies validateUrl to the object payload too - #9046

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/link-validate-url-object-payload
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/link-validate-url-object-payload

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

TOGGLE_LINK_COMMAND accepts either a URL string or an object carrying the URL plus link attributes. The handler validates one of them:

} 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)

…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)
```
@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

@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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 9, 2026
@mayrang

mayrang commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

Confirmed the bug on main (1 failure: object payload bypasses validateUrl) and verified all 4 tests pass on this branch. The fix is correct and mirrors the string branch exactly. One test coverage note and one doc note inline.

});
});

it('accepts a URL the validator allows, for an object payload', () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
*/

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The neighboring helpers in LinkExtension.test.ts ($selectHello, $getLink, dispatchPaste) have no JSDoc. The function name and signature are self-descriptive here too.

@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9060 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

@LeSingh1 LeSingh1 closed this Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants