mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix(device-pairing): clean up push subscriptions on admin reset and share URL helpers
The admin password reset finds the admin case-insensitively and deletes the push subscriptions of the sessions it ends. The app re-sends its push subscription once per signed-in session so revoke reaches re-signed-in devices, and the upsert keeps the stored device name. Plain-HTTP and URL parsing checks are shared helpers.
This commit is contained in:
1 parent
cbe4f7ed9e
commit
6e398c4d40
21 files changed
+540
-85
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { z } from "zod";
|
||||
import { trimUrlTrailingSlashes } from "../utils/url";
|
||||
import { parseHttpUrl, trimUrlTrailingSlashes } from "../utils/url";
|
||||
|
||||
export const REMOTE_ACCESS_HEADER_MAX_COUNT = 8;
|
||||
export const REMOTE_ACCESS_HEADER_VALUE_MAX_LENGTH = 512;
|
||||
@@ -204,13 +204,8 @@ export function readPairingCodeFromHash(hash: string): string | null {
|
||||
|
||||
/** Parses a browser pairing link into its origin and code, or null when the text is not one. */
|
||||
export function parseManagerBrowserPairingLink(text: string): { origin: string; code: string } | null {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(text.trim());
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (url.protocol !== "https:" && url.protocol !== "http:") return null;
|
||||
const url = parseHttpUrl(text.trim());
|
||||
if (!url) return null;
|
||||
if (url.pathname !== DEVICE_PAIRING_PATH) return null;
|
||||
const code = readPairingCodeFromHash(url.hash);
|
||||
return code === null ? null : { origin: url.origin, code };
|
||||
|
||||
+22
-5
@@ -3,6 +3,18 @@ export function trimUrlTrailingSlashes(url: string): string {
|
||||
return url.trim().replace(/\/+$/, '')
|
||||
}
|
||||
|
||||
/** Parses a value into a URL, or returns null when it is not an absolute http(s) URL. */
|
||||
export function parseHttpUrl(value: string): URL | null {
|
||||
let url: URL
|
||||
try {
|
||||
url = new URL(value)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
if (url.protocol !== 'http:' && url.protocol !== 'https:') return null
|
||||
return url
|
||||
}
|
||||
|
||||
/** Reports whether two URLs share the same origin; false when either URL is invalid. */
|
||||
export function haveSameUrlOrigin(first: string, second: string): boolean {
|
||||
const origin = readUrlOrigin(first)
|
||||
@@ -36,6 +48,15 @@ export function isPrivateNetworkHost(hostname: string): boolean {
|
||||
return firstHextet >= 0xfc00 && firstHextet <= 0xfdff
|
||||
}
|
||||
|
||||
/** Reports whether a URL uses plain HTTP on a host other than loopback or a private network. */
|
||||
export function isPublicPlainHttpUrl(url: URL): boolean {
|
||||
return (
|
||||
url.protocol === 'http:' &&
|
||||
!isLoopbackHost(url.hostname) &&
|
||||
!isPrivateNetworkHost(url.hostname)
|
||||
)
|
||||
}
|
||||
|
||||
function normalizeHostname(hostname: string): string {
|
||||
return hostname.trim().toLowerCase().replace(/^\[/, '').replace(/\]$/, '')
|
||||
}
|
||||
@@ -54,9 +75,5 @@ function parseIpv4Octets(host: string): [number, number, number, number] | null
|
||||
}
|
||||
|
||||
function readUrlOrigin(value: string): string | null {
|
||||
try {
|
||||
return new URL(value).origin
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
return parseHttpUrl(value)?.origin ?? null
|
||||
}
|
||||
Reference in new issue
Block a user