1 import { isProduction, MedusaError } from "@medusajs/framework/utils"
2
3 /**
4 * Asserts that a URL is `https`. Outside of production, `http` is allowed for
5 * localhost so local development and testing remain possible.
6 */
7 export const assertSecureUrl = (value: string, label: string): void => {
8 let url: URL
9 try {
10 url = new URL(value)
11 } catch (e) {
12 throw new MedusaError(
13 MedusaError.Types.INVALID_DATA,
14 `OIDC '${label}' must be a valid URL`
15 )
16 }
17
18 const isLocalhost =
19 url.hostname === "localhost" ||
20 url.hostname === "127.0.0.1" ||
21 url.hostname === "::1" ||
22 url.hostname === "[::1]"
23
24 const allowsHttp = isLocalhost && !isProduction()
25
26 if (url.protocol !== "https:" && !(url.protocol === "http:" && allowsHttp)) {
27 throw new MedusaError(
28 MedusaError.Types.INVALID_DATA,
29 `OIDC '${label}' must use https (http is only allowed for localhost outside of production)`
30 )
31 }
32 }