validate filenames

This commit is contained in:
Alexopus
2025-07-19 22:33:44 +02:00
parent 2ad6f4b638
commit 4a5f573d53
+31 -2
View File
@@ -427,10 +427,19 @@ class NotesSidebar {
}
async renameActiveNote() {
const newName = prompt('Enter new note name:', this.activeNote);
let newName = prompt('Enter new note name:', this.activeNote);
if (!newName) return;
// Trim spaces
newName = newName.trim();
if (!newName || newName === this.activeNote) return;
try {
if (!isValidFilename(newName)) {
alert('Invalid filename! Please avoid special characters like / \\ : * ? " < > |');
return;
}
// Check if new name already exists
if (this.notes.includes(newName)) {
alert('A note with this name already exists!');
@@ -471,11 +480,20 @@ class NotesSidebar {
async createNewNote() {
let noteName;
// Keep asking for a name until user cancels or enters a unique name
// Keep asking for a name until user cancels or enters a valid unique name
while (true) {
noteName = prompt('Enter note name:');
if (!noteName) return; // User cancelled
// Trim spaces
noteName = noteName.trim();
// Validate filename safety
if (!isValidFilename(noteName)) {
alert('Invalid filename! Please avoid special characters like / \\ : * ? " < > |');
continue; // Ask again
}
// Check if note name already exists
if (this.notes.includes(noteName)) {
alert('A note with this name already exists!');
@@ -528,6 +546,17 @@ class NotesSidebar {
}
}
// Helper function to validate filename safety
function isValidFilename(filename) {
if (!filename || filename.trim() === '') return false;
// Check for unsafe characters: / \\ : * ? " < > |
const unsafeChars = /[/\\:*?"<>|]/;
if (unsafeChars.test(filename)) return false;
return true;
}
const notesSidebar = new NotesSidebar();
app.registerExtension({