-
Notifications
You must be signed in to change notification settings - Fork 166
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(utils): markdown callouts #2298
Open
Erb3
wants to merge
13
commits into
modrinth:main
Choose a base branch
from
Erb3-forked:github-style-markdown-callouts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
709ed41
feat(utils): markdown callouts
Erb3 fa3e22b
fix(utils): add back xss class filtering for p and svg
Erb3 088920e
Merge branch 'main' into github-style-markdown-callouts
Erb3 3039f06
fix(web): lucide icons
Erb3 0cc96c9
Merge branch 'main' into github-style-markdown-callouts
Erb3 e22e90d
chore: attempt to fix conflict
Erb3 66c7af1
Merge branch 'main' into github-style-markdown-callouts
Erb3 c45da4a
refactor(web): markdown alerts scss
Erb3 552dce6
refactor(web): use pseudo-elements for markdown callouts
Erb3 15f0b9f
refactor(web): position callouts correctly
Erb3 389bc2a
Merge branch 'main' into github-style-markdown-callouts
Erb3 38d6de4
refactor: add callout icons to assets
Erb3 1072b8d
Merge branch 'main' into github-style-markdown-callouts
Erb3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
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 |
---|---|---|
@@ -1,4 +1,12 @@ | ||
import { | ||
InfoIcon, | ||
LightBulbIcon, | ||
MessageSquareWarningIcon, | ||
OctogonAlertIcon, | ||
TriangleAlertIcon, | ||
} from '@modrinth/assets' | ||
import MarkdownIt from 'markdown-it' | ||
import MarkdownItGitHubAlerts from 'markdown-it-github-alerts' | ||
import { escapeAttrValue, FilterXSS, safeAttrValue, whiteList } from 'xss' | ||
|
||
export const configuredXss = new FilterXSS({ | ||
|
@@ -24,6 +32,19 @@ export const configuredXss = new FilterXSS({ | |
source: ['media', 'sizes', 'src', 'srcset', 'type'], | ||
p: [...(whiteList.p || []), 'align'], | ||
div: [...(whiteList.p || []), 'align'], | ||
svg: [ | ||
'aria-hidden', | ||
'width', | ||
'height', | ||
'viewBox', | ||
'fill', | ||
'stroke', | ||
'stroke-width', | ||
'stroke-linecap', | ||
'stroke-linejoin', | ||
], | ||
path: ['d'], | ||
circle: ['cx', 'cy', 'r'], | ||
}, | ||
css: { | ||
whiteList: { | ||
|
@@ -75,6 +96,28 @@ export const configuredXss = new FilterXSS({ | |
} | ||
return `${name}="${escapeAttrValue(allowedClasses.join(' '))}"` | ||
} | ||
|
||
// For markdown callouts | ||
if (name === 'class' && ['div', 'p'].includes(tag)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again it seems weird that this is being whitelisted within the input, as someone could put these classes on their own html elements and have weird styles happen |
||
const classWhitelist = [ | ||
'markdown-alert', | ||
'markdown-alert-note', | ||
'markdown-alert-tip', | ||
'markdown-alert-warning', | ||
'markdown-alert-important', | ||
'markdown-alert-caution', | ||
'markdown-alert-title', | ||
] | ||
|
||
const allowed: string[] = [] | ||
for (const className of value.split(/\s/g)) { | ||
if (classWhitelist.includes(className)) { | ||
allowed.push(className) | ||
} | ||
} | ||
|
||
return `${name}="${escapeAttrValue(allowed.join(' '))}"` | ||
} | ||
}, | ||
safeAttrValue(tag, name, value, cssFilter) { | ||
if ( | ||
|
@@ -133,6 +176,16 @@ export const md = (options = {}) => { | |
...options, | ||
}) | ||
|
||
md.use(MarkdownItGitHubAlerts, { | ||
icons: { | ||
note: InfoIcon, | ||
tip: LightBulbIcon, | ||
important: MessageSquareWarningIcon, | ||
warning: TriangleAlertIcon, | ||
caution: OctogonAlertIcon, | ||
}, | ||
}) | ||
|
||
const defaultLinkOpenRenderer = | ||
md.renderer.rules.link_open || | ||
function (tokens, idx, options, _env, self) { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this has me a little concerned as I know there's a bunch of random things you can do with svgs. it doesn't necessarily look like this allows anything bad but I don't know if I am knowledgable enough about svg vulnerabilities to approve this confidently.
it also seems to me like the elements added as part of the markdown renderer should not be subject to the xss/whitelisted elements as the raw html input. I wonder if there is a different way to do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was also a concern for me, but I frankly found no other way to do it. From my testing the jsxss was executed based on the output from markdown-it, not the description itself. We can probably change it to be pre-html, but that sounds insecure.