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

Implement _fragmentParser #1

Closed
wants to merge 16 commits into from
Closed

Conversation

0xedward
Copy link
Owner

@0xedward 0xedward commented Jul 3, 2021

mozilla#3

Will first merge and squash the commits into 0xedward/sanitizer-polyfill main branch, then make a separate PR to merge into mozilla/sanitizer-polyfill for a clean commit history

Copy link

@mozfreddyb mozfreddyb left a comment

Choose a reason for hiding this comment

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

👏 👏 👏

TODO Outdated Show resolved Hide resolved
src/polyfill.js Outdated Show resolved Hide resolved
src/sanitizer.js Outdated Show resolved Hide resolved
src/sanitizer.js Outdated Show resolved Hide resolved
src/sanitizer.js Outdated Show resolved Hide resolved
src/sanitizer.js Outdated Show resolved Hide resolved
tests/testpage.html Outdated Show resolved Hide resolved
@0xedward 0xedward changed the title [DRAFT] Implement fragment parser [DRAFT] Implement _fragmentParser Jul 5, 2021
@0xedward 0xedward changed the title [DRAFT] Implement _fragmentParser [DRAFT] Implement _fragmentParser Jul 5, 2021
src/polyfill.js Outdated Show resolved Hide resolved
src/sanitizer.js Outdated
// TODO implement this function and remove eslint-disable comment above
/**
* fragmentParser
* a parser that doesn't run scripts or loads resources

Choose a reason for hiding this comment

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

ouch. I just tested this. That's not actually true 🤣 😭
This is bad, but it doesn't justify throwing all of your other useful code changes away. Can you change that comment and file a follow-up? I have some ideas, that I'll need to verify and will get into that issue once it is filed.

Copy link
Owner Author

@0xedward 0xedward Jul 7, 2021

Choose a reason for hiding this comment

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

ouch. I just tested this. That's not actually true 🤣 😭

Oh noo, that's right... Range.createContextualFragment will execute scripts 😢

I guess we are 11 years too late to take advantage of this bug 🙃

This is bad, but it doesn't justify throwing all of your other useful code changes away. Can you change that comment and file a follow-up?

Sure! I'll filed an issue, updated the comment to remove "doesn't run scripts" and added a reference to the issue in code.

I have some ideas, that I'll need to verify and will get into that issue once it is filed.

I found an interesting old WebKit, WhatWG and Twitter conversations on this.

Also this post might be useful here - https://grrr.tech/posts/create-dom-node-from-html-string/

Choose a reason for hiding this comment

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

After some research, I think we just have to use the same techniques DOMPurify is using: Use a new document, disregard the context :-(
An alternative would be to create a mock-context, i.e., adding the context Element to the mock-document and then parsing in there. That won't help when the stack of open elements contains more than 1 required element (would work for tr in table but maybe not for td in tr, because we're lacking the outer table). Not sure if that's worth for a polyfill, when DOMPurify has so far been almost untouched by this flaw?

Copy link
Owner Author

Choose a reason for hiding this comment

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

After some research, I think we just have to use the same techniques DOMPurify is using: Use a new document, disregard the context :-(

Not sure if that's worth for a polyfill, when DOMPurify has so far been almost untouched by this flaw?

Ah, should we abandon this PR and go back to shimming DOMPurify?

Choose a reason for hiding this comment

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

Yeah, I'm afraid we have to...Sorry for this all...

Copy link
Owner Author

Choose a reason for hiding this comment

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

No problem at all! I learned a lot from going down this path 😄

I'll go back to your original plan and start working on it!

@0xedward 0xedward changed the title [DRAFT] Implement _fragmentParser Implement _fragmentParser Jul 7, 2021
@0xedward 0xedward marked this pull request as ready for review July 7, 2021 05:10
@0xedward 0xedward changed the base branch from main to upstream July 7, 2021 05:29
Copy link

@mozfreddyb mozfreddyb left a comment

Choose a reason for hiding this comment

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

I'm afraid there's no way we can parse contextually without executing scripts. Sorry again..

@0xedward 0xedward closed this Jul 15, 2021
@0xedward
Copy link
Owner Author

No problem! I'm glad we explored this option, since I learned a lot about DocumentFragments and the HTML parsing algorithm :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants