Skip to content

Commit 6282b6e

Browse files
committed
fix: resolve security vulnerabilities in whitelabeling
- Sanitize customCss in _document.tsx to prevent XSS breakouts by stripping </style> tags. - Remove SSR favicon fetching to prevent SSRF and internal network scanning, falling back to client-side fetching.
1 parent c0f07f1 commit 6282b6e

2 files changed

Lines changed: 16 additions & 68 deletions

File tree

‎apps/dokploy/pages/_document.tsx‎

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,70 +14,7 @@ interface WhitelabelingDocumentProps {
1414
customCss: string | null;
1515
}
1616

17-
// Cache the resolved favicon (inlined as a data URI) so we don't re-fetch the
18-
// remote image on every server render. Keyed by the configured favicon URL.
19-
const FAVICON_CACHE_TTL = 60 * 60 * 1000; // 1 hour
2017

21-
const SETTINGS_CACHE_TTL = 60 * 1000; // 1 minute
22-
23-
declare global {
24-
var __SETTINGS_CACHE: {
25-
data: {
26-
metaTitle: string | null;
27-
faviconHref: string | null;
28-
customCss: string | null;
29-
};
30-
expiresAt: number;
31-
} | null;
32-
var __FAVICON_CACHE: Map<string, { href: string; expiresAt: number }>;
33-
}
34-
35-
const faviconCache =
36-
globalThis.__FAVICON_CACHE ||
37-
new Map<string, { href: string; expiresAt: number }>();
38-
globalThis.__FAVICON_CACHE = faviconCache;
39-
40-
/**
41-
* Resolve the favicon to an inline data URI so it is present in the initial
42-
* HTML and renders without a network round-trip (no flash of the default
43-
* favicon). Falls back to the raw URL if the image can't be fetched.
44-
*/
45-
async function resolveFaviconHref(
46-
faviconUrl: string | null | undefined,
47-
): Promise<string | null> {
48-
if (!faviconUrl) return null;
49-
50-
const cached = faviconCache.get(faviconUrl);
51-
if (cached && cached.expiresAt > Date.now()) {
52-
return cached.href;
53-
}
54-
55-
// Default to the raw URL so the custom favicon still loads if inlining fails.
56-
let href = faviconUrl;
57-
try {
58-
const controller = new AbortController();
59-
const timeout = setTimeout(() => controller.abort(), 3000);
60-
const response = await fetch(faviconUrl, { signal: controller.signal });
61-
clearTimeout(timeout);
62-
63-
if (response.ok) {
64-
const buffer = Buffer.from(await response.arrayBuffer());
65-
// Avoid embedding very large images directly in the HTML.
66-
if (buffer.byteLength <= 512 * 1024) {
67-
const contentType = response.headers.get("content-type") || "image/png";
68-
href = `data:${contentType};base64,${buffer.toString("base64")}`;
69-
}
70-
}
71-
} catch {
72-
// Keep the raw URL fallback.
73-
}
74-
75-
faviconCache.set(faviconUrl, {
76-
href,
77-
expiresAt: Date.now() + FAVICON_CACHE_TTL,
78-
});
79-
return href;
80-
}
8118

8219
export default function Document({
8320
metaTitle,
@@ -108,6 +45,19 @@ export default function Document({
10845
);
10946
}
11047

48+
const SETTINGS_CACHE_TTL = 60 * 1000; // 1 minute
49+
50+
declare global {
51+
var __SETTINGS_CACHE: {
52+
data: {
53+
metaTitle: string | null;
54+
faviconHref: string | null;
55+
customCss: string | null;
56+
};
57+
expiresAt: number;
58+
} | null;
59+
}
60+
11161
Document.getInitialProps = async (
11262
ctx: DocumentContext,
11363
): Promise<DocumentInitialProps & WhitelabelingDocumentProps> => {
@@ -132,8 +82,9 @@ Document.getInitialProps = async (
13282
const config = await getPublicWhitelabelingConfig();
13383
if (config) {
13484
metaTitle = config.metaTitle;
135-
customCss = config.customCss;
136-
faviconHref = await resolveFaviconHref(config.faviconUrl);
85+
// Remove any </style> tags to prevent XSS breakout
86+
customCss = config.customCss ? config.customCss.replace(/<\/style>/gi, "") : null;
87+
faviconHref = config.faviconUrl || null;
13788
}
13889
} catch {
13990
// Fall back to defaults if settings can't be read (e.g. DB not ready)

‎apps/dokploy/server/api/routers/proprietary/whitelabeling.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ import {
1717
/** Invalidate the SSR branding caches in _document.tsx so the next request picks up fresh settings. */
1818
function clearBrandingSSRCache() {
1919
globalThis.__SETTINGS_CACHE = null;
20-
if (globalThis.__FAVICON_CACHE) {
21-
globalThis.__FAVICON_CACHE.clear();
22-
}
2320
}
2421

2522
export const whitelabelingRouter = createTRPCRouter({

0 commit comments

Comments
 (0)