Files
cal.diy/packages/lib/zod/ssrfSafeUrl.ts
T
Pedro Castro d9aef5b6a6 fix: add URL validation for logo fields (#26522)
* fix(auth): add URL validation for organization logo fields

- Add URL validation utility for server-side fetched URLs
- Validate logo URLs in tRPC organization schema
- Validate logo URLs in API v2 team DTOs
- Add graceful fallback in logo route for invalid URLs
- Only allow HTTPS and image data URLs
- Add unit tests for URL validation

* fix: add missing IP range and type validation

- Add RFC 6598 CGNAT range (100.64.0.0/10) to blocked IPs
- Add @IsString() decorator before custom URL validators
- Add boundary tests for new IP range

* fix: address review feedback

   - Export validateUrlForSSRFSync via @calcom/platform-libraries
   - Fix nullable/optional order in Zod schema

* fix: block localhost hostname and explicit null check

- Add localhost to BLOCKED_HOSTNAMES for sync validation
- Use explicit null check (url == null) instead of falsy check

* fix: allow empty string in URL validation

* refactor: extract shared validation logic

- Extract validateUrlCore() to eliminate duplication between sync/async versions
- Add JSDoc to all exported functions for better discoverability
- Remove redundant comments that repeated function names
- Simplify existing comments to be more concise

* fix: reject empty strings in URL validator

Previously if (!url) allowed empty strings to bypass validation.
Now explicitly checks for null/undefined only
2026-01-08 22:03:54 +00:00

39 lines
1.1 KiB
TypeScript

import type { z } from "zod";
import { z as zod } from "zod";
import { validateUrlForSSRFSync } from "../ssrfProtection";
/**
* Zod schema for validating user-provided URLs before server-side fetching
*
* Applies synchronous SSRF checks only (no DNS resolution or rebinding)
* The logo route adds async DNS validation as defense-in-depth
*/
export const ssrfSafeUrlSchema: z.ZodEffects<z.ZodString, string, string> = zod.string().refine(
(url) => {
const result = validateUrlForSSRFSync(url);
return result.isValid;
},
{ message: "URL is not allowed for security reasons" }
);
/**
* Optional nullable variant for update schemas
* Allows null/undefined while validating provided values
*/
export const optionalSsrfSafeUrlSchema: z.ZodEffects<
z.ZodOptional<z.ZodNullable<z.ZodString>>,
string | null | undefined,
string | null | undefined
> = zod
.string()
.nullable()
.optional()
.refine(
(url) => {
if (url == null || url === "") return true; // null/undefined/empty allowed
return validateUrlForSSRFSync(url).isValid;
},
{ message: "URL is not allowed for security reasons" }
);