Skip to content

feat: santinize url input #1808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"@types/lodash.throttle": "^4.1.9",
"atomico": "^1.75.1",
"clsx": "^2.0.0",
"dompurify": "^3.2.5",
"lodash.debounce": "^4.0.8",
"lodash.throttle": "^4.1.1",
"nanoid": "^5.0.9",
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/__internal__/components/icon.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import clsx from 'clsx'
import DOMPurify from 'dompurify'
import { h } from 'vue'

h
Expand All @@ -16,7 +17,7 @@ export function Icon({ icon, class: className, onClick }: IconProps) {
onPointerdown={onClick}
ref={(el) => {
if (el && icon) {
;(el as HTMLElement).innerHTML = icon.trim()
;(el as HTMLElement).innerHTML = DOMPurify.sanitize(icon.trim())
}
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from 'vue'
import type { CodeBlockProps } from './code-block'
import clsx from 'clsx'
import DOMPurify from 'dompurify'

h
Fragment
Expand Down Expand Up @@ -58,10 +59,11 @@ export const PreviewPanel = defineComponent<PreviewPanelProps>({

const previewContent = preview.value

if (typeof previewContent === 'string') {
previewContainer.innerHTML = previewContent
} else if (previewContent instanceof HTMLElement) {
previewContainer.appendChild(previewContent)
if (
typeof previewContent === 'string' ||
previewContent instanceof Element
) {
previewContainer.innerHTML = DOMPurify.sanitize(previewContent)
}
})

Expand Down
9 changes: 8 additions & 1 deletion packages/components/src/image-block/view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { imageBlockConfig } from '../config'
import { withMeta } from '../../__internal__/meta'
import { createApp, ref, watchEffect } from 'vue'
import { MilkdownImageBlock } from './components/image-block'
import DOMPurify from 'dompurify'

export const imageBlockView = $view(
imageBlockSchema.node,
Expand All @@ -19,7 +20,13 @@ export const imageBlockView = $view(
const setAttr = (attr: string, value: unknown) => {
const pos = getPos()
if (pos == null) return
view.dispatch(view.state.tr.setNodeAttribute(pos, attr, value))
view.dispatch(
view.state.tr.setNodeAttribute(
pos,
attr,
attr === 'src' ? DOMPurify.sanitize(value as string) : value
)
)
}
const config = ctx.get(imageBlockConfig.key)
const app = createApp(MilkdownImageBlock, {
Expand Down
13 changes: 11 additions & 2 deletions packages/components/src/image-inline/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { $view } from '@milkdown/utils'
import type { NodeViewConstructor } from '@milkdown/prose/view'
import { imageSchema } from '@milkdown/preset-commonmark'
import type { Node } from '@milkdown/prose/model'
import { createApp, ref, watchEffect } from 'vue'
import DOMPurify from 'dompurify'

import { withMeta } from '../__internal__/meta'
import { inlineImageConfig } from './config'
import { createApp, ref, watchEffect } from 'vue'
import { MilkdownImageInline } from './components/image-inline'

export const inlineImageView = $view(
Expand All @@ -19,8 +21,15 @@ export const inlineImageView = $view(
const setAttr = (attr: string, value: unknown) => {
const pos = getPos()
if (pos == null) return
view.dispatch(view.state.tr.setNodeAttribute(pos, attr, value))
view.dispatch(
view.state.tr.setNodeAttribute(
pos,
attr,
attr === 'src' ? DOMPurify.sanitize(value as string) : value
)
)
}

const config = ctx.get(inlineImageConfig.key)
const app = createApp(MilkdownImageInline, {
src,
Expand Down
6 changes: 4 additions & 2 deletions packages/components/src/link-tooltip/edit/edit-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../slices'
import { createApp, ref, type App, type Ref } from 'vue'
import { EditLink } from './component'
import DOMPurify from 'dompurify'

interface Data {
from: number
Expand Down Expand Up @@ -80,15 +81,16 @@ export class LinkEditTooltip implements PluginView {
const view = this.ctx.get(editorViewCtx)
const { from, to, mark } = this.#data
const type = linkSchema.type(this.ctx)
if (mark && mark.attrs.href === href) {
const link = DOMPurify.sanitize(href)
if (mark && mark.attrs.href === link) {
this.#reset()
return
}

const tr = view.state.tr
if (mark) tr.removeMark(from, to, mark)

tr.addMark(from, to, type.create({ href }))
tr.addMark(from, to, type.create({ href: link }))
view.dispatch(tr)

this.#reset()
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.