Skip to content

fix(security): fail closed when CRON_SECRET is unset to prevent Bearer-undefined bypass#29654

Draft
codewithsupra wants to merge 7 commits into
calcom:mainfrom
codewithsupra:fix/cron-bearer-undefined-auth-bypass
Draft

fix(security): fail closed when CRON_SECRET is unset to prevent Bearer-undefined bypass#29654
codewithsupra wants to merge 7 commits into
calcom:mainfrom
codewithsupra:fix/cron-bearer-undefined-auth-bypass

Conversation

@codewithsupra

Copy link
Copy Markdown

What does this PR do?

Fixes an authentication bypass on cron/tasker endpoints that occurs when CRON_SECRET is not set in the environment.

Root cause

When CRON_SECRET is absent from the environment, JavaScript template literals evaluate process.env.CRON_SECRET to undefined, producing the literal string "Bearer undefined". Endpoints checking authHeader !== \Bearer ${process.env.CRON_SECRET}`would accept any request that sentAuthorization: Bearer undefined`.

The array-based calendar cron routes had the same flaw — \Bearer ${process.env.CRON_SECRET}`was included in the allowed-key array unconditionally, so"Bearer undefined"` was always an accepted key.

Affected endpoints:

  • POST/GET /api/tasks/cron — triggers processor.processQueue()
  • GET /api/tasks/cleanup — triggers tasker.cleanup() (deletes data)
  • GET /api/cron/calendar-subscriptions
  • GET /api/cron/calendar-subscriptions-cleanup
  • GET /api/cron/selected-calendars

Fix

  • tasker cleanup/cron: add !process.env.CRON_SECRET guard so the check fails closed when the variable is absent
  • calendar cron routes: build the allowed-key list by filtering out undefined values before calling .includes()
  • .env.example: document CRON_SECRET so deployers know to set it

Fixes #29565

codewithsupra and others added 3 commits June 26, 2026 22:26
…n rendering

Tailwind's preflight CSS resets h1–h3 to unstyled text, so heading
formatting set in the event description editor was invisible on the
booking page. Apply the same inline-style pattern already used for
<ul>, <ol>, and <a> in both markdownToSafeHTML (server) and
markdownToSafeHTMLClient (client) so headings render with correct size
and weight.

Fixes calcom#29645

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
When seatsPerTimeSlot is set and no booking exists yet, the else branch
was unconditionally setting shouldReserveSlot = false, blocking the very
first person from reserving a seated-event slot.

The else block is incorrect: an absent booking means no seats are taken,
so the slot is available. Remove it from both the tRPC handler and the
v2 API service so that shouldReserveSlot stays true until attendees
actually fill all seats.

Adds regression tests covering the first-attendee (should reserve) and
full-seats (should not reserve) cases.

Fixes calcom#29448

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…r-undefined bypass

When CRON_SECRET is not in the environment, template literals produce
"Bearer undefined". Endpoints that compared authHeader === \`Bearer \${process.env.CRON_SECRET}\`
would accept any request sending "Authorization: Bearer undefined",
giving unauthenticated callers access to tasker.cleanup() and
processor.processQueue().

The array-based calendar cron routes had the same problem: they included
\`Bearer \${process.env.CRON_SECRET}\` in the allowed-key list without
guarding against CRON_SECRET being undefined.

Fixes:
- tasker cleanup/cron: add !process.env.CRON_SECRET guard so the check
  fails closed when the variable is absent
- calendar cron routes: build the allowed-key list by filtering out
  undefined values before calling .includes()
- .env.example: document CRON_SECRET so deployers know to set it

Fixes calcom#29565
@github-actions github-actions Bot added the 🐛 bug Something isn't working label Jun 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Cal.diy, @codewithsupra! Thanks for opening this pull request.

A few things to keep in mind:

  • This is Cal.diy, not Cal.com. Cal.diy is a community-driven, fully open-source fork of Cal.com licensed under MIT. Your changes here will be part of Cal.diy — they will not be deployed to the Cal.com production app.
  • Please review our Contributing Guidelines if you haven't already.
  • Make sure your PR title follows the Conventional Commits format.

A maintainer will review your PR soon. Thanks for contributing!

@github-actions github-actions Bot added ❗️ .env changes contains changes to env variables and removed 🐛 bug Something isn't working labels Jun 26, 2026
@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e5536ba7-9017-4600-bc15-92a83e4253e5

📥 Commits

Reviewing files that changed from the base of the PR and between 6344a2f and 4c1e665.

📒 Files selected for processing (2)
  • .env.example
  • packages/lib/cronAuth.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • .env.example
  • packages/lib/cronAuth.ts

📝 Walkthrough

Walkthrough

.env.example adds CRON_SECRET, and cron/tasker handlers now validate requests against CRON_API_KEY plus an optional Bearer ${CRON_SECRET} value. Seated-event slot reservation keeps reservation enabled when attendee data is missing, with tests covering open and full seat cases. Both markdown sanitizers now add inline styles to h1, h2, and h3 tags.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR also changes slot reservation, markdown rendering, and related tests, which are unrelated to the cron auth fix. Remove the slots and markdown changes, or split them into separate PRs focused on those features.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the security fix for failing closed when CRON_SECRET is unset.
Description check ✅ Passed The description matches the PR and explains the cron/tasker auth bypass and fix.
Linked Issues check ✅ Passed The cron/tasker auth paths now fail closed when CRON_SECRET is missing, and .env.example documents it.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (3)
packages/trpc/server/routers/viewer/slots/reserveSlot.handler.test.ts (1)

65-79: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Replace as unknown as any with a typed Prisma test double.

These casts bypass exactly the contract this test is supposed to protect, so the test can keep passing after Prisma shape changes. A narrow typed stub/helper is safer here. As per coding guidelines, "Never use as any - use proper type-safe solutions instead."

Also applies to: 101-116

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/trpc/server/routers/viewer/slots/reserveSlot.handler.test.ts` around
lines 65 - 79, The Prisma test double in reserveSlot.handler.test.ts is using
unsafe casts that bypass the contract this test should enforce. Replace the
`prismaStub` and related setup in the `reserveSlot` test cases with a narrow,
type-safe Prisma test helper/stub that matches the `eventType`, `booking`, and
`selectedSlots` shapes expected by the handler, and remove the `as unknown as
any` casts entirely. Keep the stub minimal but properly typed so Prisma shape
changes surface as test failures rather than being hidden by `any`.

Source: Coding guidelines

apps/web/app/api/cron/calendar-subscriptions/route.ts (1)

27-28: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Share the cron allowlist builder across these routes.

The same validCronKeys construction now appears in calendar-subscriptions, calendar-subscriptions-cleanup, and selected-calendars. Pulling it behind one validator would reduce the chance of the next secret-handling fix landing on only a subset of cron endpoints.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/web/app/api/cron/calendar-subscriptions/route.ts` around lines 27 - 28,
The cron allowlist construction is duplicated across the cron route handlers, so
centralize it into one shared validator/helper and use it from
calendar-subscriptions, calendar-subscriptions-cleanup, and selected-calendars.
Move the `validCronKeys` building logic behind a reusable function or utility,
then have each route call that shared helper before comparing `apiKey`, so
secret handling stays consistent across all cron endpoints.
packages/features/tasker/api/cron.ts (1)

8-9: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Extract this tasker auth predicate into a shared helper.

This exact guard now lives in both packages/features/tasker/api/cron.ts and packages/features/tasker/api/cleanup.ts. Centralizing it would make the next auth fix land once instead of relying on both endpoints staying in sync.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/features/tasker/api/cron.ts` around lines 8 - 9, Extract the
duplicated tasker auth guard into a shared helper so both cron and cleanup use
the same predicate. Move the `process.env.CRON_SECRET` plus `authHeader` Bearer
check out of `cron.ts` into a reusable function and update the existing guard in
`cleanup.ts` to call it too, keeping the behavior identical while centralizing
the logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In @.env.example:
- Around line 68-69: The CRON_SECRET comment is too broad and implies all cron
endpoints fail closed when it is unset, but the calendar cron routes still
support CRON_API_KEY. Update the documentation near CRON_SECRET to describe only
the auth contract that actually uses this secret, and make the wording explicit
that other cron routes may authenticate differently. Use the CRON_SECRET entry
in the env example and the calendar cron auth behavior as the reference points
when tightening the text.

In `@packages/trpc/server/routers/viewer/slots/reserveSlot.handler.test.ts`:
- Around line 54-56: The test cleanup in afterEach currently always deletes
NEXT_PUBLIC_WEBAPP_URL, which can overwrite an existing preconfigured value for
later tests. Update the cleanup in reserveSlot.handler.test.ts to preserve and
restore the previous process.env.NEXT_PUBLIC_WEBAPP_URL value around each test
instead of unconditionally deleting it, using the existing afterEach setup that
also calls vi.resetModules().

---

Nitpick comments:
In `@apps/web/app/api/cron/calendar-subscriptions/route.ts`:
- Around line 27-28: The cron allowlist construction is duplicated across the
cron route handlers, so centralize it into one shared validator/helper and use
it from calendar-subscriptions, calendar-subscriptions-cleanup, and
selected-calendars. Move the `validCronKeys` building logic behind a reusable
function or utility, then have each route call that shared helper before
comparing `apiKey`, so secret handling stays consistent across all cron
endpoints.

In `@packages/features/tasker/api/cron.ts`:
- Around line 8-9: Extract the duplicated tasker auth guard into a shared helper
so both cron and cleanup use the same predicate. Move the
`process.env.CRON_SECRET` plus `authHeader` Bearer check out of `cron.ts` into a
reusable function and update the existing guard in `cleanup.ts` to call it too,
keeping the behavior identical while centralizing the logic.

