Skip to content

Commit 01d3970

Browse files
westonruterdkotter
andauthored
Read AI home Script Module data via a script-type selector (#991)
Security - Harden JSON data parsing on admin screen. Co-authored-by: westonruter <westonruter@git.wordpress.org> Co-authored-by: dkotter <dkotter@git.wordpress.org>
1 parent 155e90c commit 01d3970

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

‎routes/ai-home/stage.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,13 @@ function getPageData(): PageData {
217217
};
218218

219219
try {
220-
const rawData = JSON.parse(
221-
document.getElementById( 'wp-script-module-data-ai-wp-admin' )
222-
?.textContent ?? '{}'
220+
const script = document.querySelector(
221+
'script[id="wp-script-module-data-ai-wp-admin"]'
223222
);
223+
if ( ! ( script instanceof HTMLScriptElement ) ) {
224+
return fallback;
225+
}
226+
const rawData = JSON.parse( script.text );
224227

225228
if ( ! isRecord( rawData ) ) {
226229
return fallback;

0 commit comments

Comments
 (0)