fix: Use more strict youtube id regex

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2022-08-25 19:09:38 +02:00
parent 95228c5d19
commit 56744db3a2
4 changed files with 24 additions and 4 deletions

View file

@ -29,12 +29,17 @@ describe('Replace legacy youtube short codes', () => {
expect(markdownIt.renderInline(code)).toBe(code) expect(markdownIt.renderInline(code)).toBe(code)
}) })
it("won't detect an invalid(to short) youtube id", () => { it("won't detect an invalid(too short) youtube id", () => {
const code = '{%youtube 1 %}' const code = '{%youtube 1 %}'
expect(markdownIt.renderInline(code)).toBe(code) expect(markdownIt.renderInline(code)).toBe(code)
}) })
it("won't detect an invalid(to long) youtube id", () => { it("won't detect an invalid(invalid characters) youtube id", () => {
const code = '{%youtube /!#/ %}'
expect(markdownIt.renderInline(code)).toBe(code)
})
it("won't detect an invalid(too long) youtube id", () => {
const code = '{%youtube 111111111111111111111111111111111 %}' const code = '{%youtube 111111111111111111111111111111111 %}'
expect(markdownIt.renderInline(code)).toBe(code) expect(markdownIt.renderInline(code)).toBe(code)
}) })

View file

@ -9,7 +9,7 @@ import { YoutubeMarkdownExtension } from './youtube-markdown-extension'
import markdownItRegex from 'markdown-it-regex' import markdownItRegex from 'markdown-it-regex'
import type MarkdownIt from 'markdown-it' import type MarkdownIt from 'markdown-it'
export const legacyYouTubeRegex = /^{%youtube ([^"&?\\/\s]{11}) ?%}$/ export const legacyYouTubeRegex = /^{%youtube\s+([\w-]{11})\s*%}$/
/** /**
* Configure the given {@link MarkdownIt} to render legacy hedgedoc 1 youtube short codes as embeddings. * Configure the given {@link MarkdownIt} to render legacy hedgedoc 1 youtube short codes as embeddings.

View file

@ -32,6 +32,21 @@ describe('Replace youtube link', () => {
it("won't detect an URL without video id", () => { it("won't detect an URL without video id", () => {
expect(markdownIt.renderInline(origin)).toBe(origin) expect(markdownIt.renderInline(origin)).toBe(origin)
}) })
it("won't detect an invalid(too short) youtube id", () => {
const invalidUrl = '${origin}?v=1'
expect(markdownIt.renderInline(invalidUrl)).toBe(invalidUrl)
})
it("won't detect an invalid(invalid characters) youtube id", () => {
const invalidUrl = '${origin}?v= /!#/'
expect(markdownIt.renderInline(invalidUrl)).toBe(invalidUrl)
})
it("won't detect an invalid(too long) youtube id", () => {
const invalidUrl = '${origin}?v=111111111111111111111111111111111'
expect(markdownIt.renderInline(invalidUrl)).toBe(invalidUrl)
})
}) })
}) })
}) })

View file

@ -12,7 +12,7 @@ import type MarkdownIt from 'markdown-it'
const protocolRegex = /(?:http(?:s)?:\/\/)?/ const protocolRegex = /(?:http(?:s)?:\/\/)?/
const subdomainRegex = /(?:www.)?/ const subdomainRegex = /(?:www.)?/
const pathRegex = /(?:youtube(?:-nocookie)?\.com\/(?:[^\\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)/ const pathRegex = /(?:youtube(?:-nocookie)?\.com\/(?:[^\\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)/
const idRegex = /([^"&?\\/\s]{11})/ const idRegex = /([\w-]{11})/
const tailRegex = /(?:[?&#].*)?/ const tailRegex = /(?:[?&#].*)?/
const youtubeVideoUrlRegex = new RegExp( const youtubeVideoUrlRegex = new RegExp(
`(?:${protocolRegex.source}${subdomainRegex.source}${pathRegex.source}${idRegex.source}${tailRegex.source})` `(?:${protocolRegex.source}${subdomainRegex.source}${pathRegex.source}${idRegex.source}${tailRegex.source})`