Skip to content

Commit

Permalink
fix(code-editor): allow folder in file names (#5620)
Browse files Browse the repository at this point in the history
  • Loading branch information
allardy committed Nov 1, 2021
1 parent 68eda02 commit 0bc6b1a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
6 changes: 1 addition & 5 deletions modules/code-editor/src/backend/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import {
RAW_TYPE
} from './utils'

export const FILENAME_REGEX = /^[0-9a-zA-Z_\-.]+$/

const RAW_FILES_FILTERS = ['**/*.map', 'modules/.cache/**/*', 'modules/*.cache', 'modules/*.temp_cache']

export default class Editor {
Expand Down Expand Up @@ -161,9 +159,7 @@ export default class Editor {
}

async renameFile(file: EditableFile, newName: string): Promise<void> {
if (file.type !== RAW_TYPE) {
assertValidFilename(newName)
}
assertValidFilename(newName)

const { folder, filename } = getFileLocation(file)
const newFilename = filename.replace(filename, newName)
Expand Down
9 changes: 3 additions & 6 deletions modules/code-editor/src/backend/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BUILTIN_MODULES } from 'common/defaults'
import { isValid } from 'common/utils'
import jsonlintMod from 'jsonlint-mod'
import _ from 'lodash'

import { FileDefinition, FileTypes } from './definitions'
import { FILENAME_REGEX } from './editor'
import { EditorError } from './editorError'
import { EditableFile, FilePermissions, FileType } from './typings'

Expand Down Expand Up @@ -37,7 +37,7 @@ export const assertValidJson = (content: string): boolean => {
}

export const assertValidFilename = (filename: string) => {
if (!FILENAME_REGEX.test(filename)) {
if (!isValid(filename, 'path')) {
throw new EditorError('Filename has invalid characters')
}
}
Expand Down Expand Up @@ -95,10 +95,7 @@ export const validateFilePayload = async (
throw new EditorError(`Invalid file name. Must match ${def.filenames}`)
}

// Skip standard validation for raw, since you can set a complete folder path
if (type !== RAW_TYPE) {
assertValidFilename(name)
}
assertValidFilename(name)
}

export const buildRestrictedProcessVars = () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/bp/src/common/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { sanitize, isValid } from './utils'

test('test invalid paths', () => {
expect(isValid('../folder', 'path')).toEqual(false)
expect(isValid('../folder/file.js', 'path')).toEqual(false)
expect(isValid('folder/file.js', 'path')).toEqual(true)
expect(isValid('folder/../file.js', 'path')).toEqual(false)
expect(isValid('file.js', 'path')).toEqual(true)
expect(isValid('file..js', 'path')).toEqual(true)
})
22 changes: 22 additions & 0 deletions packages/bp/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,25 @@ export const getErrorMessage = (error: Error | string | unknown): string => {

return ''
}

const regex = {
illegalFile: /[\/\?<>\\:\*\|"]/,
illegalPath: /[\?<>:\*\|"]/,
control: /[\x00-\x1f\x80-\x9f]/,
reserved: /\.\.+?(\/|\\)/g
}

export const isValid = (input: string, type: 'file' | 'path') => {
if (regex.control.test(input) || input.match(regex.reserved)) {
return false
}

return (type === 'file' && !regex.illegalFile.test(input)) || !regex.illegalPath.test(input)
}

export const sanitize = (input: string, type?: 'file' | 'path') => {
return input
.replace(regex.control, '')
.replace(regex.reserved, '')
.replace(type === 'path' ? regex.illegalPath : regex.illegalFile, '')
}

0 comments on commit 0bc6b1a

Please sign in to comment.