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

Don't inject application/json as javascript #400

Merged
merged 1 commit into from
May 24, 2016
Merged

Don't inject application/json as javascript #400

merged 1 commit into from
May 24, 2016

Conversation

tchiotludo
Copy link
Contributor

We can have script tag with json data

<script id="data" type="application/json">{org: 10, items:["one","two"]}</script>

These tag is not evaluated by browers as javascript if type="application/json" is present.
This PR prevent spf to inject these tags as javascript

@@ -582,8 +582,11 @@ spf.nav.response.extract_ = function(frag) {
var url = attr.match(spf.nav.response.AttributeRegEx.SRC);
url = url ? url[1] : '';
var async = spf.nav.response.AttributeRegEx.ASYNC.test(attr);
result['scripts'].push(
{url: url, text: text, name: name, async: async});
var json = spf.nav.response.AttributeRegEx.JSON.test(attr);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is important, but I have a couple of issues.

  1. As is, this'll drop the script entirely. It's probably a fine first step, as it's pretty close to the current behavior, but we probably need to allow these to be available in some capacity, either in the DOM or in some other structure. Since you presumably have a use case, thoughts?
  2. This is probably better off as a Whitelist of valid types instead of a blacklist. How about only allowing scripts with empty types or type='text/javascript'?
  3. Please add tests.

@tchiotludo
Copy link
Contributor Author

  1. the script is not drop, it was inject in the dom as is without any modification but not send to javascript engine, In my case, I use this kind of script <script type="application/json"> to inject configuration for web component & locale available only on the server side, I can get the dom element and parse it with a simple JSON.parse(document.getElementById("main"))
  2. good idea, done
  3. also done

@tchiotludo
Copy link
Contributor Author

I understand the point 1) now.
I didn't see the frag.replace function that drop the script ...
It's fixed now.

{url: url, text: text, name: name, async: async});
return '';
var type = spf.nav.response.AttributeRegEx.TYPE.exec(attr);
var inject = !type || type[1].indexOf('/javascript') > 0 ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use the spf.string.contains function so the compiler can re-use the function call:

var inject = !type || spf.string.contains(type[1], '/javascript') ||
    spf.string.contains(type[1], '/x-javascript') ||
    spf.string.contains(type[1], '/ecmascript');

@nicksay
Copy link
Contributor

nicksay commented May 9, 2016

Thanks for the PR! This is looking good; I've just added a couple minor comments, but after those are fixed, I'll merge this in.

@nicksay nicksay added this to the SPF 24 (v2.4.0) milestone May 9, 2016
@tchiotludo
Copy link
Contributor Author

I've just made last cleanup & squash commits

@nicksay
Copy link
Contributor

nicksay commented May 23, 2016

Hi there! Unfortunately it looks the constant definition was lost in the last update:

src/client/nav/response.js:585: ERROR - element TYPE does not exist on this enum
          var type = spf.nav.response.AttributeRegEx.TYPE.exec(attr);

https://travis-ci.org/youtube/spfjs/builds/131329855

@tchiotludo
Copy link
Contributor Author

oups, sorry. It's ok now.
I've added text/css for style tag that is also concern

@nicksay
Copy link
Contributor

nicksay commented May 24, 2016

Great, the last thing I'll need is for you to update onto the latest changes from master so I can merge this!

@tchiotludo
Copy link
Contributor Author

Great news, it's merged

@nicksay
Copy link
Contributor

nicksay commented May 24, 2016

Would you mind squashing your commits?

@tchiotludo
Copy link
Contributor Author

I think it's done, I'm not confortable with git rebase ...

@nicksay
Copy link
Contributor

nicksay commented May 24, 2016

Looks perfect, thanks!

@nicksay nicksay merged commit cf4b0f4 into youtube:master May 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants