Skip to content
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

Feat: skip dom purify for trusted content #393

Merged
merged 3 commits into from
Oct 24, 2024
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
29 changes: 18 additions & 11 deletions packages/icons/src/PixivIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import warning from 'warning'
import { KnownIconFile } from './charcoalIconFiles'
import { getIcon, addCustomIcon } from './loaders'
import { __SERVER__ } from './ssr'
import DOMPurify from 'dompurify'

const attributes = ['name', 'scale', 'unsafe-non-guideline-scale'] as const

Expand Down Expand Up @@ -179,8 +178,11 @@ export class PixivIcon extends HTMLElement {
render() {
const size = this.forceResizedSize ?? this.scaledSize

const style = DOMPurify.sanitize(
`<style>
if (!Number.isFinite(size)) {
throw new TypeError(`icon size must not be NaN`)
}
Copy link
Member

@fsubal fsubal Oct 5, 2023

Choose a reason for hiding this comment

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

[IMO] 実は Number.isFinite() だけで良かったりしませんか?

    if (!Number.isFinite(size)) {
      throw new TypeError('icon size must be a finite number')
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

1/0Infinityを考えると確かにisFiniteが良さそうです。実際はかなりのedge caseですね

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

すみません意図としては typeofnumber であるかの判定も isFinite() だけで行けるんじゃないですか、みたいなことでした(これでも良いですが)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

4eb112c 消しました。元々エラー少しわかりやすくなるかもしれないという意図で入れていたが処理的には確か不要そうです


const style = `<style>
:host {
display: inline-flex;
--size: ${size}px;
Expand All @@ -190,23 +192,28 @@ export class PixivIcon extends HTMLElement {
width: var(--size);
height: var(--size);
}
</style>`,
{ ALLOWED_TAGS: ['style'], FORCE_BODY: true }
)
</style>`

const svg = DOMPurify.sanitize(
const svg =
this.svgContent !== undefined
? this.svgContent
: `<svg viewBox="0 0 ${size} ${size}"></svg>`,
{ USE_PROFILES: { svg: true, svgFilters: true } }
)
: `<svg viewBox="0 0 ${size} ${size}"></svg>`

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.shadowRoot!.innerHTML = style + svg
}

private async loadSvg(name: string) {
this.svgContent = await getIcon(name)
const iconResult = await getIcon(name)

if (iconResult.trusted) {
this.svgContent = iconResult.svgContent
} else {
const { default: DOMPurify } = await import('dompurify')
Copy link
Contributor Author

@yue4u yue4u Sep 25, 2023

Choose a reason for hiding this comment

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

dom-purify is still pretty large so we can do following in the future.

  1. let users do sanitizing themselves
  2. leveraging https://developer.mozilla.org/en-US/docs/Web/API/HTML_Sanitizer_API

this.svgContent = DOMPurify.sanitize(iconResult.svgContent, {
USE_PROFILES: { svg: true, svgFilters: true },
})
}
this.render()
}

Expand Down
4 changes: 4 additions & 0 deletions packages/icons/src/loaders/CharcoalIconFilesLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export class CharcoalIconFilesLoader implements Loadable {
this._name = name
}

get trusted() {
return true
}

get importIconFile() {
return charcoalIconFiles[this._name]
}
Expand Down
4 changes: 4 additions & 0 deletions packages/icons/src/loaders/CustomIconLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export class CustomIconLoader implements Loadable {
this._filePathOrUrl = filePathOrUrl
}

get trusted() {
return false
}

async fetch(): Promise<string> {
if (this._resultSvg !== undefined) {
return this._resultSvg
Expand Down
1 change: 1 addition & 0 deletions packages/icons/src/loaders/Loadable.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface Loadable {
readonly trusted: boolean
fetch(): Promise<string>
}
17 changes: 10 additions & 7 deletions packages/icons/src/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ export async function getIcon(name: string) {
throw new PixivIconLoadError(name, 'Loader was not found')
}

return loader.fetch().catch((e) => {
if (e instanceof PixivIconLoadError) {
throw e
}

throw new PixivIconLoadError(name, e)
})
return loader
.fetch()
.then((svgContent) => ({ trusted: loader.trusted, svgContent }))
.catch((e) => {
if (e instanceof PixivIconLoadError) {
throw e
}

throw new PixivIconLoadError(name, e)
})
}

function resolveIconLoader(name: string) {
Expand Down
Loading