Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Create a build of AngularJS that contains no CSS. #8459

Closed
wants to merge 1 commit into from

Conversation

realityking
Copy link
Contributor

This is useful for sites that use CSP, especially those that prohibit inline CSS but allow eval.

@petebacondarwin
Copy link
Member

Which environments allow eval but not inline CSS?

@petebacondarwin petebacondarwin added this to the Backlog milestone Aug 4, 2014
@realityking
Copy link
Contributor Author

This is mostly useful for sites with custom CSP headers, not for a certain environment like a browser extensions.

We prohibit inline styles on our projects but on some we're not ready to give up the performance increase eval offers. The resulting warning is mostly annoying but it also looks kinda unprofessional when a visitor happens to look into the console. It's also annoying because we happen to log these errors on the server.

@btford btford removed the gh: issue label Aug 20, 2014
This is useful for sites that use CSP, especially those that prohibit inline CSS but allow eval.
@realityking
Copy link
Contributor Author

Any news or feedback on this?

@petebacondarwin petebacondarwin self-assigned this Sep 5, 2014
@petebacondarwin
Copy link
Member

Checking with the core team...

@IgorMinar
Copy link
Contributor

What's the goal here? Size reduction or avoiding an exception/warning from CSP? If it's the latter then we should look into how we could avoid it via ng-csp attribute. If size reduction is the goal then I don't think it's worth it because the css is tiny.

@petebacondarwin
Copy link
Member

I believe the goal is the latter. @IgorMinar Are you saying that we could make ng-csp configurable so that it could allow eval while restricting inline CSS?

@IgorMinar
Copy link
Contributor

Yeah
On Sep 5, 2014 10:57 AM, "Pete Bacon Darwin" notifications@github.com
wrote:

I believe the goal is the latter. @IgorMinar
https://github.com/IgorMinar Are you saying that we could make ng-csp
configurable so that it could allow eval while restricting inline CSS?


Reply to this email directly or view it on GitHub
#8459 (comment).

@petebacondarwin
Copy link
Member

OK, so I took a look into this. I think that there is a reasonable work around for this specific case that doesn't require a change to the Angular build or code.

The key is to trick the $sniffer service into thinking that CSP mode is not enabled. The parser calls this to determine whether to use eval or not.

  • use ng-csp in your HTML to ensure that the inline CSS is not injected. This will also prevent the CSP automatic detection routine from running.
<html ng-csp>
  • decorate the $sniffer to override the .csp() method, so that it returns false:
angular.module('app', []).
decorate('$sniffer', function($delegate) {
  $delegate.csp = function() { return false; };
  return $delegate;
});

I haven't yet tested this code but you might want to give it a try.

@IgorMinar
Copy link
Contributor

That will also disable fn generation that speeds up dirty-checking, which might not be desirable.

@petebacondarwin
Copy link
Member

From looking at the code, I don't think so:
https://github.com/angular/angular.js/blob/master/src/ng/parse.js#L1014

The parser checks $sniffer.csp and disables fn generation only if this is true.

Actually my code is slightly wrong since $sniffer.csp is actually a property not a function...

angular.module('app', []).
decorate('$sniffer', function($delegate) {
  $delegate.csp = false;
  return $delegate;
});

@petebacondarwin
Copy link
Member

@realityking - is my workaround good enough for you?

@realityking
Copy link
Contributor Author

@petebacondarwin I probably won't be able to test this until the weekend as we're in the middle of a bigger release rigth now.

@petebacondarwin
Copy link
Member

No problem.

@realityking
Copy link
Contributor Author

I finally got a chance to test this. Your workaround works completely fine. There's no warning about styles being inserted and I manually checked that function generation is still used. While I think my solution is more elegant, this is completely serviceable. Thank you very much!

@petebacondarwin
Copy link
Member

I agree that your solution was more elegant but I am glad that you now have a serviceable solution.

petebacondarwin added a commit to petebacondarwin/angular.js that referenced this pull request Jul 14, 2015
There are two different features in Angular that can break CSP rules:
use of `eval` to execute a string as JavaScript and dynamic injection of
CSS style rules into the DOM.

This change allows us to configure which of these features should be turned
off to allow a more fine grained set of CSP rules to be supported.

Closes angular#11933
Closes angular#8459
petebacondarwin added a commit to petebacondarwin/angular.js that referenced this pull request Jul 15, 2015
There are two different features in Angular that can break CSP rules:
use of `eval` to execute a string as JavaScript and dynamic injection of
CSS style rules into the DOM.

This change allows us to configure which of these features should be turned
off to allow a more fine grained set of CSP rules to be supported.

Closes angular#11933
Closes angular#8459
petebacondarwin added a commit to petebacondarwin/angular.js that referenced this pull request Jul 15, 2015
There are two different features in Angular that can break CSP rules:
use of `eval` to execute a string as JavaScript and dynamic injection of
CSS style rules into the DOM.

This change allows us to configure which of these features should be turned
off to allow a more fine grained set of CSP rules to be supported.

Closes angular#11933
Closes angular#8459
petebacondarwin added a commit that referenced this pull request Jul 16, 2015
There are two different features in Angular that can break CSP rules:
use of `eval` to execute a string as JavaScript and dynamic injection of
CSS style rules into the DOM.

This change allows us to configure which of these features should be turned
off to allow a more fine grained set of CSP rules to be supported.

Closes #11933
Closes #8459
Closes #12346
netman92 pushed a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
There are two different features in Angular that can break CSP rules:
use of `eval` to execute a string as JavaScript and dynamic injection of
CSS style rules into the DOM.

This change allows us to configure which of these features should be turned
off to allow a more fine grained set of CSP rules to be supported.

Closes angular#11933
Closes angular#8459
Closes angular#12346
ggershoni pushed a commit to ggershoni/angular.js that referenced this pull request Sep 29, 2015
There are two different features in Angular that can break CSP rules:
use of `eval` to execute a string as JavaScript and dynamic injection of
CSS style rules into the DOM.

This change allows us to configure which of these features should be turned
off to allow a more fine grained set of CSP rules to be supported.

Closes angular#11933
Closes angular#8459
Closes angular#12346
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants