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

eval() xhr response of type application/javascript as well #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

glaszig
Copy link

@glaszig glaszig commented Aug 4, 2019

limiting execution of xhr responses to the content type text/javascript does not make much sense, i think.

meanwhile, everybody else can use this as part of you application code:

document.addEventListener("ajax:complete", function(e) {
  var xhr = e.detail;
  var responseType = xhr.getResponseHeader('content-type');
  if(responseType === 'text/javascript; charset=utf-8') {
    return;
  }
  if (/(application|text)\/javascript/i.test(responseType)) {
    eval(xhr.response);
  }
});

@walterdavis
Copy link
Contributor

I think that the point of the original behavior was to exactly mimic the way that jquery-ujs (and before that, the original Prototype.js-based library) behaved. When you add remote: true to your link or form submission or whatever, the request is sent under a text/javascript header. If you are thinking about expanding the purpose of this library beyond Rails, then I could see liteajax needing additional flexibility in this area. But as a part of a Rails-centered UJS solution, the current behavior is precisely what is needed.

@glaszig
Copy link
Author

glaszig commented Aug 4, 2019

the request is sent under a text/javascript header

the LiteAjax component sets an accept header of text/javascript, application/javascript, application/ecmascript, application/x-ecmascript by default. so, any of these are valid, interpretable responses and should be interpreted.

but then it only evaluates text/javascript? that doesn't make any sense and it's not how browsers behave either.

speaking of "rails-centered", they are handling this even more flexibly:

processResponse = (response, type) ->
  if typeof response is 'string' and typeof type is 'string'
    if type.match(/\bjson\b/)
      try response = JSON.parse(response)
    else if type.match(/\b(?:java|ecma)script\b/)
      script = document.createElement('script')
      script.setAttribute('nonce', cspNonce())
      script.text = response
      document.head.appendChild(script).parentNode.removeChild(script)
    else if type.match(/\b(xml|html|svg)\b/)
      parser = new DOMParser()
      type = type.replace(/;.+/, '') # remove something like ';charset=utf-8'
      try response = parser.parseFromString(response, type)
  response

prototype does the same:

      var contentType = response.getHeader('Content-type');
      if (this.options.evalJS == 'force'
          || (this.options.evalJS && this.isSameOrigin() && contentType
          && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i)))
        this.evalResponse();

@glaszig
Copy link
Author

glaszig commented Sep 10, 2020

bump

1 similar comment
@glaszig
Copy link
Author

glaszig commented Sep 10, 2020

bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants