{"record":{"id":"e95803fc6a6719e6","repo":"BigPizzaV3/CodexPlusPlus","slug":"refusing-to-delete-script-outside-user-script-dire","errorCode":null,"errorMessage":"refusing to delete script outside user script directory","messagePattern":"refusing to delete script outside user script directory","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codex-plus-core/src/user_scripts.rs","lineNumber":174,"sourceCode":"        let canonical_user_dir = self\n            .user_dir\n            .canonicalize()\n            .or_else(|_| {\n                fs::create_dir_all(&self.user_dir)?;\n                self.user_dir.canonicalize()\n            })\n            .with_context(|| {\n                format!(\n                    \"failed to resolve user script directory {}\",\n                    self.user_dir.display()\n                )\n            })?;\n        if path.exists() {\n            let canonical_path = path\n                .canonicalize()\n                .with_context(|| format!(\"failed to resolve user script {}\", path.display()))?;\n            if !canonical_path.starts_with(&canonical_user_dir) {\n                anyhow::bail!(\"refusing to delete script outside user script directory\");\n            }\n            fs::remove_file(&canonical_path).with_context(|| {\n                format!(\"failed to delete user script {}\", canonical_path.display())\n            })?;\n        }\n\n        let _guard = self.config_lock.lock().unwrap();\n        let mut config = self.load_config_unlocked();\n        config.scripts.remove(key);\n        config.market.remove(key);\n        self.save_config_unlocked(&config)?;\n        Ok(config)\n    }\n\n    pub fn user_script_path_for_market_id(&self, id: &str) -> PathBuf {\n        self.user_dir.join(market_script_filename(id))\n    }\n","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/crates/codex-plus-core/src/user_scripts.rs#L156-L192","documentation":"Before removing a user script file, the manager canonicalizes the target path and verifies it still resolves inside the canonicalized user scripts directory. This error is thrown when the resolved path falls outside that directory — a symlink or alias pointing elsewhere — to prevent deleting arbitrary files.","triggerScenarios":"Calling delete_user_script with a 'user:<file_name>' key whose file exists but is a symlink (or junction/hardlink-resolved path) pointing outside the user scripts directory, so canonicalize() resolves it elsewhere.","commonSituations":"User replaced a script file with a symlink to a shared script elsewhere; backup/sync tools replaced files with links; the user_dir itself was relocated and canonical paths shifted.","solutions":["Remove the symlink and place a real file with the script contents in the user scripts directory, then retry delete","Verify the user scripts directory location and that the target file is a regular file inside it (not a link)","If you intentionally keep external scripts via symlink, copy the script into the user dir and delete that copy instead"],"exampleFix":"// before\n// user:my.js -> symlink to /etc/some.js  => refused\n// after\nfs::remove_file(\"user_dir/my.js\");\nfs::write(\"user_dir/my.js\", real_contents);\nmanager.delete_user_script(\"user:my.js\")?;","handlingStrategy":"validation","validationCode":"let target = user_dir.join(file_name);\nlet meta = std::fs::symlink_metadata(&target)?;\nif meta.file_type().is_symlink() { /* refuse or copy-before-delete */ }","typeGuard":"fn is_regular_file_inside(p: &Path, dir: &Path) -> bool {\n    p.canonicalize().map(|c| c.starts_with(dir) && c.is_file()).unwrap_or(false)\n}","tryCatchPattern":"match manager.delete_user_script(key) {\n    Err(e) if e.to_string().contains(\"outside user script directory\") => eprintln!(\"target is a symlink; remove it manually or replace with a real file\"),\n    other => other?,\n}","preventionTips":["Do not symlink scripts into the user scripts directory; copy them instead","Periodically audit the scripts directory for links created by sync tools","Pin/canonicalize the user_dir location so it does not move between runs"],"tags":["security","symlink","path"],"backgroundTag":"path-traversal-blocked","analyzedSha":"b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6","analyzedAt":"2026-09-19T23:35:21.129Z","contentChangedAt":"2026-09-19T23:35:21.129Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}