Skip to content

Commit 435a370

Browse files
committed
fix(mobile): keep mermaid diagram component compiler-clean and harden WebView
Move WebView message JSON parsing into a module helper so the React compiler accepts the component. Render mermaid with securityLevel strict and disable WebView file access.
1 parent 7974c8f commit 435a370

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

‎apps/mobile/src/native/MobileMermaidDiagram.tsx‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var root = document.getElementById("root");
2828
function post(message){ window.ReactNativeWebView.postMessage(JSON.stringify(message)); }
2929
function reportHeight(){ post({ type: "height", height: document.body.scrollHeight }); }
3030
try {
31-
mermaid.initialize({ startOnLoad: false, theme: data.dark ? "dark" : "default" });
31+
mermaid.initialize({ startOnLoad: false, theme: data.dark ? "dark" : "default", securityLevel: "strict" });
3232
mermaid.render("diagram", data.source).then(function(result) {
3333
root.innerHTML = result.svg;
3434
reportHeight();
@@ -53,23 +53,34 @@ new ResizeObserver(reportHeight).observe(document.body);
5353
type HeightMessage = { readonly type: "height"; readonly height: number };
5454
type ErrorMessage = { readonly type: "error"; readonly message: string };
5555

56+
function parseMermaidMessage(data: string): HeightMessage | ErrorMessage | null {
57+
try {
58+
return JSON.parse(data) as HeightMessage | ErrorMessage;
59+
} catch {
60+
return null;
61+
}
62+
}
63+
64+
function isHeightMessage(message: HeightMessage | ErrorMessage): message is HeightMessage {
65+
return message.type === "height";
66+
}
67+
5668
export function MobileMermaidDiagram(props: { readonly source: string; readonly dark: boolean }) {
5769
const [height, setHeight] = useState(220);
5870
const [failed, setFailed] = useState<string | null>(null);
5971
const [showCode, setShowCode] = useState(false);
6072
const html = useMemo(() => buildHtml(props.source, props.dark), [props.source, props.dark]);
6173

6274
const onMessage = (event: WebViewMessageEvent) => {
63-
try {
64-
const message = JSON.parse(event.nativeEvent.data) as HeightMessage | ErrorMessage;
65-
if (message.type === "height" && Number.isFinite(message.height)) {
75+
const message = parseMermaidMessage(event.nativeEvent.data);
76+
if (!message) return;
77+
if (isHeightMessage(message)) {
78+
if (Number.isFinite(message.height)) {
6679
setHeight(Math.min(Math.max(Math.ceil(message.height) + 16, 120), 800));
67-
} else if (message.type === "error") {
68-
setFailed(message.message);
6980
}
70-
} catch {
71-
// Ignore malformed WebView messages.
81+
return;
7282
}
83+
setFailed(message.message);
7384
};
7485

7586
return (
@@ -107,6 +118,7 @@ export function MobileMermaidDiagram(props: { readonly source: string; readonly
107118
source={{ html }}
108119
javaScriptEnabled
109120
domStorageEnabled={false}
121+
allowFileAccess={false}
110122
scrollEnabled={false}
111123
style={{ height }}
112124
onMessage={onMessage}

0 commit comments

Comments
 (0)