Skip to content

Commit

Permalink
fix(query): escape special chars in path (#1185)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed May 31, 2022
1 parent 834f52e commit 87fab34
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
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)

0 comments on commit 87fab34

Please sign in to comment.