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

Errors with malformed CSS #179

Closed
Synchro opened this issue Jun 19, 2015 · 7 comments
Closed

Errors with malformed CSS #179

Synchro opened this issue Jun 19, 2015 · 7 comments
Labels
Milestone

Comments

@Synchro
Copy link
Contributor

Synchro commented Jun 19, 2015

I ran into two errors when processing some malformed style tags.

The style tag contained some stray HTML injected by ckeditor:

table.columns .right-text-pad {
  padding-right: 10px;
}
<style data-cke-temp="1">html{cursor:text;*cursor:auto}
img,input,textarea{cursor:default}

Trying to process this produced two errors. The first is caused by it choosing <style data-cke-temp="1">html as a selector, which will not convert to a valid xpath query:

2015-06-19 14:02:03 DOMXPath::query(): Invalid expression (error type 2 in ./vendor/pelago/emogrifier/Classes/Emogrifier.php on line 279)

The second is a consequence of this, attempting to iterate over the failed result of the query because the return value is not checked:

2015-06-19 14:02:03 Invalid argument supplied for foreach() (error type 2 in ./vendor/pelago/emogrifier/Classes/Emogrifier.php on line 282)

Obviously it would be better if bad CSS was not passed in in the first place, but it would be better to ignore it than throw errors.

@oliverklee
Copy link
Contributor

As for the cause of the error: I'm for having a whitelist of characters (i.e., a regular expression) which we can use to validate those selectors before using them.

Synchro added a commit to Synchro/emogrifier that referenced this issue Jun 19, 2015
@Synchro
Copy link
Contributor Author

Synchro commented Jun 19, 2015

I had a little look into matching selectors - it's not simple at all! I think the only invalid char in that selector is < - everything else is valid in other contexts.

oliverklee pushed a commit that referenced this issue Jul 5, 2015
oliverklee pushed a commit that referenced this issue Jul 5, 2015
oliverklee pushed a commit that referenced this issue Jul 5, 2015
@oliverklee oliverklee added this to the 1.0.0 milestone Jul 6, 2015
@oliverklee
Copy link
Contributor

So I propose discarding any selectors that contain a "<". What do you think about this?

And would you be willing to create a PR for this?

@Synchro
Copy link
Contributor Author

Synchro commented Sep 30, 2015

Yes, I think that's reasonable. Where would I find the code that identifies selectors?

@oliverklee
Copy link
Contributor

That should be https://github.com/jjriv/emogrifier/blob/master/Classes/Emogrifier.php#L337 in the method parseSelectors.

@Synchro
Copy link
Contributor Author

Synchro commented Oct 2, 2015

As I've been reading, it's very hard to match CSS selectors with a regex as they are not a regular grammar. I'm thinking that it would be easier to apply a simple filter to remove stray tags before trying to parse the styles, for example:

$css = preg_replace('/<[^>]*>/', '', $css);

or even a simple strip_tags(). While this is something of an edge case, it strikes me that this should be both harmless and effective as I can't think of any legitimate reason there should be HTML tags inside CSS.

@oliverklee oliverklee modified the milestones: 1.1.0, 1.0.0 Oct 10, 2015
@oliverklee oliverklee modified the milestones: 1.2.0, 1.1.0 Aug 22, 2016
@oliverklee oliverklee modified the milestones: 1.3.0, 1.2.0 Mar 2, 2017
@oliverklee oliverklee modified the milestones: 2.0.0, 2.1.0 Oct 23, 2017
@oliverklee oliverklee modified the milestones: 2.1.0, 3.0.0 Apr 3, 2018
@JakeQZ
Copy link
Contributor

JakeQZ commented Sep 11, 2019

This was fixed by #400 (in 2.0.0), with some prior relevant changes in #361 and #392. The unit test EmogrifierTest::emogrifyNotInDebugModeIgnoresInvalidCssSelectors() (there's a similar one for CssInliner) should ensure that rules with invalid selectors are silently ignored, unless debug mode is enabled.

So I'm closing this now.

@JakeQZ JakeQZ closed this as completed Sep 11, 2019
@JakeQZ JakeQZ modified the milestones: 3.0.0, 2.0.0 Sep 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants