{"record":{"id":"2e5f10e1cc6b85c4","repo":"babalae/better-genshin-impact","slug":"js-http-js-http","errorCode":null,"errorMessage":"当前JS脚本不允许使用HTTP请求，请在调度器通用设置中启用“JS HTTP权限”","messagePattern":"当前JS脚本不允许使用HTTP请求，请在调度器通用设置中启用“JS HTTP权限”","errorType":"exception","errorClass":"UnauthorizedAccessException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Core/Script/Dependence/Http.cs","lineNumber":25,"sourceCode":"using System.Text.Json;\nusing System.Collections.Generic;\nusing System.Net.Http;\nusing System.Text;\nusing BetterGenshinImpact.GameTask;\nusing Microsoft.Extensions.Logging;\n\nnamespace BetterGenshinImpact.Core.Script.Dependence;\n\npublic class Http\n{\n    private readonly ILogger<Http> _logger = App.GetLogger<Http>();\n\n    private void CheckHttpPermission(string url)\n    {\n        var currentProject = TaskContext.Instance().CurrentScriptProject;\n        if (!currentProject?.AllowJsHTTP ?? false)\n        {\n            throw new UnauthorizedAccessException(\"当前JS脚本不允许使用HTTP请求，请在调度器通用设置中启用“JS HTTP权限”\");\n        }\n        var allowedUrls = currentProject?.Project?.Manifest.HttpAllowedUrls ?? [];\n        if (allowedUrls.Length == 0)\n        {\n            throw new UnauthorizedAccessException(\"当前JS脚本没有配置允许请求的URL，请在脚本的manifest.json中配置http_allowed_urls\");\n        }\n        if (allowedUrls.Any(allowedUrl =>\n        {\n            // fuzzy match\n            var pattern = \"^\" + System.Text.RegularExpressions.Regex.Escape(allowedUrl).Replace(\"\\\\*\", \".*\") + \"$\";\n            _logger.LogDebug($\"[HTTP] 检查URL {url} 是否符合: {pattern}\");\n            var regex = new System.Text.RegularExpressions.Regex(pattern);\n            return regex.IsMatch(url);\n        }))\n        {\n            return;\n        }\n        throw new UnauthorizedAccessException($\"当前JS脚本不允许请求此URL: {url}，请在脚本的manifest.json中配置http_allowed_urls，当前允许的URL列表: [{string.Join(\", \", allowedUrls)}]\");","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Script/Dependence/Http.cs#L7-L43","documentation":"Thrown as UnauthorizedAccessException by Http.CheckHttpPermission when the current script project does not have JS HTTP permission enabled. The check reads TaskContext.Instance().CurrentScriptProject.AllowJsHTTP — if it is false or null (no project), all HTTP calls are blocked. This is a sandbox security gate: JS scripts cannot make network requests unless explicitly permitted.","triggerScenarios":"Calling any HTTP method (Get, Post, etc.) from a JS script when AllowJsHTTP is false or when CurrentScriptProject is null (no active project context). The permission must be enabled in the scheduler's general settings (调度器通用设置).","commonSituations":"User wrote a script that uses http.get() or http.post() but hasn't enabled the 'JS HTTP权限' toggle in scheduler settings. Script runs outside a project context (manually injected). Settings were reset or the config file was modified externally.","solutions":["Enable 'JS HTTP权限' in the scheduler general settings (调度器通用设置 → JS HTTP权限).","Verify CurrentScriptProject is set — the script must run within a registered script project, not ad-hoc.","If permission cannot be granted, replace HTTP calls with local file reads or pre-cached data.","Check that the ScriptGroupProject.AllowJsHTTPHash matches the current URL allowlist hash (mismatch = permission disabled)."],"exampleFix":"// No code fix — this requires user action in settings.\n// Enable: 调度器通用设置 → JS HTTP权限 = ON\n// Then ensure manifest.json has http_allowed_urls configured.","handlingStrategy":"validation","validationCode":"// Check HTTP permission before making requests\nvar project = TaskContext.Instance().CurrentScriptProject;\nif (project?.AllowJsHTTP != true)\n{\n    _logger.LogError(\"JS HTTP permission not enabled. Enable in scheduler settings: JS HTTP权限\");\n    return;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var resp = http.Get(url, headers);\n}\ncatch (UnauthorizedAccessException ex) when (ex.Message.Contains(\"不允许使用HTTP请求\"))\n{\n    _logger.LogError(\"HTTP permission denied. Enable 'JS HTTP权限' in scheduler settings.\");\n}","preventionTips":["Enable JS HTTP permission in scheduler general settings before using HTTP in scripts.","Ensure the script runs within a registered script project context.","Check AllowJsHTTP in script initialization and fail early with guidance."],"tags":["security","permission","http","sandbox","authorization","config"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}