Skip to content

implement range input sanitization - #19761

Merged
bors-servo merged 1 commit into
servo:masterfrom
tigercosmos:m1
Jan 17, 2018
Merged

bors-servo merged 1 commit into
servo:masterfrom
tigercosmos:m1

Conversation

@tigercosmos

@tigercosmos tigercosmos commented Jan 13, 2018 •

Copy link
Copy Markdown
Contributor

implement range input sanitation.
Since there is no min, max, step implementation currently, this should be continued in the future.

r? KiChjang


  • There are tests for these changes OR
  • These changes do not require tests because _____

This change is Reviewable

@highfive

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @asajeffrey: components/script/dom/htmlinputelement.rs, components/script/dom/bindings/str.rs
  • @fitzgen: components/script/dom/htmlinputelement.rs, components/script/dom/bindings/str.rs
  • @KiChjang: components/script/dom/htmlinputelement.rs, components/script/dom/bindings/str.rs

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Jan 13, 2018
Comment thread components/script/dom/bindings/str.rs Outdated
/// https://html.spec.whatwg.org/multipage/#best-representation-of-the-number-as-a-floating-point-number
pub fn best_representation_of_the_float_number(&mut self) {
let input = &self.0.clone();
match input.parse::<f64>() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can we re-use the logic in is_valid_float_number_string, or refactor so the logic isn't duplicated?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@NLincoln I have re-factored it. :)

Comment thread components/script/dom/bindings/str.rs Outdated
year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
}


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: Here's an extra newline.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure

Comment thread components/script/dom/bindings/str.rs Outdated

/// https://html.spec.whatwg.org/multipage/#best-representation-of-the-number-as-a-floating-point-number
pub fn best_representation_of_the_float_number(&mut self) {
let input = &self.0.clone();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you can do same way as is_valid_float_number_string.
Just passing &self.0 into parse_float_number then no need to clone here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

is_valid_float_number_string don't need to change self.
However, here we change it. Need clone it to avoid conflict.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

oh, maybe I'm not familiar with borrow checker enough.
If you passing self.0 with reference, will the variable be moved?

Comment thread components/script/dom/bindings/str.rs Outdated
}

/// https://html.spec.whatwg.org/multipage/#best-representation-of-the-number-as-a-floating-point-number
pub fn best_representation_of_the_float_number(&mut self) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO, maybe naming the function with a set prefix might be better to let other developers to know this function will set value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good point!

self.0 = val;
},
_ => {}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe changing this match to if let might be better; then you don't need to use the wildcard matching here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Could you paste code let me know?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we might be able to do this like

if let Ok(parsed_value) = parse_float_number(input) {
  self.0 = parsed_value;
}

