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(storage): warn & ignore files with invalid characters #1239

Merged
merged 1 commit into from
Jun 14, 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
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