Skip to content

Commit 99e8606

Browse files
Avoid HTML injection in standalone errors (react#36839)
## Summary - Render standalone DevTools server errors with DOM nodes instead of HTML strings. - Preserve the existing error box classes and copy while inserting error text with `textContent`. ## How did you test this? - `corepack yarn prettier` - `corepack yarn lint packages/react-devtools-core/src/standalone.js`
1 parent 8a5274d commit 99e8606

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

packages/react-devtools-core/src/standalone.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,31 +176,34 @@ function onDisconnected() {
176176
disconnectedCallback();
177177
}
178178

179+
function showErrorMessage(headerText: string, contentText: string) {
180+
const box = document.createElement('div');
181+
box.className = 'box';
182+
183+
const header = document.createElement('div');
184+
header.className = 'box-header';
185+
header.textContent = headerText;
186+
box.appendChild(header);
187+
188+
const content = document.createElement('div');
189+
content.className = 'box-content';
190+
content.textContent = contentText;
191+
box.appendChild(content);
192+
193+
node.textContent = '';
194+
node.appendChild(box);
195+
}
196+
179197
function onError({code, message}: $FlowFixMe) {
180198
safeUnmount();
181199

182200
if (code === 'EADDRINUSE') {
183-
node.innerHTML = `
184-
<div class="box">
185-
<div class="box-header">
186-
Another instance of DevTools is running.
187-
</div>
188-
<div class="box-content">
189-
Only one copy of DevTools can be used at a time.
190-
</div>
191-
</div>
192-
`;
201+
showErrorMessage(
202+
'Another instance of DevTools is running.',
203+
'Only one copy of DevTools can be used at a time.',
204+
);
193205
} else {
194-
node.innerHTML = `
195-
<div class="box">
196-
<div class="box-header">
197-
Unknown error
198-
</div>
199-
<div class="box-content">
200-
${message}
201-
</div>
202-
</div>
203-
`;
206+
showErrorMessage('Unknown error', String(message));
204207
}
205208
}
206209

0 commit comments

Comments
 (0)