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

Avoid prototype builtin hasOwnProperty #577

Merged
merged 2 commits into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions multipart/formfile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { hasOwnProperty } from "../util/has_own_property.ts";

/** FormFile object */
export interface FormFile {
Expand All @@ -19,9 +20,5 @@ export interface FormFile {
/** Type guard for FormFile */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isFormFile(x: any): x is FormFile {
return (
typeof x === "object" &&
x.hasOwnProperty("filename") &&
x.hasOwnProperty("type")
);
return hasOwnProperty(x, "filename") && hasOwnProperty(x, "type");
}
12 changes: 12 additions & 0 deletions multipart/formfile_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ test(function multipartIsFormFile(): void {
false
);
});

test(function isFormFileShouldNotThrow(): void {
assertEquals(
isFormFile({
filename: "foo",
type: "application/json",
hasOwnProperty: "bar"
}),
true
);
assertEquals(isFormFile(Object.create(null)), false);
});
30 changes: 30 additions & 0 deletions util/has_own_property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

/**
* Determines whether an object has a property with the specified name.
* Avoid calling prototype builtin `hasOwnProperty` for two reasons:
*
* 1. `hasOwnProperty` is defined on the object as something else:
*
* const options = {
* ending: 'utf8',
* hasOwnProperty: 'foo'
* };
* options.hasOwnProperty('ending') // throws a TypeError
*
* 2. The object doesn't inherit from `Object.prototype`:
*
* const options = Object.create(null);
* options.ending = 'utf8';
* options.hasOwnProperty('ending'); // throws a TypeError
*
* @param obj A Object.
* @param v A property name.
* @see https://eslint.org/docs/rules/no-prototype-builtins
*/
export function hasOwnProperty<T>(obj: T, v: PropertyKey): boolean {
if (obj == null) {
return false;
}
return Object.prototype.hasOwnProperty.call(obj, v);
}
3 changes: 2 additions & 1 deletion ws/mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

import { decode, encode } from "../strings/mod.ts";
import { hasOwnProperty } from "../util/has_own_property.ts";

type Conn = Deno.Conn;
type Writer = Deno.Writer;
Expand Down Expand Up @@ -34,7 +35,7 @@ export interface WebSocketCloseEvent {
export function isWebSocketCloseEvent(
a: WebSocketEvent
): a is WebSocketCloseEvent {
return typeof a === "object" && a.hasOwnProperty("code");
return hasOwnProperty(a, "code");
}

export type WebSocketPingEvent = ["ping", Uint8Array];
Expand Down