{"record":{"id":"b8864fe0e6fa1fbc","repo":"YMFE/yapi","slug":"context-href","errorCode":null,"errorMessage":"context.href 不能被赋值","messagePattern":"context\\.href 不能被赋值","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/postmanLib.js","lineNumber":260,"sourceCode":" * \n * @param {*} defaultOptions \n * @param {*} preScript \n * @param {*} afterScript \n * @param {*} commonContext  负责传递一些业务信息，crossRequest 不关注具体传什么，只负责当中间人\n */\nasync function crossRequest(defaultOptions, preScript, afterScript, commonContext = {}) {\n  let options = Object.assign({}, defaultOptions);\n  const taskId = options.taskId || Math.random() + '';\n  let urlObj = URL.parse(options.url, true),\n    query = {};\n  query = Object.assign(query, urlObj.query);\n  let context = {\n    isNode,\n    get href() {\n      return urlObj.href;\n    },\n    set href(val) {\n      throw new Error('context.href 不能被赋值');\n    },\n    get hostname() {\n      return urlObj.hostname;\n    },\n    set hostname(val) {\n      throw new Error('context.hostname 不能被赋值');\n    },\n\n    get caseId() {\n      return options.caseId;\n    },\n\n    set caseId(val) {\n      throw new Error('context.caseId 不能被赋值');\n    },\n\n    method: options.method,\n    pathname: urlObj.pathname,","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/YMFE/yapi/blob/59bade3a8a43e7db077d38a4b0c7c584f30ddf8c/common/postmanLib.js#L242-L278","documentation":"In the postman/script sandbox, context is a frozen-ish object exposing URL parts as getters. Assigning to context.href is intentionally forbidden — href is derived from the request URL — so the setter always throws.","triggerScenarios":"A test/script (assert or script sandbox) contains code like context.href = 'http://...' during interface case execution.","commonSituations":"Scripts ported from Postman where setting request URL via variables; users trying to redirect the request inside a script; generated script templates that mutate context.","solutions":["Remove any assignment to context.href from your script","Use context.url or the request configuration to change the target before the request, not context.href","Read context.href if you only need the current URL"],"exampleFix":"// before\ncontext.href = 'http://newhost/api';\n// after\n// do not mutate href; use request URL config instead\nconsole.log(context.href);","handlingStrategy":"type-guard","validationCode":"if (Object.getOwnPropertyDescriptor(context, 'href') && context.href === wantedUrl) { /* already correct, skip */ }","typeGuard":"function canSet(obj, key){ const d = Object.getOwnPropertyDescriptor(obj, key); return !!d && typeof d.set === 'function' && !/不能被赋值/.test(String(d.set)); }","tryCatchPattern":"try {\n  context.href = newUrl;\n} catch (e) {\n  console.warn('context is read-only; change request URL in case config instead');\n}","preventionTips":["Treat script-sandbox context as read-only","Change URLs via case/environment config, not scripts","Read-only use: const u = context.href","Review scripts copied from Postman for context mutation"],"tags":["script-sandbox","immutable-context","postman-lib"],"backgroundTag":"readonly-property-assignment","analyzedSha":"59bade3a8a43e7db077d38a4b0c7c584f30ddf8c","analyzedAt":"2026-08-29T08:45:22.203Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}