From ac0244c362a5c38615db2b0d0dfa300769bfc0d9 Mon Sep 17 00:00:00 2001 From: snowdingo Date: Mon, 2 Dec 2024 20:33:55 +0900 Subject: [PATCH 1/2] Modified the FaviconSchema so it doesn't accept any absolute URL for favicon, and throws an error when it is the case. --- packages/starlight/schemas/favicon.ts | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/starlight/schemas/favicon.ts b/packages/starlight/schemas/favicon.ts index 7f599099d2f..e7a4d1e6a4b 100644 --- a/packages/starlight/schemas/favicon.ts +++ b/packages/starlight/schemas/favicon.ts @@ -1,4 +1,4 @@ -import { extname } from 'node:path'; +import { extname, } from 'node:path'; import { z } from 'astro/zod'; const faviconTypeMap = { @@ -10,13 +10,17 @@ const faviconTypeMap = { '.svg': 'image/svg+xml', }; +function isFaviconExt(ext: string): ext is keyof typeof faviconTypeMap { + return ext in faviconTypeMap; +} + export const FaviconSchema = () => z .string() .default('/favicon.svg') .transform((favicon, ctx) => { const ext = extname(favicon).toLowerCase(); - + // Return error when favicon isn't the file type accepted if (!isFaviconExt(ext)) { ctx.addIssue({ code: z.ZodIssueCode.custom, @@ -25,7 +29,21 @@ export const FaviconSchema = () => return z.NEVER; } + // check for both http and https + if (/^https?:\/\//.test(favicon)) { + ctx.addIssue({ + // Show error message + code: z.ZodIssueCode.custom, + message: 'Favicons must be a relative URL with a .ico, .gif, .jpg, .png, or .svg file.', + }); + return z.NEVER; + } + // If it is a relative link but with different format, reformat it. + if (!favicon.startsWith('/')) { + favicon = `/${favicon}`; + } + // Return the relative path (correctly formatted) return { href: favicon, type: faviconTypeMap[ext], @@ -35,6 +53,3 @@ export const FaviconSchema = () => 'The default favicon for your site which should be a path to an image in the `public/` directory.' ); -function isFaviconExt(ext: string): ext is keyof typeof faviconTypeMap { - return ext in faviconTypeMap; -} From f9fe81cabd23a7551ecee4275410232ac1226e38 Mon Sep 17 00:00:00 2001 From: Fire Dingo <101443426+SnowDingo@users.noreply.github.com> Date: Mon, 2 Dec 2024 20:52:26 +0900 Subject: [PATCH 2/2] Favicon schema error fixed. --- packages/starlight/schemas/favicon.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/starlight/schemas/favicon.ts b/packages/starlight/schemas/favicon.ts index e7a4d1e6a4b..e47a878acd1 100644 --- a/packages/starlight/schemas/favicon.ts +++ b/packages/starlight/schemas/favicon.ts @@ -38,10 +38,6 @@ export const FaviconSchema = () => }); return z.NEVER; } - // If it is a relative link but with different format, reformat it. - if (!favicon.startsWith('/')) { - favicon = `/${favicon}`; - } // Return the relative path (correctly formatted) return {