Skip to content

Commit

Permalink
fix: ensure 'is' functions can't crash
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Apr 4, 2019
1 parent 45f1ddd commit 59ceb78
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
10 changes: 7 additions & 3 deletions array/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ module.exports = function (value) {

// Sanity check (reject objects which do not expose common Array interface)
if (!hasOwnProperty.call(value, "length")) return false;
if (typeof value.length !== "number") return false;
if (typeof value.push !== "function") return false;
if (typeof value.splice !== "function") return false;
try {
if (typeof value.length !== "number") return false;
if (typeof value.push !== "function") return false;
if (typeof value.splice !== "function") return false;
} catch (error) {
return false;
}

return !isPrototype(value);
};
17 changes: 10 additions & 7 deletions date/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ var dateValueOf = Date.prototype.valueOf;
module.exports = function (value) {
if (!value) return false;

// Sanity check (reject objects which do not expose common Date interface)
if (typeof value.getFullYear !== "function") return false;
if (typeof value.getTimezoneOffset !== "function") return false;
if (typeof value.setFullYear !== "function") return false;
try {
// Sanity check (reject objects which do not expose common Date interface)
if (typeof value.getFullYear !== "function") return false;
if (typeof value.getTimezoneOffset !== "function") return false;
if (typeof value.setFullYear !== "function") return false;

// Ensure its native Date object (has [[DateValue]] slot)
try { dateValueOf.call(value); }
catch (error) { return false; }
// Ensure its native Date object (has [[DateValue]] slot)
dateValueOf.call(value);
} catch (error) {
return false;
}

// Ensure it hosts valid date
if (isNaN(value)) return false;
Expand Down
6 changes: 5 additions & 1 deletion error/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ module.exports = function (value) {
if (!value) return false;

// Sanity check (reject objects which do not expose common Error interface)
if (typeof value.message !== "string") return false;
try {
if (typeof value.message !== "string") return false;
} catch (error) {
return false;
}

// Ensure its native Error object (has [[ErrorData]] slot)
// Note: it's not 100% precise as string tag may be overriden
Expand Down
10 changes: 7 additions & 3 deletions reg-exp/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ module.exports = function (value) {

// Sanity check (reject objects which do not expose common RegExp interface)
if (!hasOwnProperty.call(value, "lastIndex")) return false;
if (typeof value.lastIndex !== "number") return false;
if (typeof value.test !== "function") return false;
if (typeof value.exec !== "function") return false;
try {
if (typeof value.lastIndex !== "number") return false;
if (typeof value.test !== "function") return false;
if (typeof value.exec !== "function") return false;
} catch (error) {
return false;
}

// Ensure its native RegExp object (has [[RegExpMatcher]] slot)
if (isToStringTagSupported && typeof value[Symbol.toStringTag] === "string") {
Expand Down

0 comments on commit 59ceb78

Please sign in to comment.