Repository navigation
fix(runtime): tighten asset checks #768
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
Merged
+218
−22
Merged
Changes from 1 commit
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
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,89 @@ | ||
| import type { H3Event } from '#nuxtseo/h3' | ||
| import type { OgImageRenderEventContext, VNode } from '../../src/runtime/types' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { walkTree } from '../../src/runtime/server/og-image/core/plugins' | ||
| import imageSrc from '../../src/runtime/server/og-image/core/plugins/imageSrc' | ||
| import { fetchLocalAsset } from '../../src/runtime/server/util/fetchLocalAsset' | ||
|
|
||
| const transport = vi.hoisted(() => ({ local: vi.fn(), external: vi.fn() })) | ||
| vi.mock('#nuxtseo/nitro', () => ({ | ||
| fetchWithEvent: transport.local, | ||
| useStorage: () => ({ hasItem: async () => false }), | ||
| })) | ||
| vi.mock('#nuxtseo/h3', () => ({ getRequestURL: () => new URL('https://site.example/render') })) | ||
| vi.mock('#site-config/server/composables', () => ({ getNitroOrigin: () => 'https://site.example' })) | ||
| vi.mock('ofetch', () => ({ $fetch: transport.external })) | ||
|
|
||
| const crossOriginPaths = ['//other.example/logo.png', '/\\other.example/logo.png', '/\t/other.example/logo.png', '/\n/other.example/logo.png'] | ||
|
|
||
| function createEvent(assets?: { fetch: (url: string) => Promise<Response> }): H3Event { | ||
| return { context: { ASSETS: assets } } as unknown as H3Event | ||
| } | ||
|
|
||
| function createContext(): OgImageRenderEventContext { | ||
| return { | ||
| e: createEvent(), | ||
| publicStoragePath: 'public', | ||
| runtimeConfig: { app: { baseURL: '/' }, fetchTimeout: 20 }, | ||
| timings: { | ||
| measure: async <T>(_name: string, work: () => T | Promise<T>) => work(), | ||
| start: () => () => {}, | ||
| }, | ||
| } as unknown as OgImageRenderEventContext | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| transport.local.mockResolvedValue(undefined) | ||
| transport.external.mockResolvedValue(undefined) | ||
| }) | ||
|
|
||
| describe('local asset fetch', () => { | ||
| it.each(crossOriginPaths)('keeps %j outside local transports', async (path) => { | ||
| const assets = { fetch: vi.fn(async () => new Response('asset')) } | ||
| expect(await fetchLocalAsset(createEvent(assets), path, { fetchTimeout: 20, includeExternalFallback: true })).toBeUndefined() | ||
| expect(assets.fetch).not.toHaveBeenCalled() | ||
| expect(transport.local).not.toHaveBeenCalled() | ||
| expect(transport.external).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('keeps asset query parameters when using the local transport', async () => { | ||
| const bytes = new ArrayBuffer(4) | ||
| transport.local.mockResolvedValue(bytes) | ||
| const event = createEvent() | ||
| expect(await fetchLocalAsset(event, '/logo.png?width=32', { fetchTimeout: 20 })).toBe(bytes) | ||
| expect(transport.local).toHaveBeenCalledWith(event, '/logo.png?width=32', expect.any(Object)) | ||
| }) | ||
|
|
||
| it('reads a same-origin asset from the binding', async () => { | ||
| const assets = { fetch: vi.fn(async () => new Response('asset')) } | ||
| const bytes = await fetchLocalAsset(createEvent(assets), '/logo.png?width=32', { fetchTimeout: 20 }) | ||
| expect(new TextDecoder().decode(bytes)).toBe('asset') | ||
| expect(assets.fetch).toHaveBeenCalledWith('https://site.example/logo.png?width=32', expect.any(Object)) | ||
| expect(transport.local).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| describe('image asset transformation', () => { | ||
| it.each(crossOriginPaths)('removes %j from image nodes', async (src) => { | ||
| const node: VNode = { type: 'img', props: { src } } | ||
| await Promise.all(walkTree(createContext(), node, [imageSrc])) | ||
| expect(node.props.src).toBeUndefined() | ||
| expect(transport.local).not.toHaveBeenCalled() | ||
| expect(transport.external).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it.each(crossOriginPaths)('removes %j from background images', async (src) => { | ||
| const node: VNode = { type: 'div', props: { style: { backgroundImage: `url(${src})` } } } | ||
| await Promise.all(walkTree(createContext(), node, [imageSrc])) | ||
| expect(node.props.style?.backgroundImage).toBeUndefined() | ||
| expect(transport.local).not.toHaveBeenCalled() | ||
| expect(transport.external).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('keeps the app origin when a local image is unavailable', async () => { | ||
| const node: VNode = { type: 'img', props: { src: '/logo.png?width=32' } } | ||
| await Promise.all(walkTree(createContext(), node, [imageSrc])) | ||
| expect(node.props.src).toBe('https://site.example/logo.png?width=32') | ||
| }) | ||
| }) |
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.