-
Notifications
You must be signed in to change notification settings - Fork 142
Fix radio button unsafe token access bug #2790
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
Fix radio button unsafe token access bug #2790
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2790 +/- ##
==========================================
+ Coverage 71.67% 71.70% +0.03%
==========================================
Files 134 134
Lines 7286 7294 +8
Branches 1609 1587 -22
==========================================
+ Hits 5222 5230 +8
- Misses 1937 2018 +81
+ Partials 127 46 -81 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
If I understand it correctly, your change parses empty (checked or unchecked) radios to be recognized and rendered, while the previous implementation does not do so. I think we should stick to the old implementation for the following reasons:
See:
Edit: |
gerteck
left a comment
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.
lgtm with some small changes
| var radio = new TokenConstructor('html_inline', '', 0); | ||
| var disabledAttr = disableRadio ? ' disabled="" ' : ''; | ||
| if (token.content.indexOf('( ) ') === 0) { | ||
|
|
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.
Let's not cause a change in functionality (see comment in main thread)
I think the refactoring is good (naming checked and unChecked), can keep that.
Probably can consider using const instead of var, var is legacy and shouldn't be used anymore.
Though if you do might as well refactor the whole file which is legacy but that would go beyond the scope of the PR, and you culd and should probably do another PR for it. Tons of refactoring improvement possibilities:
- Use
startsWithinstead ofindexOf === 0 - change all var to const
- use template literatls instead of string concat
etc etc
Maybe tackle broader refactoring if you wish, in another PR
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.
Noted! Shall clean this up and work on refactoring in another PR in the future.
packages/core/src/lib/markdown-it/plugins/markdown-it-radio-button.js
Outdated
Show resolved
Hide resolved
packages/core/test/unit/lib/markdown-it/plugins/markdown-it-radio-button.test.ts
Show resolved
Hide resolved
gerteck
left a comment
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.
LGTM, thanks for the quick update!
|
Welcome, @MoshiMoshiMochi! 🎉 Thank you for your contribution to the MarkBind project! @gerteck, please remember to add @MoshiMoshiMochi as an official contributor to our repository. See the full list of contributors here. ✨ |
|
@all-contributors please add MoshiMoshiMochi for code |
|
I've put up a pull request to add @MoshiMoshiMochi! 🎉 |

What is the purpose of this pull request?
Resolves #2749
Overview of changes:
Fixed the unsafe token access & unskipped the testcases made in #2747
(x)&(X)respectivelyAnything you'd like to highlight/discuss:
As @gerteck mentioned in the issue,
Hence, I fixed it by adding bounds checks before accessing
tokens[i-5]&tokens[i-4]which prevents the crash when those tokens don't exist.Additionally, the previous logic only handled the radio button cases if there was a space after the parentheses, i,.e.
'( ) '. However, will not be the case dealing with empty radio buttons. Hence, I added the cases to explicitly when the radio button is the entire content of the line (i.e. empty radio button).indexOfchecks were initially for'( ) '& not'( )'to begin with (,because from my testing, using the latter fixes this issue for the most part, but I might be missing the bigger picture here.)Aside from that, kindly let me know if my changes are suffice and if there is anything else that I missed/could improve. 🤓
PS: Potential code quality improvement?
I was thinking of repurposing
function startsWithTodoMarkdown()since it is only used byfunction isTodoItem(token, index)and its logic is somewhat repeated infunction makeRadioButton(token, TokenConstructor, radioId).But at the same time, I'm not sure if doing so will be worth it if it decreases the clarity / makes
makeRadioButtondependent onstartsWithTodoMarkdowntoo. 🫤Interested to hear your thoughts on this matter too! 😀
See resolved example
e.g. when serving this markdown file
Example of whats generated:

Testing instructions:
Proposed commit message: (wrap lines at 72 characters)
Fix markdown-it radio button unsafe token access
Checklist: ☑️
Reviewer checklist:
Indicate the SEMVER impact of the PR:
At the end of the review, please label the PR with the appropriate label:
r.Major,r.Minor,r.Patch.Breaking change release note preparation (if applicable):