Skip to content

Node6 incompatibilities with querystrings #571

Open
@cmidkiff87

Description

@cmidkiff87

Node 6 changed the querystring.parse to return an object whose __proto__ property no longer points back to Object. This was done to allow for more esoteric query string names, such as https://example.com/route?__proto__=some-value, which would conflict with the native js object properties.

However, this causes js-data to error when calling hasOwnProperty() if you pass it the querystring object.

This is especially problematic when using js-data-http as it automatically sends the requests with the url's query strings in the format js-data expects.

It can be fixed by using

Object.prototype.hasOwnProperty(query, propKey);

in place of

query.hasOwnProperty(propKey);

The libraries involved are

  • api:
    • node 5.11.1 // yikes
    • node 6.17.1
    • js-data 3.0.8
    • koa 1.7.0
  • webapp
    • js-data 3.0.0-rc.4
    • js-data-http 3.0.0-rc.2

It can also be fixed externally by shimming the hasOwnProperty() function onto the querystring

exports.jsData = function() {
  // koa 1.x middleware
  return function*(next) {
    this.jsData = {};

    // `this.query` is the result of the `querystring.parse()` function
    this.jsData.query = this.query; 
    
    function hasOwnPropertyShim(key) {
      // `this` === `this.jsData.query`
      return Object.hasOwnProperty.call(this, key);
    }

    // make the prop unenumerable, or it will get included in the sql query
    Object.defineProperty(this.jsData.query, 'hasOwnProperty', {
      value: hasOwnPropertyShim,
      enumerable: false,
    });

    // ...
  }
}

However, that solution isn't super intuitive without some digging

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy