Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Core:Manipulation: Add basic TrustedHTML support#4927

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mgol merged 2 commits intojquery:mainfrommgol:trusted-html
Sep 30, 2021

Conversation

mgol
Copy link
Member

@mgolmgol commentedSep 9, 2021
edited
Loading

Summary

This ensures HTML wrapped in TrustedHTML can be used as an input to jQuery
manipulation methods in a way that doesn't violate the
require-trusted-types-for Content Security Policy directive.
This commit builds on previous work needed for trusted types support, including
gh-4642 andgh-4724.

One restriction is that while any TrustedHTML wrapper should work as input
for jQuery methods like.html() or.append(), for passing directly to the
jQuery factory the string must start with< and end with>; no trailing
or leading whitespaces are allowed. This is necessary as we cannot parse out
a part of the input for further construction; that would violate the CSP rule -
and that's what's done to HTML input not matching these constraints.

No trusted types API is used explicitly in source; the majority of the work is
ensuring we don't pass the input converted to string to APIs that would
eventually assign it toinnerHTML. This extra cautiousness is caused by the
API being Blink-only, at least for now.

The ban on passing strings toinnerHTML means support tests relying on such
assignments are impossible. We don't currently have such tests on themain
branch but we used to have many of them in the 3.x & older lines. If there's
a need to re-add such a test, we'll need an escape hatch to skip them for apps
needing CSP-enforced TrustedHTML.

Seehttps://web.dev/trusted-types/ for more information about TrustedHTML.

Fixesgh-4409
Refgh-4642
Refgh-4724

+30 bytes

Checklist

@mgolmgol added Manipulation Core Discuss in MeetingReserved for Issues and PRs that anyone would like to discuss in the weekly meeting. labelsSep 9, 2021
@mgolmgol self-assigned thisSep 9, 2021
@mgol
Copy link
MemberAuthor

mgol commentedSep 9, 2021

Travis fails due to#4926 which is unrelated.

}
} ),
inputs = [
[ "<div></div>", "<div class='test'></div>", [ "div" ] ],

Choose a reason for hiding this comment

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

Does it make sense to also add a test forTrustedHTMLs that wrap over a non-obvious HTML, e.g. just some text? From what I can tell, different branches are used then.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Yes, good point. That will only apply to the second array items as the first ones are passed directly tojQuery which also supports selectors - and since for TrustedHTML we cannot carve out a part of an input string to later parse, I opted to restrict support for TrustedHTML-wrapped strings to ones that are start with< and end with> - i.e. what was already going through the fast path skipping the regex match.

Choose a reason for hiding this comment

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

So, I assume thatTrustedHTML wrappingsomething will just get stringified somewhere and be treated as a selector? That's OK, I don't think there would be expectations for jQuery to do anything else. I think it just makes sense to have an explicit test for that behavior.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

@koto It would actually just create a jQuery wrapper with a single element being this TrustedHTML instance, i.e. it would treat it like any other non-DOM object. I just didn't handle it in any special way and let it go where it goes naturally in the current flow.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

To be clear, this limitation only applies to (pseudo-code)jQuery(TrustedHTML('foo'));jQuery(document.body).append(TrustedHTML('foo')) should append the textfoo just as without wrapping in TrustedHTML; I'll add a test for that.

Choose a reason for hiding this comment

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

Oh, I forgot abouthttps://api.jquery.com/jQuery/#jQuery-object behavior :) Yeah, that's fine, thanks for clarifying.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I added one test case for text content. I haven't added one for the object wrapper as I'm not sure if we want this to be a part of the contract.

Choose a reason for hiding this comment

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

Sounds good!

@koto
Copy link

Looks great!

@mgol
Copy link
MemberAuthor

mgol commentedSep 13, 2021
edited
Loading

PR updated, we're down from +38 to +30 bytes.

koto reacted with heart emoji

@mgolmgol added this to the4.0.0 milestoneSep 13, 2021
@mgolmgol marked this pull request as ready for reviewSeptember 13, 2021 17:07
@linux-foundation-easycla
Copy link

linux-foundation-easyclabot commentedSep 14, 2021
edited
Loading

CLA Signed

The committers are authorized under a signed CLA.

  • ✅ Michał Gołębiowski-Owczarek (57c62b4)

@timmywiltimmywil removed the Discuss in MeetingReserved for Issues and PRs that anyone would like to discuss in the weekly meeting. labelSep 20, 2021
This ensures HTML wrapped in TrustedHTML can be used as an input to jQuerymanipulation methods in a way that doesn't violate the`require-trusted-types-for` Content Security Policy directive.This commit builds on previous work needed for trusted types support, includingjquerygh-4642 andjquerygh-4724.One restriction is that while any TrustedHTML wrapper should work as inputfor jQuery methods like `.html()` or `.append()`, for passing directly to the`jQuery` factory the string must start with `<` and end with `>`; no trailingor leading whitespaces are allowed. This is necessary as we cannot parse outa part of the input for further construction; that would violate the CSP rule -and that's what's done to HTML input not matching these constraints.No trusted types API is used explicitly in source; the majority of the work isensuring we don't pass the input converted to string to APIs that wouldeventually assign it to `innerHTML`. This extra cautiousness is caused by theAPI being Blink-only, at least for now.The ban on passing strings to `innerHTML` means support tests relying on suchassignments are impossible. We don't currently have such tests on the `main`branch but we used to have many of them in the 3.x & older lines. If there'sa need to re-add such a test, we'll need an escape hatch to skip them for appsneeding CSP-enforced TrustedHTML.Seehttps://web.dev/trusted-types/ for more information about TrustedHTML.Fixesjquerygh-4409Refjquerygh-4642Refjquerygh-4724
@mgol
Copy link
MemberAuthor

@timmywil@koto I also added one small change in the tests (in the fixup commit) that makes the tests run even in browsers with no TrustedHTML support - there, it uses a simple object wrapper with atoString function returning the HTML string. It also shows that the changes in this PR are generic and would work with a number of similar APIs as well.

koto reacted with thumbs up emoji

@mgolmgol merged commitde5398a intojquery:mainSep 30, 2021
@mgolmgol deleted the trusted-html branchSeptember 30, 2021 14:00
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@kotokotokoto approved these changes

@timmywiltimmywiltimmywil approved these changes

Assignees

@mgolmgol

Milestone
4.0.0
Development

Successfully merging this pull request may close these issues.

Have Trusted Types API built directly into the jQuery Core Files
3 participants
@mgol@koto@timmywil

[8]ページ先頭

©2009-2025 Movatter.jp