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

Block scripts with text/csv, audio/*, video/* and image/* mime types #16126

Merged
merged 1 commit into from
Apr 5, 2017

Conversation

ferjm
Copy link
Contributor

@ferjm ferjm commented Mar 24, 2017

This patch implements step 12 of the Main Fetch section of the Fetch API standard. It blocks the load of scripts with text/csv, audio/*, video/* and image/* mime types.

Credit for the logic of should_block_mime_type function should go to the author of #14770.


This change is Reviewable

@highfive
Copy link

Heads up! This PR modifies the following files:

@highfive
Copy link

warning Warning warning

  • These commits include an empty title element (<title></title>). Consider adding appropriate metadata.

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 24, 2017
@ferjm
Copy link
Contributor Author

ferjm commented Mar 24, 2017

r? @asajeffrey

@highfive highfive assigned asajeffrey and unassigned Manishearth Mar 24, 2017
@ferjm ferjm force-pushed the issue-14520-block-media-csv branch from a63e21c to 2c3b985 Compare March 24, 2017 21:11
Copy link
Contributor

@nox nox left a comment

Choose a reason for hiding this comment

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

Some improvements can be made.

@@ -616,6 +621,18 @@ fn should_block_nosniff(request: &Request, response: &Response) -> bool {
};
}

/// https://fetch.spec.whatwg.org/#should-response-to-request-be-blocked-due-to-mime-type?
fn should_block_mime_type(request: &Request, response: &Response) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: should_be_blocked_due_to_mime_type, given I recently named the other one should_be_blocked_due_to_bad_port.

fn should_block_mime_type(request: &Request, response: &Response) -> bool {
let mime_type = response.headers.get::<ContentType>();
let csv: Mime = "text/csv".parse().unwrap();
request.type_ == Type::Script && mime_type.is_some() && match *mime_type.unwrap() {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can first unwrap mime_type and return early if it is None.

let mime_type = match response.headers.get::<ContentType>() {
    Some(header) => header,
    None => return false,
};

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd also improve this by not pre-allocating "text/csv".parse() at all and pattern-match directly against ContentType(Mime(TopLevel::Text, SubLevel::Csv, _)).

Copy link
Contributor

Choose a reason for hiding this comment

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

SubLevel::Csv doesn't exist, and I already suggested a way to not parse the CSV mimetype below. :)

ContentType(Mime(TopLevel::Audio, _, _)) |
ContentType(Mime(TopLevel::Video, _, _)) |
ContentType(Mime(TopLevel::Image, _, _)) => true,
ContentType(ref m_type) => *m_type == csv
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to allocate a Mime value for csv.

match *mime_type {
    ...
    ContentType(Mime(TopLevel::Text, SubLevel::Ext(ref ext), _)) => ext == "csv",
    _ => false,
}

@nox nox assigned nox and unassigned asajeffrey Mar 25, 2017
@nox nox added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Mar 25, 2017
@ferjm ferjm force-pushed the issue-14520-block-media-csv branch from 2c3b985 to e9915f3 Compare March 27, 2017 10:21
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Mar 27, 2017
@ferjm
Copy link
Contributor Author

ferjm commented Mar 27, 2017

Thanks for the feedback @nox. Could you take another look, please?

@nox
Copy link
Contributor

nox commented Mar 28, 2017

Squash the two commits together and this is ready to be merged.

@nox nox added S-needs-squash Some (or all) of the commits in the PR should be combined. and removed S-awaiting-review There is new code that needs to be reviewed. labels Mar 28, 2017
@ferjm ferjm force-pushed the issue-14520-block-media-csv branch from e9915f3 to e91c177 Compare March 28, 2017 08:10
@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 28, 2017
@ferjm
Copy link
Contributor Author

ferjm commented Mar 28, 2017

Thank you @nox. Rebased and squashed.

@nox
Copy link
Contributor

nox commented Mar 28, 2017

@jdm Shouldn't tests/wpt/mozilla/tests/mozilla/block-mime-as-script.html be in tests/wpt/web-platform-tests instead?

@nox nox added S-awaiting-answer Someone asked a question that requires an answer. and removed S-awaiting-review There is new code that needs to be reviewed. S-needs-squash Some (or all) of the commits in the PR should be combined. labels Mar 28, 2017
@bors-servo
Copy link
Contributor

☔ The latest upstream changes (presumably #16160) made this pull request unmergeable. Please resolve the merge conflicts.

@highfive highfive added the S-needs-rebase There are merge conflict errors. label Mar 29, 2017
@ferjm ferjm force-pushed the issue-14520-block-media-csv branch from e91c177 to d6dab7b Compare March 29, 2017 11:48
@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 29, 2017
@ferjm
Copy link
Contributor Author

ferjm commented Mar 29, 2017

Rebased and moved tests to tests/wpt/web-platform-tests

@ferjm ferjm removed the S-needs-rebase There are merge conflict errors. label Mar 29, 2017
@bors-servo
Copy link
Contributor

☔ The latest upstream changes (presumably #16214) made this pull request unmergeable. Please resolve the merge conflicts.

@highfive highfive added the S-needs-rebase There are merge conflict errors. label Apr 3, 2017
@ferjm ferjm force-pushed the issue-14520-block-media-csv branch from d6dab7b to cf44fab Compare April 3, 2017 14:39
@ferjm ferjm removed the S-needs-rebase There are merge conflict errors. label Apr 3, 2017
@jdm jdm removed the S-awaiting-answer Someone asked a question that requires an answer. label Apr 3, 2017
@jdm
Copy link
Member

jdm commented Apr 3, 2017

Seems good to me.

// Defer rebinding result
blocked_error_response = Response::network_error(NetworkError::Internal("Blocked by nosniff".into()));
blocked_error_response = Response::network_error(
NetworkError::Internal("Blocked by nosniff or mime type".into()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Please instead do a else if block and separate the two blocking errors.

@nox nox added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Apr 3, 2017
@ferjm ferjm force-pushed the issue-14520-block-media-csv branch from cf44fab to 29a56c4 Compare April 3, 2017 16:25
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Apr 3, 2017
@ferjm
Copy link
Contributor Author

ferjm commented Apr 3, 2017

Done. r? @nox

@nox
Copy link
Contributor

nox commented Apr 5, 2017

@bors-servo r+

@bors-servo
Copy link
Contributor

📌 Commit 29a56c4 has been approved by nox

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Apr 5, 2017
@bors-servo
Copy link
Contributor

⌛ Testing commit 29a56c4 with merge 1071c33...

bors-servo pushed a commit that referenced this pull request Apr 5, 2017
Block scripts with text/csv, audio/*, video/* and image/* mime types

This patch implements step 12 of the Main Fetch section of the Fetch API standard. It blocks the load of scripts with `text/csv`, `audio/*`, `video/*` and `image/*` mime types.

Credit for the logic of `should_block_mime_type` function should go to the author of #14770.

- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #14520
- [X] There are tests for these changes

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/16126)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css, mac-rel-wpt1, mac-rel-wpt2, windows-msvc-dev
Approved by: nox
Pushing 1071c33 to master...

@bors-servo bors-servo merged commit 29a56c4 into servo:master Apr 5, 2017
@highfive highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Apr 5, 2017
@ferjm ferjm deleted the issue-14520-block-media-csv branch April 5, 2017 08:52
jdm pushed a commit to web-platform-tests/wpt that referenced this pull request Apr 17, 2017
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.

Block scripts with media/CSV mime types
8 participants