Skip to content

feat(plugin-git): sanitize commit messages #436

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 4 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions plugins/development/plugin-git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
"@vuepress/helper": "workspace:*",
"@vueuse/core": "catalog:",
"execa": "^9.5.2",
"rehype-parse": "^9.0.1",
"rehype-sanitize": "^6.0.0",
"rehype-stringify": "^10.0.1",
"unified": "^11.0.5",
"vue": "catalog:"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const GitChangelog = defineComponent({
[h('code', item.hash.slice(0, 5))],
),
h('span', { class: 'vp-changelog-divider' }, '-'),
h('span', { class: 'vp-changelog-message' }, item.message),
h('span', { class: 'vp-changelog-message', innerHTML: item.message }),
h('span', { 'class': 'vp-changelog-date', 'data-allow-mismatch': '' }, [
locale.value.timeOn || 'on',
' ',
Expand Down
3 changes: 2 additions & 1 deletion plugins/development/plugin-git/src/node/resolveChangelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { MergedRawCommit } from './typings.js'
import {
getContributorInfo,
getUserNameWithNoreplyEmail,
sanitizeHTML,
} from './utils/index.js'

const RE_CLEAN_REFS = /[()]/g
Expand Down Expand Up @@ -44,7 +45,7 @@ export const resolveChangelog = (
time,
email: contributor?.email || email,
author: contributor?.name ?? contributor?.username ?? author,
message: app.markdown.renderInline(message),
message: sanitizeHTML(app.markdown.renderInline(message)),
}

if (coAuthors.length) resolved.coAuthors = coAuthors
Expand Down
1 change: 1 addition & 0 deletions plugins/development/plugin-git/src/node/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './getUserNameWithNoreplyEmail.js'
export * from './inferGitProvider.js'
export * from './injectGitOptions.js'
export * from './checkGithubUsername.js'
export * from './sanitizeHTML.js'
27 changes: 27 additions & 0 deletions plugins/development/plugin-git/src/node/utils/sanitizeHTML.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import rehypeParse from 'rehype-parse'
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize'
import rehypeStringify from 'rehype-stringify'
import type { Processor } from 'unified'
import { unified } from 'unified'

let rehypeInstance: Processor | null = null

/**
* Sanitize HTML
* @see https://github.com/rehypejs/rehype-sanitize
*/
export const sanitizeHTML = (html: string): string => {
rehypeInstance ??= unified()
.use(rehypeParse, { fragment: true })
.use(rehypeSanitize, {
...defaultSchema,
attributes: {
...defaultSchema.attributes,
a: [...(defaultSchema.attributes?.a ?? []), 'target', 'rel'],
},
})
.use(rehypeStringify)
.freeze() as unknown as Processor

return String(rehypeInstance.processSync(html))
}
Loading
Loading