Restrict file creation to workspace

pull/1052/head
ioedeveloper 4 years ago
parent b9f3b137b7
commit 3968ec0b1c
  1. 10
      apps/remix-ide/src/app/files/fileManager.js
  2. 16
      apps/remix-ide/src/app/files/workspaceFileProvider.js
  3. 15
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx

@ -9,7 +9,6 @@ const globalRegistry = require('../../global/registry')
const toaster = require('../ui/tooltip') const toaster = require('../ui/tooltip')
const modalDialogCustom = require('../ui/modal-dialog-custom') const modalDialogCustom = require('../ui/modal-dialog-custom')
const helper = require('../../lib/helper.js') const helper = require('../../lib/helper.js')
const path = require('path')
/* /*
attach to files event (removed renamed) attach to files event (removed renamed)
@ -59,14 +58,7 @@ class FileManager extends Plugin {
} }
limitPluginScope (path) { limitPluginScope (path) {
const workspace = this.fileManager.currentWorkspace() return path.replace(/^\/browser\//, '').replace(/^browser\//, '') // forbids plugin to access the root filesystem
if (workspace) {
} else {
}
} }
/** /**

@ -1,6 +1,7 @@
'use strict' 'use strict'
const FileProvider = require('./fileProvider') const FileProvider = require('./fileProvider')
const path = require('path')
class WorkspaceFileProvider extends FileProvider { class WorkspaceFileProvider extends FileProvider {
constructor () { constructor () {
@ -32,8 +33,19 @@ class WorkspaceFileProvider extends FileProvider {
if (path.startsWith(this.workspacesPath + '/' + this.workspace)) return path if (path.startsWith(this.workspacesPath + '/' + this.workspace)) return path
if (path.startsWith(this.workspace)) return this.workspacesPath + '/' + this.workspace if (path.startsWith(this.workspace)) return this.workspacesPath + '/' + this.workspace
path = super.removePrefix(path) path = super.removePrefix(path)
const ret = this.workspacesPath + '/' + this.workspace + '/' + (path === '/' ? '' : path) let ret = this.workspacesPath + '/' + this.workspace + '/' + (path === '/' ? '' : path)
return ret.replace(/^\/|\/$/g, '')
ret = ret.replace(/^\/|\/$/g, '')
if (!this.isSubDirectory(this.workspacesPath + '/' + this.workspace, ret)) throw new Error('Cannot read/write to path outside workspace')
return ret
}
isSubDirectory (parent, child) {
if (!parent) return false
if (parent === child) return true
const relative = path.relative(parent, child)
return !!relative && relative.split(path.sep)[0] !== '..'
} }
resolveDirectory (path, callback) { resolveDirectory (path, callback) {

@ -314,7 +314,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
const createNewFile = (newFilePath: string) => { const createNewFile = (newFilePath: string) => {
const fileManager = state.fileManager const fileManager = state.fileManager
if (helper.checkSpecialChars(newFilePath) || helper.checkSlash(newFilePath)) return toast('special characters are not allowed') try {
helper.createNonClashingName(newFilePath, filesProvider, async (error, newName) => { helper.createNonClashingName(newFilePath, filesProvider, async (error, newName) => {
if (error) { if (error) {
modal('Create File Failed', error, { modal('Create File Failed', error, {
@ -334,6 +334,12 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
} }
}) })
} catch (error) {
return modal('File Creation Failed', error.message, {
label: 'Close',
fn: async () => {}
}, null)
}
} }
const createNewFolder = async (newFolderPath: string) => { const createNewFolder = async (newFolderPath: string) => {
@ -354,8 +360,10 @@ export const FileExplorer = (props: FileExplorerProps) => {
return { ...prevState, focusElement: [{ key: newFolderPath, type: 'folder' }] } return { ...prevState, focusElement: [{ key: newFolderPath, type: 'folder' }] }
}) })
} catch (e) { } catch (e) {
console.log('error: ', e) return modal('File Creation Failed', e.message, {
toast('Failed to create folder: ' + newFolderPath) label: 'Close',
fn: async () => {}
}, null)
} }
} }
@ -393,7 +401,6 @@ export const FileExplorer = (props: FileExplorerProps) => {
fn: () => {} fn: () => {}
}, null) }, null)
} else { } else {
if (helper.checkSpecialChars(newPath) || helper.checkSlash(newPath)) throw new Error('special characters are not allowed')
await fileManager.rename(oldPath, newPath) await fileManager.rename(oldPath, newPath)
} }
} catch (error) { } catch (error) {

Loading…
Cancel
Save