Skip to content

Commit f934b22

Browse files
authoredSep 26, 2023
Merge pull request from GHSA-4q6p-r6v2-jvc5
1 parent 1436af2 commit f934b22

File tree

3 files changed

+9027
-6174
lines changed

3 files changed

+9027
-6174
lines changed
 

‎index.js

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
const { toString } = Function.prototype;
1515
const functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*/)]+\*\/\s*)*([^\s(/]+)/;
16+
const maxFunctionSourceLength = 512;
1617
function getFuncName(aFunc) {
1718
if (typeof aFunc !== 'function') {
1819
return null;
@@ -22,6 +23,12 @@ function getFuncName(aFunc) {
2223
if (typeof Function.prototype.name === 'undefined' && typeof aFunc.name === 'undefined') {
2324
// Here we run a polyfill if Function does not support the `name` property and if aFunc.name is not defined
2425
// eslint-disable-next-line prefer-reflect
26+
const functionSource = toString.call(aFunc);
27+
// To avoid unconstrained resource consumption due to pathalogically large function names,
28+
// we limit the available return value to be less than 512 characters.
29+
if (functionSource.indexOf('(') > maxFunctionSourceLength) {
30+
return name;
31+
}
2532
const match = toString.call(aFunc).match(functionNameMatch);
2633
if (match) {
2734
[ name ] = match;

‎package-lock.json

+9,007-6,174
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ describe('getFuncName', function () {
3131
assert(getFuncName(anonymousFunc) === '');
3232
});
3333

34+
it('should return an empty string for overly large function names', function () {
35+
// eslint-disable-next-line max-len, func-style, func-name-matching, id-length
36+
const longFunc = function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() {};
37+
Object.defineProperty(longFunc, 'name', { value: undefined });
38+
// Temporarily disable the Function.prototype.name getter
39+
const realFPName = Object.getOwnPropertyDescriptor(Function.prototype, 'name');
40+
// eslint-disable-next-line no-extend-native
41+
Object.defineProperty(Function.prototype, 'name', { value: undefined });
42+
assert(getFuncName(longFunc) === '');
43+
// eslint-disable-next-line no-extend-native
44+
Object.defineProperty(Function.prototype, 'name', realFPName);
45+
});
46+
3447
it('should return `null` when passed a String as argument', function () {
3548
assert(getFuncName('') === null);
3649
});

0 commit comments

Comments
 (0)
Please sign in to comment.