Repository navigation
feat(astro): Serve the managed tunnel from the Sentry middleware #25419
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
Draft
+242
−9
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
34649a5
feat(astro): Serve the managed tunnel from the Sentry middleware
logaretm b4453e3
fix(astro): Run the server init before the middleware so the tunnel w…
logaretm a1c95e4
fix(astro): Serve a relative `tunnel` path under the base path too
logaretm a0a4102
test(astro): Check the managed tunnel under `astro dev` too
logaretm 4d98700
revert(astro): Load the server init from SSR pages only, as on develop
logaretm bbc97ee
ref(astro): Import the tunnel handler from @sentry/core/server
logaretm 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
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
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
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
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
14 changes: 14 additions & 0 deletions
14
dev-packages/e2e-tests/test-applications/astro-7/src/middleware.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,14 @@ | ||
| import { defineMiddleware } from 'astro:middleware'; | ||
|
|
||
| // The tunnel variant checks that the managed tunnel answers before an auth middleware like this one. | ||
| export const onRequest = defineMiddleware((context, next) => { | ||
| if ( | ||
| import.meta.env.PUBLIC_E2E_TEST_TUNNEL && | ||
| context.request.method === 'POST' && | ||
| context.request.headers.get('x-auth') !== 'ok' | ||
| ) { | ||
| return new Response('blocked by user auth', { status: 401 }); | ||
| } | ||
|
|
||
| return next(); | ||
| }); |
43 changes: 43 additions & 0 deletions
43
dev-packages/e2e-tests/test-applications/astro-7/tests/tunnel.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,43 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils'; | ||
|
|
||
| test.skip(!process.env.PUBLIC_E2E_TEST_TUNNEL, 'Only runs in the tunnel variant'); | ||
|
|
||
| const TUNNEL_PATH = /^\/docs\/[a-z0-9]{1,7}\/$/; | ||
|
|
||
| test('answers the managed tunnel under the base path before the app middleware', async ({ page }) => { | ||
| // The variant's auth middleware rejects every other POST. | ||
| expect((await page.request.post('/docs/anything/', { data: 'x' })).status()).toBe(401); | ||
|
|
||
| const tunnelServerSpanPromise = waitForStreamedSpan( | ||
| 'astro-7', | ||
| span => | ||
| getSpanOp(span) === 'http.server' && | ||
| /\/docs\/[a-z0-9]{1,7}\/?$/.test(String(span.attributes['url.path']?.value ?? '')), | ||
| ); | ||
| const tunnelResponsePromise = page.waitForResponse( | ||
| response => response.request().method() === 'POST' && TUNNEL_PATH.test(new URL(response.url()).pathname), | ||
| ); | ||
|
|
||
| await page.goto('/docs/'); | ||
| const tunnelResponse = await tunnelResponsePromise; | ||
|
|
||
| expect(tunnelResponse.status()).toBe(200); | ||
| expect(new URL(tunnelResponse.url()).origin).toBe(new URL(page.url()).origin); | ||
|
|
||
| // Anchor on a regular server span requested after the tunnel traffic: spans flush in order, so a tunnel span | ||
| // would arrive first. | ||
| const anchorSpanPromise = waitForStreamedSpan( | ||
| 'astro-7', | ||
| span => | ||
| getSpanOp(span) === 'http.server' && | ||
| String(span.attributes['url.path']?.value ?? '').startsWith('/docs/test-ssr'), | ||
| ); | ||
| await page.request.get('/docs/test-ssr/'); | ||
|
|
||
| const winner = await Promise.race([ | ||
| tunnelServerSpanPromise.then(() => 'tunnel' as const), | ||
| anchorSpanPromise.then(() => 'anchor' as const), | ||
| ]); | ||
| expect(winner).toBe('anchor'); | ||
| }); |
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
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
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,32 @@ | ||
| import type { TunnelOptions } from '@sentry/core'; | ||
| import type { AstroConfig } from 'astro'; | ||
| import { TUNNEL_OPTIONS_GLOBAL } from '../tunnel'; | ||
| import type { VitePlugin } from './cloudflare'; | ||
|
|
||
| const TUNNEL_OPTIONS_MODULE = 'virtual:@sentry/astro/tunnel-options'; | ||
| const RESOLVED_TUNNEL_OPTIONS_MODULE = `\0${TUNNEL_OPTIONS_MODULE}`; | ||
|
|
||
| /** The tunnel options Astro's routing requires, or `undefined` if the defaults work. */ | ||
| export function getAstroTunnelOptions(config: Pick<AstroConfig, 'base' | 'trailingSlash'>): TunnelOptions | undefined { | ||
| const basePath = config.base?.replace(/\/$/, ''); | ||
| const trailingSlash = config.trailingSlash === 'always'; | ||
|
|
||
| return basePath || trailingSlash | ||
| ? { ...(basePath && { basePath }), ...(trailingSlash && { trailingSlash }) } | ||
| : undefined; | ||
| } | ||
|
|
||
| /** Imported first in the injected init scripts, so the options are set before `Sentry.init` runs. */ | ||
| export const TUNNEL_OPTIONS_IMPORT = `import "${TUNNEL_OPTIONS_MODULE}";\n`; | ||
|
|
||
| /** Serves the module that hands the tunnel options to the SDKs. */ | ||
| export function makeTunnelOptionsVitePlugin(tunnelOptions: TunnelOptions): VitePlugin { | ||
| return { | ||
| name: 'sentry-astro-tunnel-options', | ||
| resolveId: id => (id === TUNNEL_OPTIONS_MODULE ? RESOLVED_TUNNEL_OPTIONS_MODULE : null), | ||
| load: id => | ||
| id === RESOLVED_TUNNEL_OPTIONS_MODULE | ||
| ? `globalThis[${JSON.stringify(TUNNEL_OPTIONS_GLOBAL)}] = ${JSON.stringify(tunnelOptions)};` | ||
| : null, | ||
| }; | ||
| } |
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
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
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,14 @@ | ||
| import type { Options, TunnelOptions } from '@sentry/core'; | ||
| import { applyTunnelDefaults, GLOBAL_OBJ } from '@sentry/core'; | ||
|
|
||
| /** Set by the Sentry Astro integration from the `base` and `trailingSlash` config. */ | ||
| export const TUNNEL_OPTIONS_GLOBAL = '__sentry_astro_tunnel_options'; | ||
|
|
||
| /** | ||
| * Serves the managed tunnel under the app's base path and with Astro's trailing slash policy, which Astro enforces | ||
| * before any middleware runs. Options the user set win. | ||
| */ | ||
| export function applyAstroTunnelOptions(options: Pick<Options, 'tunnel'>): void { | ||
| const astroTunnelOptions = (GLOBAL_OBJ as { [TUNNEL_OPTIONS_GLOBAL]?: TunnelOptions })[TUNNEL_OPTIONS_GLOBAL]; | ||
| options.tunnel = applyTunnelDefaults(options.tunnel, astroTunnelOptions); | ||
| } |
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
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,72 @@ | ||
| import { GLOBAL_OBJ } from '@sentry/core'; | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import { getAstroTunnelOptions, makeTunnelOptionsVitePlugin } from '../src/integration/tunnel'; | ||
| import { applyAstroTunnelOptions, TUNNEL_OPTIONS_GLOBAL } from '../src/tunnel'; | ||
|
|
||
| const globalWithOptions = GLOBAL_OBJ as { __sentry_astro_tunnel_options?: unknown }; | ||
|
|
||
| describe('getAstroTunnelOptions', () => { | ||
| it('maps `base` and `trailingSlash: always` onto tunnel options', () => { | ||
| expect(getAstroTunnelOptions({ base: '/docs/', trailingSlash: 'always' })).toEqual({ | ||
| basePath: '/docs', | ||
| trailingSlash: true, | ||
| }); | ||
| expect(getAstroTunnelOptions({ base: '/docs', trailingSlash: 'ignore' })).toEqual({ basePath: '/docs' }); | ||
| }); | ||
|
|
||
| it('returns nothing for the defaults', () => { | ||
| expect(getAstroTunnelOptions({ base: '/', trailingSlash: 'ignore' })).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('makeTunnelOptionsVitePlugin', () => { | ||
| it('serves a module that sets the tunnel options global', () => { | ||
| const plugin = makeTunnelOptionsVitePlugin({ basePath: '/docs' }); | ||
| const resolved = (plugin.resolveId as unknown as (id: string) => string | null)( | ||
| 'virtual:@sentry/astro/tunnel-options', | ||
| ); | ||
|
|
||
| expect((plugin.load as unknown as (id: string) => string | null)(resolved!)).toBe( | ||
| `globalThis[${JSON.stringify(TUNNEL_OPTIONS_GLOBAL)}] = {"basePath":"/docs"};`, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('applyAstroTunnelOptions', () => { | ||
| afterEach(() => { | ||
| delete globalWithOptions.__sentry_astro_tunnel_options; | ||
| }); | ||
|
|
||
| it('adds the Astro routing options to the managed tunnel, letting user options win', () => { | ||
| globalWithOptions.__sentry_astro_tunnel_options = { basePath: '/docs', trailingSlash: true }; | ||
| const fromTrue: Parameters<typeof applyAstroTunnelOptions>[0] = { tunnel: true }; | ||
| const fromOptions: Parameters<typeof applyAstroTunnelOptions>[0] = { tunnel: { basePath: '/other' } }; | ||
|
|
||
| applyAstroTunnelOptions(fromTrue); | ||
| applyAstroTunnelOptions(fromOptions); | ||
|
|
||
| expect(fromTrue.tunnel).toEqual({ basePath: '/docs', trailingSlash: true }); | ||
| expect(fromOptions.tunnel).toEqual({ basePath: '/other', trailingSlash: true }); | ||
| }); | ||
|
|
||
| it('serves a relative tunnel path with the Astro routing options too', () => { | ||
| globalWithOptions.__sentry_astro_tunnel_options = { basePath: '/docs', trailingSlash: true }; | ||
| const options: Parameters<typeof applyAstroTunnelOptions>[0] = { tunnel: '/monitor' }; | ||
|
|
||
| applyAstroTunnelOptions(options); | ||
|
|
||
| expect(options.tunnel).toEqual({ basePath: '/docs', trailingSlash: true, path: '/monitor' }); | ||
| }); | ||
|
|
||
| it('leaves URL tunnels and a disabled tunnel alone', () => { | ||
| globalWithOptions.__sentry_astro_tunnel_options = { basePath: '/docs' }; | ||
| const url: Parameters<typeof applyAstroTunnelOptions>[0] = { tunnel: 'https://example.com/tunnel' }; | ||
| const off: Parameters<typeof applyAstroTunnelOptions>[0] = {}; | ||
|
|
||
| applyAstroTunnelOptions(url); | ||
| applyAstroTunnelOptions(off); | ||
|
|
||
| expect(url.tunnel).toBe('https://example.com/tunnel'); | ||
| expect(off.tunnel).toBeUndefined(); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.