Comment thread components/script/dom/bindings/str.rs Outdated
val.is_infinite() || val.is_nan() || input.ends_with(".") || input.starts_with("+")
) => {
// FIXME(tigercosmos): need consider `min`, `max`, `step`, when they are implemented
Ok(format!("{}", val.round()))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a reason why this can't be Ok(val.round().to_string())?

Comment thread components/script/dom/bindings/str.rs Outdated
input.parse::<f64>().ok().map_or(false, |val| {
!(val.is_infinite() || val.is_nan() || input.ends_with(".") || input.starts_with("+"))
})
pub fn is_valid_float_number_string(&self) -> bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I find the terminology here very odd. We usually call them "floats" or "floating point numbers", but never "float number".

Comment thread components/script/dom/bindings/str.rs Outdated
@@ -306,11 +306,15 @@ impl DOMString {
/// A valid number is the same as what rust considers to be valid,
/// except for +1., NaN, and Infinity.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This comment should be pasted above parse_float_number.

Comment thread components/script/dom/bindings/str.rs Outdated
Ok(val) if !(
val.is_infinite() || val.is_nan() || input.ends_with(".") || input.starts_with("+")
) => {
// FIXME(tigercosmos): need consider `min`, `max`, `step`, when they are implemented

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please do file an issue about this.

// TODO: Implement more value sanitization algorithms for different types of inputs
InputType::Range => {
let mut textinput = self.textinput.borrow_mut();
textinput.single_line_content_mut()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe we don't need to create the textinput variable here:

self.textinput
    .borrow_mut()
    .single_line_content_mut()
    .set_best_representation_of_the_float_number();

@KiChjang KiChjang 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 Jan 15, 2018
Comment thread components/script/dom/bindings/str.rs Outdated
year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
}

// https://html.spec.whatwg.org/multipage/#rules-for-parsing-floating-point-number-values

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe this one could be a documentation comment which should be start with ///.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good catch

@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 Jan 15, 2018
match input.parse::<f64>() {
Ok(val) if !(
// A valid number is the same as what rust considers to be valid,
// except for +1., NaN, and Infinity.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This does not account for trimming ASCII whitespace, I am also not sure this properly accounts for steps 15-17 of the spec.

Comment thread components/script/dom/bindings/str.rs Outdated
}

/// https://html.spec.whatwg.org/multipage/#rules-for-parsing-floating-point-number-values
fn parse_floating_point_number(input: &str) -> Result<String, ()> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why does this method return a String?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

do you mean return f64?
I think string is better, so we don't parse again.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually, @nox has a point here, there's no reason why this function needs to return a String, and it doesn't make sense either since the name of this function starts with parse. I would expect it to return a Result containing a floating number instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok

expected: FAIL

[range input value equals 2]
[range input value set to an integer]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why does this test now fail?

@tigercosmos tigercosmos Jan 15, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Originally pass because of accident, in other words, it is lucky.
Origin code doesn't do anything, but we now add logic.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you elaborate on what this accident is about? Is it a case of where two negatives makes a positive?

@tigercosmos tigercosmos Jan 16, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

there is no function to check the value originally, so the output is still as same as input, which made it passed. But the new code do some checking, and without step inside, the checking is not completed. That's why here is a test become to failed.

range input value equals 2 test:
the divisor is 1 as default, so 4.6 will turn to 5
but when step is 0.1, 4.6 is still 4.6, and 4.67 will turn to be 4.7

self.textinput
.borrow_mut()
.single_line_content_mut()
.set_best_representation_of_the_floating_point_number();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please add a spec link for this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure

Comment thread components/script/dom/bindings/str.rs Outdated
})
pub fn is_valid_floating_point_number_string(&self) -> bool {
// for the case that `parse_floating_point_number` cannot handle
if *&self.0.contains(" ") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this creating a reference and then immediately dereferenced? Also, this check does not look like it belongs here, but rather in parse_floating_point_number. Even so, step 6 of the parsing algorithm explicitly states to skip ASCII whitespace in front. Why is this check necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

valid floating number cannot include whitespace.
skipping the front whitespace is for parsing, which is for making the input to best presentation.

the two purpose are different. One can have whitespace and the one cannot. That's why the code here do this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This defies intuition, since this would mean an invalid floating point number string can successfully be parsed. I'm going to have to clarify the spec language with WHATWG.

Regardless of what the result may be, you still need to address the first question of my last comment.

@KiChjang KiChjang Jan 17, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Okay, so WHATWG replied that it is indeed OK to successfully parse an invalid string. Ignore that last comment and please address the first question in the first comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@KiChjang fixed

@KiChjang

Copy link
Copy Markdown
Contributor

@bors-servo r+

Thanks!

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit b29230b has been approved by KiChjang

@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 Jan 17, 2018
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit b29230b with merge 4f09987...

bors-servo pushed a commit that referenced this pull request Jan 17, 2018
implement range input sanitization

<!-- Please describe your changes on the following line: -->
implement range input sanitation.
Since there is no `min`, `max`, `step` implementation currently, this should be continued in the future.

r? KiChjang

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #19172 (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- 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/19761)
<!-- Reviewable:end -->
@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev
Approved by: KiChjang
Pushing 4f09987 to master...

@bors-servo
bors-servo merged commit b29230b into servo:master Jan 17, 2018
@highfive highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Jan 17, 2018
@tigercosmos
tigercosmos deleted the m1 branch January 17, 2018 11:25
nox pushed a commit to web-platform-tests/wpt that referenced this pull request Jan 18, 2018
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.

[meta] Implement value sanitizations for different input modes

7 participants