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
This commit is contained in:
1 parent
a5387bbffe
commit
d9aef5b6a6
10 files changed
+488
-20
No files matched your search
@@ -1,8 +1,10 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsBoolean, IsObject, IsOptional, IsString, IsUrl, Length } from "class-validator";
|
||||
import { IsBoolean, IsObject, IsOptional, IsString, Length, Validate } from "class-validator";
|
||||
|
||||
import { Metadata, METADATA_DOCS, ValidateMetadata } from "@calcom/platform-types";
|
||||
|
||||
import { SSRFSafeUrlValidator } from "../validators/ssrfSafeUrlValidator";
|
||||
|
||||
export class CreateTeamInput {
|
||||
@IsString()
|
||||
@Length(1)
|
||||
@@ -19,7 +21,8 @@ export class CreateTeamInput {
|
||||
readonly slug?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUrl()
|
||||
@IsString()
|
||||
@Validate(SSRFSafeUrlValidator)
|
||||
@ApiPropertyOptional({
|
||||
type: String,
|
||||
example: "https://i.cal.com/api/avatar/b0b58752-68ad-4c0d-8024-4fa382a77752.png",
|
||||
@@ -28,17 +31,20 @@ export class CreateTeamInput {
|
||||
readonly logoUrl?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUrl()
|
||||
@IsString()
|
||||
@Validate(SSRFSafeUrlValidator)
|
||||
@ApiPropertyOptional()
|
||||
readonly calVideoLogo?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUrl()
|
||||
@IsString()
|
||||
@Validate(SSRFSafeUrlValidator)
|
||||
@ApiPropertyOptional()
|
||||
readonly appLogo?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUrl()
|
||||
@IsString()
|
||||
@Validate(SSRFSafeUrlValidator)
|
||||
@ApiPropertyOptional()
|
||||
readonly appIconLogo?: string;
|
||||
|
||||
@@ -88,7 +94,8 @@ export class CreateTeamInput {
|
||||
readonly darkBrandColor?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUrl()
|
||||
@IsString()
|
||||
@Validate(SSRFSafeUrlValidator)
|
||||
@ApiPropertyOptional({
|
||||
type: String,
|
||||
example: "https://i.cal.com/api/avatar/949be534-7a88-4185-967c-c020b0c0bef3.png",
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsBoolean, IsObject, IsOptional, IsString, IsUrl, Length } from "class-validator";
|
||||
import { IsBoolean, IsObject, IsOptional, IsString, Length, Validate } from "class-validator";
|
||||
|
||||
import { Metadata, METADATA_DOCS, ValidateMetadata } from "@calcom/platform-types";
|
||||
|
||||
import { SSRFSafeUrlValidator } from "../validators/ssrfSafeUrlValidator";
|
||||
|
||||
export class UpdateTeamDto {
|
||||
@IsString()
|
||||
@Length(1)
|
||||
@@ -15,7 +17,8 @@ export class UpdateTeamDto {
|
||||
readonly slug?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUrl()
|
||||
@IsString()
|
||||
@Validate(SSRFSafeUrlValidator)
|
||||
@ApiPropertyOptional({
|
||||
type: String,
|
||||
example: "https://i.cal.com/api/avatar/b0b58752-68ad-4c0d-8024-4fa382a77752.png",
|
||||
@@ -24,17 +27,20 @@ export class UpdateTeamDto {
|
||||
readonly logoUrl?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUrl()
|
||||
@IsString()
|
||||
@Validate(SSRFSafeUrlValidator)
|
||||
@ApiPropertyOptional()
|
||||
readonly calVideoLogo?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUrl()
|
||||
@IsString()
|
||||
@Validate(SSRFSafeUrlValidator)
|
||||
@ApiPropertyOptional()
|
||||
readonly appLogo?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUrl()
|
||||
@IsString()
|
||||
@Validate(SSRFSafeUrlValidator)
|
||||
@ApiPropertyOptional()
|
||||
readonly appIconLogo?: string;
|
||||
|
||||
@@ -84,7 +90,8 @@ export class UpdateTeamDto {
|
||||
readonly darkBrandColor?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUrl()
|
||||
@IsString()
|
||||
@Validate(SSRFSafeUrlValidator)
|
||||
@ApiPropertyOptional({
|
||||
type: String,
|
||||
example: "https://i.cal.com/api/avatar/949be534-7a88-4185-967c-c020b0c0bef3.png",
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import type { ValidationError } from "class-validator";
|
||||
import { IsOptional, Validate, validateSync } from "class-validator";
|
||||
|
||||
import { SSRFSafeUrlValidator } from "./ssrfSafeUrlValidator";
|
||||
|
||||
const mockValidateUrlForSSRFSync: jest.Mock = jest.fn();
|
||||
|
||||
// Mock with minimal logic - actual validation tested in packages/lib
|
||||
jest.mock("@calcom/platform-libraries", () => ({
|
||||
validateUrlForSSRFSync: (url: string) => mockValidateUrlForSSRFSync(url),
|
||||
}));
|
||||
|
||||
class TestDto {
|
||||
@IsOptional()
|
||||
@Validate(SSRFSafeUrlValidator)
|
||||
url?: string;
|
||||
}
|
||||
|
||||
describe("SSRFSafeUrlValidator", () => {
|
||||
const validate = (url?: string): ValidationError[] => {
|
||||
const dto = new TestDto();
|
||||
dto.url = url;
|
||||
return validateSync(dto);
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockValidateUrlForSSRFSync.mockReset();
|
||||
});
|
||||
|
||||
it("accepts undefined values (works with @IsOptional)", () => {
|
||||
expect(validate(undefined)).toHaveLength(0);
|
||||
expect(mockValidateUrlForSSRFSync).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("validates empty strings (does not skip validation)", () => {
|
||||
mockValidateUrlForSSRFSync.mockReturnValue({ isValid: false });
|
||||
|
||||
const errors = validate("");
|
||||
expect(errors).toHaveLength(1);
|
||||
expect(mockValidateUrlForSSRFSync).toHaveBeenCalledWith("");
|
||||
});
|
||||
|
||||
it("delegates to validateUrlForSSRFSync and accepts valid URLs", () => {
|
||||
mockValidateUrlForSSRFSync.mockReturnValue({ isValid: true });
|
||||
|
||||
expect(validate("https://example.com/logo.png")).toHaveLength(0);
|
||||
expect(mockValidateUrlForSSRFSync).toHaveBeenCalledWith("https://example.com/logo.png");
|
||||
});
|
||||
|
||||
it("delegates to validateUrlForSSRFSync and rejects invalid URLs", () => {
|
||||
mockValidateUrlForSSRFSync.mockReturnValue({ isValid: false });
|
||||
|
||||
const errors = validate("blocked-url");
|
||||
expect(errors).toHaveLength(1);
|
||||
expect(errors[0].constraints).toHaveProperty("ssrfSafeUrl");
|
||||
expect(mockValidateUrlForSSRFSync).toHaveBeenCalledWith("blocked-url");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { ValidatorConstraint } from "class-validator";
|
||||
import type { ValidatorConstraintInterface } from "class-validator";
|
||||
|
||||
import { validateUrlForSSRFSync } from "@calcom/platform-libraries";
|
||||
|
||||
/**
|
||||
* Validates that a URL is safe for server-side fetching
|
||||
*
|
||||
* Allows: HTTPS URLs, image data URLs
|
||||
* Blocks: HTTP, private IPs, metadata endpoints, unsafe schemes
|
||||
*/
|
||||
@ValidatorConstraint({ name: "ssrfSafeUrl", async: false })
|
||||
export class SSRFSafeUrlValidator implements ValidatorConstraintInterface {
|
||||
validate(url: string): boolean {
|
||||
// Allow null/undefined (handled by @IsOptional), but not empty strings
|
||||
if (url === undefined || url === null) return true;
|
||||
|
||||
const result = validateUrlForSSRFSync(url);
|
||||
return result.isValid;
|
||||
}
|
||||
|
||||
defaultMessage(): string {
|
||||
return "URL is not allowed for security reasons (blocked: private IPs, metadata endpoints)";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user