Skip to content

Commit fec84a0

Browse files
committed
Fix #540 by validating file names
1 parent 5d08f15 commit fec84a0

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/app/components/generator/FileCreation.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,27 @@ export function FileCreation({ model, id, method, onClose }: Props) {
1616
const { locale } = useLocale()
1717
const { projects, project, updateFile } = useProject()
1818
const [fileId, setFileId] = useState(id === 'pack_mcmeta' ? 'pack' : '')
19+
const [error, setError] = useState<string>()
20+
21+
const changeFileId = (str: string) => {
22+
setError(undefined)
23+
setFileId(str)
24+
}
1925

2026
const doSave = () => {
27+
if (!fileId.match(/^([a-z0-9_.-]+:)?[a-z0-9/_.-]+$/)) {
28+
setError('Invalid resource location')
29+
return
30+
}
2131
Analytics.saveProjectFile(id, projects.length, project.files.length, method as any)
2232
updateFile(id, undefined, { type: id, id: fileId, data: DataModel.unwrapLists(model.data) })
2333
onClose()
2434
}
2535

2636
return <Modal class="file-modal" onDismiss={onClose}>
2737
<p>{locale('project.save_current_file')}</p>
28-
<TextInput autofocus={id !== 'pack_mcmeta'} class="btn btn-input" value={fileId} onChange={setFileId} onEnter={doSave} onCancel={onClose} placeholder={locale('resource_location')} spellcheck={false} readOnly={id === 'pack_mcmeta'} />
38+
<TextInput autofocus={id !== 'pack_mcmeta'} class="btn btn-input" value={fileId} onChange={changeFileId} onEnter={doSave} onCancel={onClose} placeholder={locale('resource_location')} spellcheck={false} readOnly={id === 'pack_mcmeta'} />
39+
{error !== undefined && <span class="invalid">{error}</span>}
2940
<Btn icon="file" label={locale('project.save')} onClick={doSave} />
3041
</Modal>
3142
}

src/app/components/generator/FileRenaming.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,27 @@ export function FileRenaming({ id, name, onClose }: Props) {
1414
const { locale } = useLocale()
1515
const { projects, project, updateFile } = useProject()
1616
const [fileId, setFileId] = useState(name)
17+
const [error, setError] = useState<string>()
18+
19+
const changeFileId = (str: string) => {
20+
setError(undefined)
21+
setFileId(str)
22+
}
1723

1824
const doSave = () => {
25+
if (!fileId.match(/^([a-z0-9_.-]+:)?[a-z0-9/_.-]+$/)) {
26+
setError('Invalid resource location')
27+
return
28+
}
1929
Analytics.renameProjectFile(id, projects.length, project.files.length, 'menu')
2030
updateFile(id, name, { type: id, id: fileId })
2131
onClose()
2232
}
2333

2434
return <Modal class="file-modal" onDismiss={onClose}>
2535
<p>{locale('project.rename_file')}</p>
26-
<TextInput autofocus class="btn btn-input" value={fileId} onChange={setFileId} onEnter={doSave} placeholder={locale('resource_location')} spellcheck={false} />
36+
<TextInput autofocus class="btn btn-input" value={fileId} onChange={changeFileId} onEnter={doSave} onCancel={onClose} placeholder={locale('resource_location')} spellcheck={false} />
37+
{error !== undefined && <span class="invalid">{error}</span>}
2738
<Btn icon="pencil" label={locale('project.rename')} onClick={doSave} />
2839
</Modal>
2940
}

src/styles/global.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,11 @@ main.has-project {
14171417
pointer-events: all;
14181418
}
14191419

1420+
.modal span.invalid {
1421+
color: var(--invalid-text);
1422+
font-size: 16px;
1423+
}
1424+
14201425
[data-modals] .tree {
14211426
pointer-events: none;
14221427
}

0 commit comments

Comments
 (0)