-
Notifications
You must be signed in to change notification settings - Fork 13.9k
fix(api): improve ICS URL validation edge cases #29412
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
Open
abhinavlevi
wants to merge
2
commits into
calcom:main
Choose a base branch
from
abhinavlevi:fix/ics-validation-clean-v5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−16
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
apps/api/v2/src/platform/calendars/input/create-ics.input.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { validate } from "class-validator"; | ||
| import { CreateIcsFeedInputDto } from "./create-ics.input"; | ||
|
|
||
| describe("CreateIcsFeedInputDto Validation", () => { | ||
| it("should accept a standard .ics URL", async () => { | ||
| const input = new CreateIcsFeedInputDto(); | ||
| input.urls = ["https://example.com/calendar.ics"]; | ||
| const errors = await validate(input); | ||
| expect(errors.length).toBe(0); | ||
| }); | ||
|
|
||
| it("should accept valid ICS URLs without a .ics extension (Fixes #29286)", async () => { | ||
| const input = new CreateIcsFeedInputDto(); | ||
| input.urls = ["https://caldav.soverin.net/calendars/MyCalendar?export"]; | ||
| const errors = await validate(input); | ||
| expect(errors.length).toBe(0); | ||
| }); | ||
|
|
||
| it("should reject plain strings and malformed URLs", async () => { | ||
| const input = new CreateIcsFeedInputDto(); | ||
| input.urls = ["not-a-valid-url"]; | ||
| const errors = await validate(input); | ||
| expect(errors.length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it("should reject localhost and local IPs to prevent SSRF", async () => { | ||
| const input = new CreateIcsFeedInputDto(); | ||
| input.urls = [ | ||
| "http://localhost:3000/fake-calendar", | ||
| "http://127.0.0.1/cal", | ||
| "http://192.168.1.1/feed" | ||
| ]; | ||
| const errors = await validate(input); | ||
| expect(errors.length).toBeGreaterThan(0); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
apps/api/v2/src/platform/calendars/validators/is-calendar-url.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { lookup } from "node:dns/promises"; | ||
| import { isIP } from "node:net"; | ||
| import { | ||
| ValidatorConstraint, | ||
| ValidatorConstraintInterface, | ||
| isURL, | ||
| } from "class-validator"; | ||
| import { URL } from "node:url"; | ||
|
|
||
| @ValidatorConstraint({ async: true }) | ||
| export class IsICSUrlConstraint implements ValidatorConstraintInterface { | ||
| private isPrivateIp(ip: string): boolean { | ||
| return ( | ||
| /^127\./.test(ip) || | ||
| /^10\./.test(ip) || | ||
| /^192\.168\./.test(ip) || | ||
| /^172\.(1[6-9]|2[0-9]|3[0-1])\./.test(ip) || | ||
| /^169\.254\./.test(ip) || | ||
| /^(::1|fe80:|fc|fd|::)$/i.test(ip) | ||
| ); | ||
| } | ||
|
|
||
| private async assertSafe(hostname: string) { | ||
| const records = await lookup(hostname, { all: true }); | ||
|
|
||
| for (const r of records) { | ||
| if (this.isPrivateIp(r.address)) { | ||
| throw new Error("Private IP blocked"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async validate(url: unknown): Promise<boolean> { | ||
| if (typeof url !== "string") return false; | ||
|
|
||
| let parsed: URL; | ||
| try { | ||
| parsed = new URL(url); | ||
| } catch { | ||
| return false; | ||
| } | ||
|
|
||
| const hostname = parsed.hostname; | ||
|
|
||
| if (!["http:", "https:"].includes(parsed.protocol)) return false; | ||
|
|
||
| if ( | ||
| !isURL(url, { | ||
| require_protocol: true, | ||
| require_valid_protocol: true, | ||
| }) | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| // IP direct check | ||
| if (isIP(hostname)) { | ||
| if (this.isPrivateIp(hostname)) return false; | ||
| return true; | ||
| } | ||
|
|
||
| // DNS SSRF check (THIS fixes your failing test) | ||
| try { | ||
| await this.assertSafe(hostname); | ||
| } catch { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| defaultMessage() { | ||
| return "Invalid or unsafe ICS URL"; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: IPv6 regex does not block private/link-local addresses.
The regex
/^(::1|fe80:|fc|fd|::)$/irequires an exact match due to the$anchor being outside the alternation group. Real IPv6 addresses likefe80::1(link-local) orfd00::1(unique-local) will not match, allowing SSRF bypass via IPv6 private addresses.fe80::1234:5678.match(regex) →null(should block, doesn't)fd12:3456::1.match(regex) →null(should block, doesn't)Proposed fix
private isPrivateIp(ip: string): boolean { + const normalizedIp = ip.toLowerCase(); return ( /^127\./.test(ip) || /^10\./.test(ip) || /^192\.168\./.test(ip) || /^172\.(1[6-9]|2[0-9]|3[0-1])\./.test(ip) || /^169\.254\./.test(ip) || - /^(::1|fe80:|fc|fd|::)$/i.test(ip) + normalizedIp === "::1" || + normalizedIp === "::" || + /^fe80:/i.test(ip) || + /^f[cd]/i.test(ip) ); }🤖 Prompt for AI Agents