Skip to content

Added middleware header - #1087

Open
tomski747 wants to merge 6 commits into
mainfrom
fix/no-cache-for-you
Open

Added middleware header#1087
tomski747 wants to merge 6 commits into
mainfrom
fix/no-cache-for-you

Conversation

@tomski747

Copy link
Copy Markdown
Member

This pull request makes targeted improvements to the middleware.ts file, primarily to enhance how the middleware communicates its decision about iframe embedding to downstream handlers. The main changes introduce a new custom header and correct a configuration URL.

Middleware response improvements:

  • Added a new custom header, x-descope-middleware, to responses to explicitly indicate whether iframe embedding is allowed ('true' or 'false'). This provides clearer signaling to downstream consumers or middleware. [1] [2] [3]

Configuration correction:

  • Corrected the fallback API base URL for configuration checks from https://api.descope.com to https://api.descope.org when handling .preview.descope.org hostnames.

- Changed API base URL from 'https://api.descope.com' to 'https://api.descope.org' for preview environments.
- Updated fetch cache option from 'force-cache' to 'no-store' to ensure fresh data retrieval.

feat(vercel): add cache control headers

- Introduced Cache-Control headers in vercel.json to prevent caching at various levels.
- Added regex for static file extensions to bypass middleware for asset requests.
- Implemented no-cache headers for static assets and project configuration responses to improve security and data freshness.
- Updated the default response headers to include no-cache settings when iframe embedding is allowed.
…ndling

- Updated tests to ensure no-cache headers are applied correctly for static assets.
- Corrected API base URL in tests to reflect the change from 'api.descope.com' to 'api.descope.org'.
- Improved test coverage for middleware behavior regarding iframe embedding and cache settings.
… logic

- Removed static asset cache control headers and implemented a matcher to exclude static file requests from middleware processing.
- Updated API base URL for project configuration from 'https://api.descope.org' to 'https://api.descope.com'.
- Changed fetch cache option to 'force-cache' for project configuration requests.
- Enhanced middleware to conditionally omit X-Frame-Options header based on project configuration settings.
- Updated API base URL for preview environments from 'https://api.descope.com' to 'https://api.descope.org'.
- Modified middleware to include a custom header 'moshe' based on project configuration for iframe embedding.
- Updated middleware to include project configuration in the response headers when iframe embedding is allowed, enhancing the context provided to the next middleware function.
Copilot AI review requested due to automatic review settings February 24, 2026 18:23
@shuni-bot-dev

shuni-bot-dev Bot commented Feb 24, 2026

Copy link
Copy Markdown

✅ Code review completed successfully

#1087

@vercel

vercel Bot commented Feb 24, 2026

Copy link
Copy Markdown

Your Vercel team Allen Zhou is not permitted to deploy from this git repository. Contact an administrator to add github organization descope as a Protected Git Scope in Allen Zhou on Vercel. Once added, commit again to see your changes.

Learn more: https://vercel.com/docs/security/protected-git-scopes

@shuni-bot-dev shuni-bot-dev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🐕 Shuni's Review

Adds x-descope-middleware response header and fixes preview API base URL (.com.org). But the tests weren't updated — they'll break on every changed behavior.

Sniffed out 2 issues:

  • 1 🟠 HIGH: All existing tests will fail — assertions don't account for the new header or the URL change
  • 1 🟡 MEDIUM: Test description text references stale domain

These bones are buried in the wrong yard! Woof!

Comment thread middleware.ts
import { projectRegex } from './src/shared/projectRegex';

const FETCH_TIMEOUT_MS = 2000;
const DESCOPE_MIDDLEWARE_HEADER = 'x-descope-middleware';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 HIGH: Tests not updated — all assertions will fail.

The test file src/middleware.test.ts was not modified in this PR, but every assertion is now stale:

  1. "Embedding allowed" path (lines 85, 97): Tests assert expect(mockedNext).toHaveBeenCalledWith() (zero args), but the code now passes next({ headers: { 'x-descope-middleware': 'true' } }). These two tests will fail.

  2. expectXFrameOptions helper (lines 22-26): Asserts next() is called with { headers: { 'X-Frame-Options': 'SAMEORIGIN' } }, but the code now also includes 'x-descope-middleware': 'false' in the headers object. toHaveBeenCalledWith does an exact match on the argument shape, so every test using this helper will fail (6+ tests).

  3. Preview URL tests (lines 160-162, 175-177): Assert api.descope.com in the fetch URL, but the code now uses api.descope.org. Both config-base-URL tests will fail.

Please update src/middleware.test.ts to match the new behavior.

Comment thread middleware.ts

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Vercel middleware to explicitly signal iframe-embedding decisions via a custom response header, and corrects the fallback configuration API base URL used for .preview.descope.org deployments.

Changes:

  • Add x-descope-middleware: true|false header to indicate whether iframe embedding is allowed.
  • Update .preview.descope.org config lookup fallback from https://api.descope.com to https://api.descope.org.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread middleware.ts
Comment thread middleware.ts
Comment thread middleware.ts
Comment on lines 51 to 55
return next({
headers: {
[DESCOPE_MIDDLEWARE_HEADER]: 'false',
'X-Frame-Options': 'SAMEORIGIN'
}

Copilot AI Feb 24, 2026

Copy link

Choose a reason for hiding this comment

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

Default response headers now include x-descope-middleware: false in addition to X-Frame-Options. Update existing assertions that expect only X-Frame-Options (e.g., the expectXFrameOptions() helper in src/middleware.test.ts) so they account for the additional header.

Copilot uses AI. Check for mistakes.

@itaihanski itaihanski 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.

Looks good! Some tests got broken

Comment thread middleware.ts
Comment thread middleware.ts
import { projectRegex } from './src/shared/projectRegex';

const FETCH_TIMEOUT_MS = 2000;
const DESCOPE_MIDDLEWARE_HEADER = 'x-descope-middleware';

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.

I don't think we really need this - it's always false with the x-frame-header and always true otherwise. WDYT?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants