Skip to content

Commit

Permalink
fix(storage): warn & ignore files with invalid characters (#1239)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Jun 14, 2022
1 parent 6c903de commit b52f678
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/runtime/server/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,34 @@ export const cacheParsedStorage = prefixStorage(useStorage(), 'cache:content:par
const isProduction = process.env.NODE_ENV === 'production'

const contentConfig = useRuntimeConfig().content

/**
* Content ignore patterns
*/
export const contentIgnores = contentConfig.ignores.map((p: any) =>
typeof p === 'string' ? new RegExp(`^${p}`) : p
export const contentIgnores: Array<RegExp> = contentConfig.ignores.map((p: any) =>
typeof p === 'string' ? new RegExp(`^${p}|:${p}`) : p
)

/**
* Invalid key characters
*/
const invalidKeyCharacters = "'\"?#/".split('')

/**
* Filter predicate for ignore patterns
*/
const contentIgnorePredicate = (key: string) =>
!key.startsWith('preview:') && !contentIgnores.some((prefix: RegExp) => key.split(':').some(k => prefix.test(k)))
const contentIgnorePredicate = (key: string) => {
if (key.startsWith('preview:') || contentIgnores.some(prefix => prefix.test(key))) {
return false
}
if (invalidKeyCharacters.some(ik => key.includes(ik))) {
// eslint-disable-next-line no-console
console.warn(`Ignoring [${key}]. File name should not contain any of the following characters: ${invalidKeyCharacters.join(', ')}`)
return false
}

return true
}

export const getContentsIds = async (event: CompatibilityEvent, prefix?: string) => {
let keys = []
Expand Down
9 changes: 8 additions & 1 deletion test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fileURLToPath } from 'url'
import { assert, test, describe, expect } from 'vitest'
import { assert, test, describe, expect, vi } from 'vitest'
import { setup, $fetch } from '@nuxt/test-utils'
import { hash } from 'ohash'
import { testMarkdownParser } from './features/parser-markdown'
Expand All @@ -15,6 +15,8 @@ import { testParserHooks } from './features/parser-hooks'
import { testModuleOption } from './features/module-options'
import { testContentQuery } from './features/content-query'

const spyConsoleWarn = vi.spyOn(global.console, 'warn')

describe('fixtures:basic', async () => {
await setup({
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
Expand Down Expand Up @@ -106,6 +108,11 @@ describe('fixtures:basic', async () => {
expect(html).contains('Content (v2)')
})

test('warn invalid file name', () => {
expect(spyConsoleWarn).toHaveBeenCalled()
expect(spyConsoleWarn).toHaveBeenCalledWith('Ignoring [content:with-\'invalid\'-char.md]. File name should not contain any of the following characters: \', ", ?, #, /')
})

testContentQuery()

testNavigation()
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/basic/content/with-'invalid'-char.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This file's name is invalid

0 comments on commit b52f678

Please sign in to comment.