Skip to content
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

fix(query): escape special chars in path #1185

Merged
merged 2 commits into from May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/runtime/composables/query.ts
Expand Up @@ -39,7 +39,9 @@ export function queryContent<T = ParsedContent>(query: string, ...pathParts: str
export function queryContent<T = ParsedContent> (query: QueryBuilderParams): QueryBuilder<T>;
export function queryContent<T = ParsedContent> (query?: string | QueryBuilderParams, ...pathParts: string[]) {
if (typeof query === 'string') {
const path = withLeadingSlash(withoutTrailingSlash(joinURL(query, ...pathParts)))
let path = withLeadingSlash(withoutTrailingSlash(joinURL(query, ...pathParts)))
// escape regex special chars
path = path.replace(/[-[\]{}()*+.,^$\s]/g, '\\$&')

return createQuery<T>(queryFetch).where({ _path: new RegExp(`^${path}`) })
}
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/server/storage.ts
Expand Up @@ -128,6 +128,9 @@ export function serverQueryContent<T = ParsedContent> (event: CompatibilityEvent
let params = (path || {}) as Partial<QueryBuilderParams>
if (typeof path === 'string') {
path = withLeadingSlash(joinURL(path, ...pathParts))
// escape regex special chars
path = path.replace(/[-[\]{}()*+.,^$\s]/g, '\\$&')

params = {
where: [{ _path: new RegExp(`^${path}`) }]
}
Expand Down
4 changes: 4 additions & 0 deletions test/basic.test.ts
Expand Up @@ -99,6 +99,10 @@ describe('fixtures:basic', async () => {
expect(html).not.contains('<meta name="description" content="Description overwritten"><meta property="og:image" content="https://picsum.photos/200/300">')
})

test('partial:specials-chars', async () => {
const html = await $fetch('/_partial/content-(v2)')
expect(html).contains('Content (v2)')
})
testNavigation()

testMarkdownParser()
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/basic/content/_partial/content (v2).md
@@ -0,0 +1 @@
# Content (v2)