Skip to content

Make check for meta tag to verify site more permissive#10040

Merged
abidhahmed merged 11 commits intomasterfrom
fix/meta-tag-verify-site
Aug 28, 2018
Merged

Make check for meta tag to verify site more permissive#10040
abidhahmed merged 11 commits intomasterfrom
fix/meta-tag-verify-site

Conversation

@Tug
Copy link
Copy Markdown
Contributor

@Tug Tug commented Aug 21, 2018

Changes proposed in this Pull Request:

The validation for meta tags entered in the Jetpack Settings page for site verification is overly strict. In fact, even the same meta tags we offer in our alternative UI (under "tools" in the wp-admin menu) don't work! (because they aren't self-closing tags, lack a space before the >, and use single instead of double quotes).

This PR updates the input validation we have for meta tags used for website verification services. We just want a meta tag that is valid enough so we can extract the value of the content property from it. HTML parser on browsers are quite flexible so we need to allow all kinds of valid meta tags.
For instance this change makes it so those are valid:

  • <meta name="google-site-verification" content="1234"/> (no space before />)
  • <meta name='google-site-verification' content='1234' /> (use of ' instead of ")
  • <meta name='google-site-verification' content=1234 /> (does not use any quotes)
  • <meta content="1234" name="google-site-verification" /> (switches the order)
  • <meta name="google-site-verification" content="1234" some-prop /> (has extra properties)
  • <meta name="google-site-verification" content="1234"> (does not have a closing character)
    ...

Testing instructions:

Enter any of the formats above, both in the Jetpack Settings UI, and the same UI under "tools".

The content should be saved successfully without validation errors.

Enter a "bad" string in the JS UI, e.g. <moota name="google-site-verification" content="1234"/>

The content should fail to save with a validation error.

Note that the legacy UI under "tools" will actually accept <moota name="google-site-verification" content="1234"/> - that UI is going to be removed eventually so it's less of a concern.

Proposed changelog entry for your changes:

  • Update input validation for meta tags given in site verification

@Tug Tug self-assigned this Aug 21, 2018
@Tug Tug requested a review from a team as a code owner August 21, 2018 18:26
@jetpackbot
Copy link
Copy Markdown
Collaborator

That's a great PR description, thank you so much for your effort!

Generated by 🚫 dangerJS

@gravityrail gravityrail self-requested a review August 21, 2018 22:12
@gravityrail
Copy link
Copy Markdown
Contributor

gravityrail commented Aug 21, 2018

I found a bug: pasting in a meta tag with single quotes and the meta name "blah" results in the following output in the HTML:

<meta name="google-site-verification" content="&lt;meta name=&#039;blah&#039; content=&#039;dBw5CvburAxi537Rp9qi5uG2174Vb6JwHwIRwPSLIK8&#039;&gt;" />

Trying to fix now.

@Tug
Copy link
Copy Markdown
Contributor Author

Tug commented Aug 21, 2018

@gravityrail it's not a regex that's able to parse any html property value, just a verification code which according to the regex for the code only (on the same line) is simply [a-z0-9_-]+

@gravityrail
Copy link
Copy Markdown
Contributor

I get the same issue when I enter this value:

<meta name='google-site-verification' content='dBw5CvburAxi537Rp9qi5uG2174Vb6JwHwIRwPSLIK8'>

The meta tag shows:

<meta name="google-site-verification" content="&lt;meta name=&#039;google-site-verification&#039; content=&#039;dBw5CvburAxi537Rp9qi5uG2174Vb6JwHwIRwPSLIK8&#039;&gt;" />

@gravityrail
Copy link
Copy Markdown
Contributor

But it works correctly when I enter:

<meta name="google-site-verification" content="dBw5CvburAxi537Rp9qi5uG2174Vb6JwHwIRwPSLIK8" />

(same values as above, but tag is self-closing and uses double-quotes)

This outputs correctly:

<meta name="google-site-verification" content="dBw5CvburAxi537Rp9qi5uG2174Vb6JwHwIRwPSLIK8" />

Copy link
Copy Markdown
Contributor

@gravityrail gravityrail left a comment

Choose a reason for hiding this comment

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

Works now - hooray!

@gravityrail gravityrail added [Status] Needs Review This PR is ready for review. Bug When a feature is broken and / or not performing as intended and removed [Status] In Progress labels Aug 21, 2018
@gravityrail gravityrail added this to the 6.5 milestone Aug 21, 2018
@cfinke
Copy link
Copy Markdown
Contributor

cfinke commented Aug 22, 2018

This line:

foreach( [ 'google', 'bing', 'yandex' ] as $key ) {

isn't valid syntax in PHP 5.2 and is preventing new builds from being created by TravisCI, so I can't test the current code.

*/
public static function validate_verification_service( $value = '', $request, $param ) {
if ( ! empty( $value ) && ! ( is_string( $value ) && ( preg_match( '/^[a-z0-9_-]+$/i', $value ) || preg_match( '#^<meta name="([a-z0-9_\-.:]+)?" content="([a-z0-9_-]+)?" />$#i', $value ) ) ) ) {
if ( ! empty( $value ) && ! ( is_string( $value ) && ( preg_match( '/^[a-z0-9_-]+$/i', $value ) || preg_match( '#^<meta[^<>]+content=["\']?([^"\' <>]*)[^<>]*/?>$#i', $value ) ) ) ) {
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.

If the content attribute has an apostrophe or quotation mark in it, this regex will fail.

<meta content="Jetpack's the best" />

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.

It could also perceive this HTML as passing the check:

<meta name="content=" />

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.

Or even <meta-content=>. Instead of duplicating work, this function should be calling jetpack_verification_get_code() and checking if it returns something, since that's the code that will actually be extracting the verification code. (jetpack_verification_get_code() should also be given a second look, as its regex is pretty liberal as well.)

Copy link
Copy Markdown
Contributor Author

@Tug Tug Aug 22, 2018

Choose a reason for hiding this comment

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

As I said here this regex will only work for valid content values. I agree it's cleaner to use jetpack_verification_get_code() here instead though 👍I'll update!

@Tug
Copy link
Copy Markdown
Contributor Author

Tug commented Aug 22, 2018

I updated the form on the client to replace the code value by the full meta tag. On the server nothing is changed and the code value is persisted by default (and the code stays backward-compatible with full meta tag saved in the db).

@mdawaffe
Copy link
Copy Markdown
Member

I don't think it matters too much that the regex in jetpack_verification_get_code() is a bit forgiving. It seems to me the failure case is: someone didn't paste quite the right thing, so they try again.

The latest changes have a some failing tests, though.

@cbauerman cbauerman added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review This PR is ready for review. labels Aug 22, 2018
@abidhahmed abidhahmed merged commit 51564c2 into master Aug 28, 2018
@ghost ghost removed the [Status] Ready to Merge Go ahead, you can push that green button! label Aug 28, 2018
@abidhahmed abidhahmed deleted the fix/meta-tag-verify-site branch August 28, 2018 07:39
@jeherve jeherve added [Feature] Verification tools Admin Page React-powered dashboard under the Jetpack menu and removed General labels Sep 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Admin Page React-powered dashboard under the Jetpack menu Bug When a feature is broken and / or not performing as intended [Feature] Verification tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants