From 652f30a826583b335e38b40558ab5298f83e5bfa Mon Sep 17 00:00:00 2001 From: adios2d6 <31654864+lighterEB@users.noreply.github.com> Date: Mon, 9 Mar 2026 11:50:07 +0800 Subject: [PATCH] fix(web): prevent fetch error when hostname is an IP address in TEE check (#672) Currently, teeApiBase() splits the hostname by '.' and incorrectly parses IP addresses like 127.0.0.1 or localhost into invalid URLs (e.g., http://api.0.0.1/), which causes the fetch API to throw a 'Failed to construct Request' TypeError and crashes the web UI. This fix: - Skips TEE checks if the hostname is an IP address or localhost. - Wraps checkTeeStatus() and fetchTeeReport() with try...catch to gracefully handle any unforeseen fetch errors without bubbling up to the global scope. Co-authored-by: lighterEB --- src/channels/web/static/app.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/channels/web/static/app.js b/src/channels/web/static/app.js index 573ce5f2..71dee53b 100644 --- a/src/channels/web/static/app.js +++ b/src/channels/web/static/app.js @@ -3593,10 +3593,15 @@ let teeReportCache = null; let teeReportLoading = false; function teeApiBase() { - var parts = window.location.hostname.split('.'); - if (parts.length < 2) return null; - var domain = parts.slice(1).join('.'); - return window.location.protocol + '//api.' + domain; + var hostname = window.location.hostname; + // Skip IP addresses (IPv4 and IPv6) and localhost + if (hostname === "localhost" || /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(hostname) || hostname.indexOf(":") !== -1) { + return null; + } + var parts = hostname.split("."); + if (parts.length < 2) return null; + var domain = parts.slice(1).join("."); + return window.location.protocol + "//api." + domain; } function teeInstanceName() { @@ -3607,13 +3612,19 @@ function checkTeeStatus() { var base = teeApiBase(); if (!base) return; var name = teeInstanceName(); - fetch(base + '/instances/' + encodeURIComponent(name) + '/attestation').then(function(res) { - if (!res.ok) throw new Error(res.status); - return res.json(); - }).then(function(data) { - teeInfo = data; - document.getElementById('tee-shield').style.display = 'flex'; - }).catch(function() {}); + try { + fetch(base + '/instances/' + encodeURIComponent(name) + '/attestation').then(function(res) { + if (!res.ok) throw new Error(res.status); + return res.json(); + }).then(function(data) { + teeInfo = data; + document.getElementById('tee-shield').style.display = 'flex'; + }).catch(function(err) { + console.warn('Failed to fetch TEE attestation:', err); + }); + } catch (e) { + console.warn("Failed to check TEE status:", e); + } } function fetchTeeReport() {