Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent CSS interference in an injected piece of HTML?

I'm currently developing a Safari extension that uses an injected script to further inject some HTML into the current webpage, as well as injecting some other scripts to make it work. This is all working fine, but the issue is that the HTML that is injected gets affected by CSS stylesheets that the webpage has already imported. For example, the HTML looks perfect on Google.com (which has relatively little CSS styling), but awful on StackOverflow.com (which styles buttons etc).

jQuery is injected into the webpage at the time of this HTML being displayed, so I have that available. I've tried all kinds of things, including walking through all of the elements and calling removeClass() on each of them, to no avail. I've also tried to add "CSS reset" classes, etc, but nothing seems to be working.

What's the best way to go around preventing the CSS from interfering with my HTML?

like image 748
Anshu Chimala Avatar asked Aug 22 '10 09:08

Anshu Chimala


People also ask

What are the ways CSS are injected to HTML files?

CSS can be added to HTML documents in 3 ways: Inline - by using the style attribute inside HTML elements. Internal - by using a <style> element in the <head> section. External - by using a <link> element to link to an external CSS file.

What is CSS injection?

A CSS Injection vulnerability involves the ability to inject arbitrary CSS code in the context of a trusted web site which is rendered inside a victim's browser. The impact of this type of vulnerability varies based on the supplied CSS payload. It may lead to cross site scripting or data exfiltration.

Why is it important to separate CSS from HTML?

The separation of HTML from CSS makes it easier to maintain sites, share style sheets across pages, and tailor pages to different environments. This is referred to as the separation of structure (or: content) from presentation.

How does browser affect CSS?

The browser parses the HTML and creates a DOM from it. Next, it parses the CSS. Since the only rule available in the CSS has a span selector, the browser sorts the CSS very quickly! It applies that rule to each one of the three <span> s, then paints the final visual representation to the screen.


2 Answers

You can't prevent that from happen. However, you can override the CSS rules. Give your main element a unique id (which really should be unique by obfustation, like "yourapplicationname_mainelement_name" or something), then override all possible styles that might give strange effects on your html.

Your plugin:

<div id="yourapplicationname_mainelement_name">
  <p>My paragraph that must not be styled</p>
</div>

Your css:

#yourapplicationname_mainelement_name p {
  display: block;
  color: black;
  background: white;
  position: relative;
  ... and so on ...
}

As your css style rules are the most specific, given your id, they will override any settings present on the page where your html is injected.

Further... It might be hard to see what rules are the most important. You can use firebug or similar to understand which is overriding another. You'll have a hard time without it when developing your application.

like image 172
Johan Avatar answered Sep 27 '22 17:09

Johan


that's a tough one. two options as I see it. You could set a wrapping div around all your content and prefix all your css with that. example:

<body>
  <div class='wrappingDiv'>
     ...
  </div>
</body>

stylesheet:

.wrappingDiv * {}

Then when you inject jquery use that to close off the initial wrapping div before your content and to wrap any following content in the another wrapping div.

Issues:

  1. Only possible if you are injecting other site content onto your own site.
  2. This could get complicated depending on where you are injecting html.

The other option is to load a resetting stylesheet that targets your injected html specifically. In this case only your injected html would be wrapped but you'd need a css file that reset all attributes for all tags to their default before you add your own styles. No real issues here, just not very elegant...

Another way would be to use an element that doesn't inherit stylesheet like an iframe, but that comes with its own issues...

like image 27
Stuart Burrows Avatar answered Sep 27 '22 17:09

Stuart Burrows