Skip to content

Commit b47f810

Browse files
committed
Optimization Detective: Add validation of SCRIPT#optimization-detective-detect-args
1 parent b97c13f commit b47f810

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

plugins/optimization-detective/detect-loader.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,25 @@ async function load() {
2626
} );
2727
}
2828

29-
const data = JSON.parse(
30-
/** @type {HTMLElement} */ (
31-
document.getElementById( 'optimization-detective-detect-args' )
32-
).textContent ?? ''
29+
const argsScript = document.getElementById(
30+
'optimization-detective-detect-args'
3331
);
32+
if ( ! ( argsScript instanceof HTMLScriptElement ) ) {
33+
throw new Error( 'Missing: SCRIPT#optimization-detective-detect-args' );
34+
}
35+
const data = JSON.parse( argsScript.textContent );
36+
if (
37+
! Array.isArray( data ) ||
38+
data.length !== 2 ||
39+
'string' !== typeof data[ 0 ] ||
40+
'object' !== typeof data[ 1 ]
41+
) {
42+
throw new Error(
43+
'SCRIPT#optimization-detective-detect-args is not [ string, object ]'
44+
);
45+
}
3446

35-
const detectSrc = /** @type {string} */ data[ 0 ];
47+
const detectSrc = data[ 0 ];
3648
const detectArgs =
3749
/** @type {import("./detect.js").DetectFunctionArgs} */ data[ 1 ];
3850
const detect = /** @type {import("./detect.js").DetectFunction} */ (

0 commit comments

Comments
 (0)