In `@packages/trpc/server/routers/viewer/slots/reserveSlot.handler.test.ts`:
- Around line 65-79: The Prisma test double in reserveSlot.handler.test.ts is
using unsafe casts that bypass the contract this test should enforce. Replace
the `prismaStub` and related setup in the `reserveSlot` test cases with a
narrow, type-safe Prisma test helper/stub that matches the `eventType`,
`booking`, and `selectedSlots` shapes expected by the handler, and remove the
`as unknown as any` casts entirely. Keep the stub minimal but properly typed so
Prisma shape changes surface as test failures rather than being hidden by `any`.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5a8af310-c773-4781-9e3e-5351fc1bebc5

📥 Commits

Reviewing files that changed from the base of the PR and between 62317bd and 65885d9.

📒 Files selected for processing (11)
  • .env.example
  • apps/api/v2/src/modules/slots/slots-2024-04-15/services/slots.service.ts
  • apps/web/app/api/cron/calendar-subscriptions-cleanup/route.ts
  • apps/web/app/api/cron/calendar-subscriptions/route.ts
  • apps/web/app/api/cron/selected-calendars/route.ts
  • packages/features/tasker/api/cleanup.ts
  • packages/features/tasker/api/cron.ts
  • packages/lib/markdownToSafeHTML.ts
  • packages/lib/markdownToSafeHTMLClient.ts
  • packages/trpc/server/routers/viewer/slots/reserveSlot.handler.test.ts
  • packages/trpc/server/routers/viewer/slots/reserveSlot.handler.ts
💤 Files with no reviewable changes (2)
  • apps/api/v2/src/modules/slots/slots-2024-04-15/services/slots.service.ts
  • packages/trpc/server/routers/viewer/slots/reserveSlot.handler.ts

Comment thread .env.example Outdated
Comment thread packages/trpc/server/routers/viewer/slots/reserveSlot.handler.test.ts Outdated
@github-actions github-actions Bot added the 🐛 bug Something isn't working label Jun 27, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/lib/cronAuth.ts`:
- Around line 14-20: The auth check in cronAuth.ts is incorrectly allowing
CRON_SECRET through the query-string fallback because apiKey is compared against
the same allow-list as the Authorization header. Update the logic in the auth
helper so CRON_SECRET is accepted only when it comes from
req.headers.get("authorization"), while the query-param path continues to work
only for CRON_API_KEY. Keep the validation in the same cronAuth.ts flow and use
the existing apiKey/validKeys structure, but split the handling so Bearer
${process.env.CRON_SECRET} is never matched from
req.nextUrl.searchParams.get("apiKey").
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5bcab88e-5dfe-48af-a58e-01a8df783d16

📥 Commits

Reviewing files that changed from the base of the PR and between 65885d9 and 6344a2f.

📒 Files selected for processing (4)
  • apps/web/app/api/cron/calendar-subscriptions-cleanup/route.ts
  • apps/web/app/api/cron/calendar-subscriptions/route.ts
  • apps/web/app/api/cron/selected-calendars/route.ts
  • packages/lib/cronAuth.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/web/app/api/cron/calendar-subscriptions-cleanup/route.ts

Comment thread packages/lib/cronAuth.ts Outdated

@bandhan-majumder bandhan-majumder left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please attach a demo

@bandhan-majumder bandhan-majumder marked this pull request as draft June 27, 2026 20:55
@codewithsupra

Copy link
Copy Markdown
Author

Hi @bandhan-majumder, thanks for the review!

Since this is a backend security fix with no UI change, here's a demonstration of the before/after behaviour using curl:

Before (vulnerable):

# CRON_SECRET=mysecret
# This incorrectly authorised because ?apiKey was merged with the Authorization header check
curl -X POST https://your-app.com/api/cron/calendar-subscriptions?apiKey=Bearer%20mysecret
# → 200 OK  ⚠️ bypassed the header-only contract

After (fixed):

# CRON_SECRET=mysecret — now only checked against Authorization header
curl -X POST https://your-app.com/api/cron/calendar-subscriptions?apiKey=Bearer%20mysecret
# → 401 Unauthorized ✅ query-param bypass blocked

# CRON_API_KEY=myapikey — still works from query param (intentional)
curl -X POST https://your-app.com/api/cron/calendar-subscriptions?apiKey=myapikey
# → 200 OK ✅

# Bearer header still works for both
curl -H "Authorization: Bearer mysecret" -X POST https://your-app.com/api/cron/calendar-subscriptions
# → 200 OK ✅

The fix is in packages/lib/cronAuth.tsCRON_SECRET is now only evaluated against the Authorization header, while CRON_API_KEY continues to work from either the header or the ?apiKey query param.

Also pushed a small follow-up commit to fix the test afterEach cleanup (restoring NEXT_PUBLIC_WEBAPP_URL to its original value instead of always deleting it), which was the remaining CodeRabbit actionable item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 bug Something isn't working ❗️ .env changes contains changes to env variables size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cron/tasker endpoints authorize with "Bearer undefined" when CRON_SECRET is not set

2 participants