Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the favicon schema so it doesn't accept absolute URL. Closes #2647 #2651

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions packages/starlight/schemas/favicon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extname } from 'node:path';
import { extname, } from 'node:path';
import { z } from 'astro/zod';

const faviconTypeMap = {
Expand All @@ -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,
Expand All @@ -25,7 +29,17 @@ 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;
}

// Return the relative path (correctly formatted)
return {
href: favicon,
type: faviconTypeMap[ext],
Expand All @@ -35,6 +49,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;
}