App: allow native deep links from iOS widget#31242
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Consider guarding
receiveFromApp(and the message handler) againstrouterbeing undefined or not yet initialized to avoid potential runtime errors if messages arrive beforeappDetectionis called. - The
messageevent handlers currently parse any incoming event data; you might want to add basic validation (e.g., checkingevent.source,message.type,message.pathformat) before callingrouter.pushto reduce the risk of malformed or unwanted navigation requests.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider guarding `receiveFromApp` (and the message handler) against `router` being undefined or not yet initialized to avoid potential runtime errors if messages arrive before `appDetection` is called.
- The `message` event handlers currently parse any incoming event data; you might want to add basic validation (e.g., checking `event.source`, `message.type`, `message.path` format) before calling `router.push` to reduce the risk of malformed or unwanted navigation requests.
## Individual Comments
### Comment 1
<location path="assets/js/utils/native.ts" line_range="21-24" />
<code_context>
+ if (!isApp()) return;
+ document.querySelector("html")?.classList.add("app");
+
+ const handler = (event: Event) => {
+ try {
+ receiveFromApp(JSON.parse((event as MessageEvent).data));
+ } catch {
+ return;
+ }
+ };
+ window.addEventListener("message", handler); // iOS
+ document.addEventListener("message", handler); // Android
+}
</code_context>
<issue_to_address>
**🚨 suggestion (security):** Tighten message event handling with proper typing and origin/shape checks for security and robustness.
The handler assumes every `Event` is a `MessageEvent` and parses `event.data` as JSON without validating its type or origin, so unrelated messages (e.g. from other iframes/extensions) could influence navigation.
I recommend:
- Typing the handler as `MessageEvent` instead of casting.
- Guarding with `if (typeof event.data !== "string") return;` before `JSON.parse`.
- Optionally checking `event.origin` for `window` messages so only trusted senders can trigger navigation.
This preserves current behavior while making the handler safer against unexpected inputs.
```suggestion
export function appDetection(r: Router) {
router = r;
if (!isApp()) return;
document.querySelector("html")?.classList.add("app");
const handler = (event: MessageEvent) => {
if (typeof event.data !== "string") {
return;
}
let parsed: unknown;
try {
parsed = JSON.parse(event.data);
} catch {
return;
}
// Only accept messages with the expected shape
if (!parsed || typeof parsed !== "object") {
return;
}
const message = parsed as Partial<FromAppMessage>;
if (message.type !== "navigate" || typeof message.path !== "string") {
return;
}
// For window messages, only accept from our own origin (or "null" which is
// typical for native webviews)
const isWindowMessage = event.source === window;
if (
isWindowMessage &&
event.origin !== window.location.origin &&
event.origin !== "null"
) {
return;
}
receiveFromApp(message as FromAppMessage);
};
window.addEventListener("message", handler); // iOS
document.addEventListener("message", handler as unknown as EventListener); // Android
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Companion web change for evcc-io/app#212. Lets the native app drive web UI navigation (iOS widget deep links).
Native posts a JSON message over the webview bridge:
{ "type": "navigate", "path": "/forecast" }