Skip to content

Change extend util to check hasOwnProperty#380

Merged
ryanseys merged 1 commit into
googleapis:masterfrom
stephen:stephen-fix-extend
Mar 3, 2015
Merged

Change extend util to check hasOwnProperty#380
ryanseys merged 1 commit into
googleapis:masterfrom
stephen:stephen-fix-extend

Conversation

@stephen

@stephen stephen commented Mar 3, 2015

Copy link
Copy Markdown
Contributor

The lack of a hasOwnProperty check can accidentally cause functions on the prototype to bubble up to being a parameter in an API request.

For example... defining a polyfill:

Object.prototype.entries = function* entries(obj) {
  for (let key of Object.keys(obj)) {
    yield [key, obj[key]];
  }
};

will cause an extra entries param to exist in a request in the query string, e.g. https://www.googleapis.com:443'/calendar/v3/calendars/stephen%40stephenwan.net/events?entries=.

@ryanseys

ryanseys commented Mar 3, 2015

Copy link
Copy Markdown
Contributor

Please add a test.

@stephen

stephen commented Mar 3, 2015

Copy link
Copy Markdown
Contributor Author

@ryanseys - Added a test that fails before the changes to extend, but passes currently. Satisfactory? Just checks that a prototyped function doesn't get copied over.

@stephen stephen force-pushed the stephen-fix-extend branch from b5c14f5 to 6fae09c Compare March 3, 2015 06:01
@ryanseys

ryanseys commented Mar 3, 2015

Copy link
Copy Markdown
Contributor

Ahaha, jslint won't even let the build pass with it because extending Object is so bad. I guess we'll have to the scrap the test. I would highly suggest you don't extend native objects like that and would go as far to say that it's really not our fault if you do that and our code breaks because of it, but because this change is relatively straightforward and using hasOwnProperty is sort of "standard practice" in JavaScript due to limitations of for .. in .. I'll let it slide. God's speed for .. of ..

@stephen

stephen commented Mar 3, 2015

Copy link
Copy Markdown
Contributor Author

@ryanseys - I added a jshint ignore for the one line, so we can keep the test around and not have travis complain.

Generally agree with not extending native objects, but I believe Object.entries is (was?) being considered at some point for ES7 support, and is fairly useful - https://esdiscuss.org/topic/object-entries-object-values.

Comment thread test/test.utils.js

This comment was marked as spam.

This comment was marked as spam.

@ryanseys

ryanseys commented Mar 3, 2015

Copy link
Copy Markdown
Contributor

Cool :) Thanks! Any way you could finally squash into 1 commit? Sorry for the troubles and thanks for your PR.

Add test for extend function
@stephen stephen force-pushed the stephen-fix-extend branch from 0232f50 to 9554de3 Compare March 3, 2015 06:15
@stephen

stephen commented Mar 3, 2015

Copy link
Copy Markdown
Contributor Author

No worries - was just in the middle of rebasing actually. 😸

ryanseys added a commit that referenced this pull request Mar 3, 2015
Change extend util to check hasOwnProperty
@ryanseys ryanseys merged commit cd14418 into googleapis:master Mar 3, 2015
@ryanseys

ryanseys commented Mar 3, 2015

Copy link
Copy Markdown
Contributor

Thanks! Released in v1.1.5!

@robertrossmann

Copy link
Copy Markdown
Contributor

I shall only add here as a tip that extending native objects this way is indeed considered a terrible practice exactly because of the side-effects you observed.

If you really need to do so, you must do it "properly", making sure your extensions are not in an enumerable property.

Object.defineProperty(Array.prototype, 'entries',
  { enumerable: false // default if omitted
  , value: function entries () { /* logic here */ }
  }
)

@stephen

stephen commented Mar 3, 2015

Copy link
Copy Markdown
Contributor Author

@Alaneor - thanks for the tip. Realized that this was also causing a problem in request as well, and ended up switching to defineProperty.

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.

3 participants