{"record":{"id":"b222f91063d14b3a","repo":"libnyanpasu/clash-nyanpasu","slug":"pac-script-must-contain-findproxyforurl-function","errorCode":null,"errorMessage":"PAC script must contain FindProxyForURL function","messagePattern":"PAC script must contain FindProxyForURL function","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/tauri/src/core/pac.rs","lineNumber":94,"sourceCode":"    }\n\n    /// Save PAC script to cache directory\n    pub async fn save_pac_script(script: &str) -> Result<PathBuf> {\n        let cache_dir = crate::utils::dirs::cache_dir()?;\n        let pac_file = cache_dir.join(\"pac.js\");\n\n        fs::write(&pac_file, script)\n            .await\n            .context(\"failed to save PAC script\")?;\n\n        Ok(pac_file)\n    }\n\n    /// Basic validation of PAC script structure - check for required functions\n    pub async fn validate_pac_script(script: &str) -> Result<()> {\n        // A basic validation without using the JS engine - just check if FindProxyForURL function exists\n        if !script.contains(\"FindProxyForURL\") {\n            return Err(anyhow::anyhow!(\n                \"PAC script must contain FindProxyForURL function\"\n            ));\n        }\n\n        // Additional basic checks could be added here if needed\n        Ok(())\n    }\n\n    /// Set system proxy to use PAC URL\n    pub fn set_pac_proxy(url: &str) -> Result<()> {\n        // Check if Autoproxy is supported on this platform\n        if !Autoproxy::is_support() {\n            return Err(anyhow::anyhow!(\n                \"PAC proxy is not supported on this platform\"\n            ));\n        }\n\n        let autoproxy = Autoproxy {","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/tauri/src/core/pac.rs#L76-L112","documentation":"validate_pac_script performs a structural check that a PAC script is usable: it requires the string \"FindProxyForURL\" to be present, since every valid PAC script must define the FindProxyForURL(url, host) entry-point function the browser/proxy resolver calls. The check is deliberately lightweight — no JavaScript engine is used.","triggerScenarios":"Calling validate_pac_script with content that lacks the substring FindProxyForURL — e.g. an HTML error page, an empty file, a 404 body saved as the script, or a truncated download.","commonSituations":"PAC URL served an error page instead of the script; user pasted wrong content into the PAC editor; script downloaded but truncated/empty; someone wrote a PAC file naming the function differently (e.g. misspelled).","solutions":["Open the script and ensure it defines function FindProxyForURL(url, host) { ... }","Re-download the script from the correct URL — the current content is likely an error page","Check for typos in the function name (validation is a substring check, so the exact casing FindProxyForURL is required)","Validate the full script in a browser/engine if it contains FindProxyForURL but still misbehaves","Restore the default/generic PAC script if you cannot author one"],"exampleFix":"// before: script body\nfunction findproxyforurl(url, host) { return \"DIRECT\"; }\n// after\nfunction FindProxyForURL(url, host) { return \"DIRECT\"; }","handlingStrategy":"validation","validationCode":"fn looks_like_pac(script: &str) -> bool {\n    script.contains(\"FindProxyForURL\") && !script.trim_start().starts_with('<') // reject HTML error pages\n}","typeGuard":"fn is_valid_pac_script(s: &str) -> bool {\n    !s.trim().is_empty() && s.contains(\"FindProxyForURL\")\n}","tryCatchPattern":"match validate_pac_script(&script).await {\n    Ok(()) => save_script(script),\n    Err(e) => log::error!(\"downloaded content is not a PAC script: {e}; keeping previous script\"),\n}","preventionTips":["Verify downloaded content is JavaScript, not an HTML error page, before saving","Always include function FindProxyForURL(url, host) with exact casing","Lint PAC scripts before deployment","Keep the default PAC template as a restore point"],"tags":["pac","validation","proxy","javascript"],"backgroundTag":"schema-validation-failed","analyzedSha":"f7dbce2997c633e484f54788035e770b3ee99773","analyzedAt":"2026-09-08T01:24:59.197Z","contentChangedAt":"2026-09-08T01:24:59.197Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}