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

SE-2176 Fix elem not selected if id contains special chars #23039

Merged
merged 5 commits into from
Jun 19, 2020

Conversation

samuelallan72
Copy link

@samuelallan72 samuelallan72 commented Feb 7, 2020

If the id of a .formulaequationinput input element contains a special
character, then the selector for $preview was silently failing to match
the element, because no escaping was happening.

This fixes the issue by escaping the id before passing to the jQuery
selector function. CSS.escape is the ideal method, but this isn't
present in IE or Edge, so we use a fallback borrowed from the new
jQuery.escapeSelector method. (If this is a common thing, would suggest adding a global CSS.escape polyfill instead of a local fallback)

This also fixes bugs in tests for the xsslint tool that were causing tests to fail. The javascript-interpolate lint rule was removed altogether since it was deemed no longer necessary.

JIRA tickets: OSPR-4082

Dependencies: None

Sandbox URL: TBD - sandbox is being provisioned.

Merge deadline: "None"

Testing instructions:

  • Load a formula equation capa problem xblock from blockstore (ie. where the xblock ID doesn't go through the deprecated html_id function)
  • verify that everything loads correctly, the mathjax preview is rendered and updates with the text input, and the spinner shows/hides as expected
  • also check that the xsslint test changes make sense

Reviewers

Settings

@openedx-webhooks
Copy link

Thanks for the pull request, @swalladge! I've created OSPR-4082 to keep track of it in JIRA. JIRA is a place for product owners to prioritize feature reviews by the engineering development teams.

Feel free to add as much of the following information to the ticket:

  • supporting documentation
  • edx-code email threads
  • timeline information ("this must be merged by XX date", and why that is)
  • partner information ("this is a course on edx.org")
  • any other information that can help Product understand the context for the PR

All technical communication about the code itself will still be done via the GitHub pull request interface. As a reminder, our process documentation is here.

@openedx-webhooks openedx-webhooks added needs triage open-source-contribution PR author is not from Axim or 2U labels Feb 7, 2020
@natabene
Copy link
Contributor

natabene commented Feb 7, 2020

@swalladge Thank you for your contribution. Please let me know once it is ready for our review.

)
def test_javascript_interpolate(self, data):
def test_javascript_escape(self, data):
Copy link
Author

Choose a reason for hiding this comment

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

Note that this was renamed, because previously it was the same name as the previous test, and was shadowing that test (which also turned out to be failing)...

Copy link
Contributor

@bradenmacdonald bradenmacdonald left a comment

Choose a reason for hiding this comment

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

That does the trick! Just one request.

Note that if loading the xblock through some sandbox in not the usual way, we have had issues because the capa problem by default loads some resources through a relative url. This temporary patch to edx-platform should allow testing this PR in this context:

Turns out there is a simple, permanent fix that we should include in this PR. I've pushed it as a commit - hope you don't mind: 40237ec

common/static/js/capa/src/formula_equation_preview.js Outdated Show resolved Hide resolved
@bradenmacdonald
Copy link
Contributor

I'm not sure about the XSS lint changes, but the fix to capa and the static asset part are working well. 👍

@samuelallan72
Copy link
Author

@natabene This is ready for edX review. :)

@natabene
Copy link
Contributor

@swalladge Thanks for letting me know!

Copy link
Contributor

@ormsbee ormsbee left a comment

Choose a reason for hiding this comment

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

I've left a note for @robrap to comment on the linting, but this seems reasonable to me. Please squash and write up the nice commit message.

scripts/xsslint/xsslint/linters.py Outdated Show resolved Hide resolved
# Ignores calls starting with "_.", because those are safe
regex = regex = re.compile(r"(?<!_).escape\(")
# Ensure that calls to fooescape or foo.escape are ignored; they aren't the dangerous escape functions.
regex = re.compile(r"(?:^|(?<![\w.$]))escape\(")
Copy link
Contributor

Choose a reason for hiding this comment

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

@robrap: Could you please take a look at this adjustment to the XSS linting? It seems reasonable to me, but you have a lot more context on this than I do.

@samuelallan72 samuelallan72 force-pushed the samuel/fix-unescaped-selector branch 2 times, most recently from 86def2c to bc1457a Compare April 28, 2020 07:25
scripts/xsslint/tests/test_linters.py Show resolved Hide resolved
scripts/xsslint/xsslint/linters.py Outdated Show resolved Hide resolved
scripts/xsslint/xsslint/linters.py Outdated Show resolved Hide resolved
scripts/xsslint/xsslint/linters.py Outdated Show resolved Hide resolved
common/static/js/capa/src/formula_equation_preview.js Outdated Show resolved Hide resolved
common/static/js/capa/src/formula_equation_preview.js Outdated Show resolved Hide resolved
Copy link
Contributor

