-
-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Harden media embed element against XSS
- Loading branch information
Showing
3 changed files
with
73 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@udecode/plate-media": patch | ||
--- | ||
|
||
- Explicitly prohibit `javascript:` protocol when parsing URLs in `useMediaState`. | ||
- In the return value of `useMediaState`, rename `url` to `unsafeUrl` to indicate that it has not been sanitised. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { type EmbedUrlParser, parseMediaUrl } from './useMediaState'; | ||
|
||
describe('parseMediaUrl', () => { | ||
const parsersWithoutFallback: EmbedUrlParser[] = [ | ||
(url) => (url.startsWith('a') ? { id: 'A' } : undefined), | ||
(url) => (url.endsWith('b') ? { id: 'B' } : undefined), | ||
]; | ||
|
||
const parsersWithFallback: EmbedUrlParser[] = [ | ||
...parsersWithoutFallback, | ||
() => ({ id: 'C' }), | ||
]; | ||
|
||
it('returns undefined if no parsers match', () => { | ||
const embed = parseMediaUrl('x', { urlParsers: parsersWithoutFallback }); | ||
expect(embed).toBeUndefined(); | ||
}); | ||
|
||
it('uses the first matching parser', () => { | ||
const embed = parseMediaUrl('ab', { urlParsers: parsersWithoutFallback }); | ||
expect(embed?.id).toBe('A'); | ||
}); | ||
|
||
it('uses fallback parser if present', () => { | ||
const embed = parseMediaUrl('javascript', { | ||
urlParsers: parsersWithFallback, | ||
}); | ||
expect(embed?.id).toBe('C'); | ||
}); | ||
|
||
it('does not allow javascript: URLs', () => { | ||
const embed = parseMediaUrl('javascript:', { | ||
urlParsers: parsersWithFallback, | ||
}); | ||
expect(embed).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters