Skip to content

Commit f5a5716

Browse files
committed
fix: Refactor complex deep link parsing logic.
1 parent e8a4afb commit f5a5716

File tree

1 file changed

+49
-49
lines changed
  • infrastructure/eid-wallet/src/routes/(app)/scan-qr

1 file changed

+49
-49
lines changed

infrastructure/eid-wallet/src/routes/(app)/scan-qr/+page.svelte

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -97,58 +97,58 @@ async function cancelScan() {
9797
}
9898
9999
onMount(async () => {
100-
const [_empty, ...rest] = page.url.search.split("?");
101-
const methodAndParam = rest.join("?");
102-
const [method, ...param] = methodAndParam.split("?");
103-
const data = param.join("?");
104-
const deeplinkMethodSpecifiers = ["auth"];
105-
if (method && !deeplinkMethodSpecifiers.includes(method)) {
106-
console.error("Unknown method specifier");
107-
}
108-
switch (method) {
109-
case "auth": {
110-
const params = new URLSearchParams(data);
111-
platform = params.get("platform");
112-
session = params.get("session");
113-
redirect = params.get("redirect");
114-
if (!redirect || !platform || !session) {
115-
console.error("Bad deeplink!");
116-
break;
117-
}
118-
try {
119-
hostname = new URL(redirect as string).hostname;
120-
} catch (error) {
121-
console.error("Invalid redirect URL:", error);
122-
break;
123-
}
124-
// Validate platform name
125-
if (!/^[a-zA-Z0-9-_.]+$/.test(platform)) {
126-
console.error("Invalid platform name format");
127-
break;
128-
}
100+
const params = page.url.searchParams;
101+
const deepLinkData = page.url.search.substring(1); // Remove leading '?'
102+
if (deepLinkData) {
103+
try {
104+
const [method, ...paramParts] = deepLinkData.split("?");
105+
const paramString = paramParts.join("?");
106+
if (method === "auth") {
107+
const params = new URLSearchParams(paramString);
108+
platform = params.get("platform");
109+
session = params.get("session");
110+
redirect = params.get("redirect");
111+
if (!redirect || !platform || !session) {
112+
console.error("Bad deeplink!");
113+
return;
114+
}
115+
try {
116+
hostname = new URL(redirect as string).hostname;
117+
} catch (error) {
118+
console.error("Invalid redirect URL:", error);
119+
return;
120+
}
121+
// Validate platform name
122+
if (!/^[a-zA-Z0-9-_.]+$/.test(platform)) {
123+
console.error("Invalid platform name format");
124+
return;
125+
}
129126
130-
// Validate session format (UUID)
131-
if (
132-
!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
133-
session,
134-
)
135-
) {
136-
console.error("Invalid session format");
137-
break;
138-
}
127+
// Validate session format (UUID)
128+
if (
129+
!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
130+
session,
131+
)
132+
) {
133+
console.error("Invalid session format");
134+
return;
135+
}
139136
140-
// Validate redirect URL domain
141-
if (
142-
!/^(?=.{1,253}$)(?!\-)([a-zA-Z0-9\-]{1,63}(?<!\-)\.)+[a-zA-Z]{2,}$/.test(
143-
hostname,
144-
)
145-
) {
146-
console.error("Invalid redirect URL format.");
147-
break;
137+
// Validate redirect URL domain
138+
if (
139+
!/^(?=.{1,253}$)(?!\-)([a-zA-Z0-9\-]{1,63}(?<!\-)\.)+[a-zA-Z]{2,}$/.test(
140+
hostname,
141+
)
142+
) {
143+
console.error("Invalid redirect URL format.");
144+
return;
145+
}
146+
codeScannedDrawerOpen = true;
147+
scanning = false;
148+
return;
148149
}
149-
codeScannedDrawerOpen = true;
150-
scanning = false;
151-
break;
150+
} catch (err) {
151+
console.error("Error parsing deep link data:", err);
152152
}
153153
}
154154
if (!codeScannedDrawerOpen) startScan();

0 commit comments

Comments
 (0)