@robrap robrap left a comment

Choose a reason for hiding this comment

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

Minor comments and suggestions. Thank you for this.

No worries about leaving the utility where it is. Proper escaping and linting is hard, which is why the docs were vague to begin with regarding CSS escaping. I imagine it is a security blindspot for many people, but not sure. Hopefully we aren't popping in user-generated data in this way.

common/static/js/capa/src/formula_equation_preview.js Outdated Show resolved Hide resolved
scripts/xsslint/tests/test_linters.py Show resolved Hide resolved
scripts/xsslint/xsslint/linters.py Outdated Show resolved Hide resolved
scripts/xsslint/xsslint/linters.py Outdated Show resolved Hide resolved
@natabene
Copy link
Contributor

@swalladge Please let me know once it is ready for another look.

@samuelallan72 samuelallan72 requested a review from a team May 1, 2020 04:10
@samuelallan72
Copy link
Author

@natabene Thanks. Yes this is ready for another review pass. CC @robrap

Copy link
Contributor

@robrap robrap left a comment

Choose a reason for hiding this comment

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

Thank you! The linting changes all look good to me.

  • Maybe update the PR description and commit comment to mention that we removed the linting rule for javascript-interpolate because it was deemed unnecessary.

Samuel Walladge and others added 3 commits May 4, 2020 09:27
If the id of a `.formulaequationinput input` element contains a special
character, then the selector for $preview was silently failing to match
the element, because no escaping was happening.

This fixes the issue by escaping the id before passing to the jQuery
selector function. CSS.escape is the ideal method, but this isn't
present in IE or Edge, so we use a fallback borrowed from the new
jQuery.escapeSelector method.
Improve accuracy of javascript-escape linter: Previously this would
match on FOOescape() and FOO.escape calls, but neither are the global
escape function we are worried about.

The regex probably isn't 100% accurate; there may be still false
positives (javascript allows a large range of characters in identifiers,
some of which may not be covered by [\w.$]). The main thing is to avoid
false negatives here though - this will definitely catch any use of
`escape()` or `window.escape()`.

Also remove javascript-interpolate lint - this was deemed unecessary.
StringUtils.interpolate is not in fact safe (it does no html escaping),
so the results of this lint are misleading.
@samuelallan72 samuelallan72 force-pushed the samuel/fix-unescaped-selector branch from 8e17d04 to fe06bc8 Compare May 4, 2020 00:00
@samuelallan72
Copy link
Author

@robrap @ormsbee Thanks for your reviews. Commits have been squashed to logical changes and neater messages written. Ready for another review pass. :)

Copy link
Contributor

@robrap robrap left a comment

Choose a reason for hiding this comment

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

Still approve, with 2 caveats:

  1. Fixing the Diff Quality errors. See the diff_quality_eslint tab under the report: https://build.testeng.edx.org/job/edx-platform-quality-pipeline-pr/15631/Diff_20Quality_20Report/
  2. I am not planning to review the 3rd commit Fix capa's static_url on devstack in new runtime. Hopefully someone else has that. :)
    Thanks for all the linting clean-up.

@natabene
Copy link
Contributor

natabene commented May 4, 2020

@ormsbee Can you give this another look?

@samuelallan72
Copy link
Author

@robrap thanks for your time reviewing this. diff quality errors fixed. 😄

@edx-status-bot
Copy link

Your PR has finished running tests. There were no failures.

@ormsbee ormsbee merged commit 0b4cf7e into openedx:master Jun 19, 2020
@openedx-webhooks
Copy link

@swalladge 🎉 Your pull request was merged!

Please take a moment to answer a two question survey so we can improve your experience in the future.

@ormsbee
Copy link
Contributor

ormsbee commented Jun 19, 2020

Argh. I forgot to squash + merge >_<

@edx-pipeline-bot
Copy link
Contributor

EdX Release Notice: This PR has been deployed to the staging environment in preparation for a release to production.

@edx-pipeline-bot
Copy link
Contributor

EdX Release Notice: This PR has been deployed to the production environment.

@samuelallan72
Copy link
Author

Thanks @ormsbee! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged open-source-contribution PR author is not from Axim or 2U
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants