fix: prevent relocation to root directories (#6547)

* fix: prevent relocation to root directories

* Update web-app/src/locales/zh-TW/settings.json

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>

---------

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
This commit is contained in:
Louis 2025-09-23 10:16:11 +07:00 committed by GitHub
parent 5adc0d9d46
commit 292941e1d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 50 additions and 0 deletions

View File

@ -209,6 +209,8 @@
"showInFileExplorer": "Show in File Explorer",
"openContainingFolder": "Open Containing Folder",
"failedToRelocateDataFolder": "Failed to relocate data folder",
"couldNotRelocateToRoot": "Cannot relocate data folder to root directory. Please choose another location.",
"couldNotResetRootDirectory": "Cannot reset data folder when it's set to a root directory. Please delete the data folder manually.",
"failedToRelocateDataFolderDesc": "Failed to relocate data folder. Please try again.",
"devVersion": "Development version detected",
"noUpdateAvailable": "You're running the latest version",

View File

@ -203,6 +203,8 @@
"showInFinder": "Tampilkan di Finder",
"showInFileExplorer": "Tampilkan di File Explorer",
"openContainingFolder": "Buka Folder Induk",
"couldNotRelocateToRoot": "Tidak dapat memindahkan folder data ke direktori root. Silakan pilih lokasi lain.",
"couldNotResetRootDirectory": "Tidak dapat mengatur ulang folder data saat diatur ke direktori root. Silakan hapus folder data secara manual.",
"failedToRelocateDataFolder": "Gagal memindahkan folder data",
"failedToRelocateDataFolderDesc": "Gagal memindahkan folder data. Silakan coba lagi.",
"devVersion": "Versi pengembangan terdeteksi",

View File

@ -206,6 +206,8 @@
"showInFinder": "Pokaż w Finderze",
"showInFileExplorer": "Pokaż w Explorerze",
"openContainingFolder": "Otwórz Katalog Nadrzędny",
"couldNotRelocateToRoot": "Nie można przenieść folderu danych do katalogu głównego. Proszę wybrać inną lokalizację.",
"couldNotResetRootDirectory": "Nie można zresetować folderu danych, gdy jest ustawiony na katalog główny. Proszę ręcznie usunąć folder danych.",
"failedToRelocateDataFolder": "Błąd zmiany katalogu danych",
"failedToRelocateDataFolderDesc": "Nie udało się przenieść katalogu danych. Proszę spróbować później.",
"devVersion": "Wykryto wersję deweloperską",

View File

@ -205,6 +205,8 @@
"showInFinder": "Hiển thị trong Finder",
"showInFileExplorer": "Hiển thị trong File Explorer",
"openContainingFolder": "Mở Thư mục Chứa",
"couldNotRelocateToRoot": "Không thể di chuyển thư mục dữ liệu đến thư mục gốc. Vui lòng chọn vị trí khác.",
"couldNotResetRootDirectory": "Không thể đặt lại thư mục dữ liệu khi nó được đặt thành thư mục gốc. Vui lòng xóa thư mục dữ liệu thủ công.",
"failedToRelocateDataFolder": "Không thể di chuyển thư mục dữ liệu",
"failedToRelocateDataFolderDesc": "Không thể di chuyển thư mục dữ liệu. Vui lòng thử lại.",
"devVersion": "Đã phát hiện phiên bản phát triển",

View File

@ -205,6 +205,8 @@
"showInFinder": "在 Finder 中显示",
"showInFileExplorer": "在文件资源管理器中显示",
"openContainingFolder": "打开包含文件夹",
"couldNotRelocateToRoot": "无法将数据文件夹重新定位到根目录。请选择其他位置。",
"couldNotResetRootDirectory": "当数据文件夹设置为根目录时,无法重置。请手动删除数据文件夹。",
"failedToRelocateDataFolder": "无法重新定位数据文件夹",
"failedToRelocateDataFolderDesc": "无法重新定位数据文件夹。请重试。",
"devVersion": "检测到开发版本",

View File

@ -203,6 +203,8 @@
"showInFinder": "在 Finder 中顯示",
"showInFileExplorer": "在檔案總管中顯示",
"openContainingFolder": "打開包含資料夾",
"couldNotRelocateToRoot": "無法將資料夾重新定位到根目錄。請選擇其他位置。",
"couldNotResetRootDirectory": "當資料檔案夾設定為根目錄時,無法重置。請手動刪除資料檔案夾。",
"failedToRelocateDataFolder": "無法重新定位資料夾",
"failedToRelocateDataFolderDesc": "無法重新定位資料夾。請重試。",
"devVersion": "檢測到開發版本",

View File

@ -30,6 +30,7 @@ import { useHardware } from '@/hooks/useHardware'
import LanguageSwitcher from '@/containers/LanguageSwitcher'
import { PlatformFeatures } from '@/lib/platform/const'
import { PlatformFeature } from '@/lib/platform/types'
import { isRootDir } from '@/utils/path'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const Route = createFileRoute(route.settings.general as any)({
@ -73,6 +74,11 @@ function General() {
}, [serviceHub])
const resetApp = async () => {
// Prevent resetting if data folder is root directory
if (isRootDir(janDataFolder ?? '/')) {
toast.error(t('settings:general.couldNotResetRootDirectory'))
return
}
pausePolling()
// TODO: Loading indicator
await serviceHub.app().factoryReset()
@ -117,6 +123,9 @@ function General() {
serviceHub.events().emit(SystemEvent.KILL_SIDECAR)
setTimeout(async () => {
try {
// Prevent relocating to root directory (e.g., C:\ or D:\ on Windows, / on Unix)
if (isRootDir(selectedNewPath))
throw new Error(t('settings:general.couldNotRelocateToRoot'))
await serviceHub.app().relocateJanDataFolder(selectedNewPath)
setJanDataFolder(selectedNewPath)
// Only relaunch if relocation was successful

29
web-app/src/utils/path.ts Normal file
View File

@ -0,0 +1,29 @@
/**
* Determines if the given path is a root directory.
*
* On Windows, this checks for drive roots such as `C:\` or `D:\`.
* On Mac/Linux, this checks if the path is `/`.
*
* @param selectedNewPath - The path to check.
* @returns `true` if the path is a root directory, otherwise `false`.
*/
export const isRootDir = (selectedNewPath: string) => {
// Windows root: C:\, D:\, etc.
if (IS_WINDOWS) {
return /^[a-zA-Z]:\\?$/.test(selectedNewPath)
}
// Linux/Mac root: /, /mnt, /media, etc.
const linuxRoots = [
'/',
'/mnt',
'/media',
'/boot',
'/home',
'/opt',
'/var',
'/usr',
]
const normalized =
selectedNewPath.replace(/\\/g, '/').replace(/\/+$/, '') || '/'
return linuxRoots.some((root) => normalized === root)
}