{"record":{"id":"365f7d58f87c3969","repo":"jackwener/OpenCLI","slug":"invalid-input","errorCode":"INVALID_INPUT","errorMessage":"INVALID_INPUT","messagePattern":"INVALID_INPUT","errorType":"error_code","errorClass":"CliError","httpStatus":null,"severity":"error","filePath":"clis/zhihu/target.js","lineNumber":17,"sourceCode":"import { CliError } from '@jackwener/opencli/errors';\nconst USER_RE = /^user:([A-Za-z0-9_-]+)$/;\nconst QUESTION_RE = /^question:(\\d+)$/;\nconst ANSWER_RE = /^answer:(\\d+):(\\d+)$/;\nconst ARTICLE_RE = /^article:(\\d+)$/;\nconst USER_PATH_RE = /^\\/people\\/([A-Za-z0-9_-]+)\\/?$/;\nconst QUESTION_PATH_RE = /^\\/question\\/(\\d+)\\/?$/;\nconst ANSWER_PATH_RE = /^\\/question\\/(\\d+)\\/answer\\/(\\d+)\\/?$/;\nconst ARTICLE_PATH_RE = /^\\/p\\/(\\d+)\\/?$/;\nconst EMPTY_AUTHORITY_RE = /^https:\\/\\/(?::)?@/i;\nfunction isAllowedZhihuUrl(url) {\n    return url.protocol === 'https:' && url.username === '' && url.password === '' && url.port === '';\n}\nexport function parseTarget(input) {\n    const value = String(input).trim();\n    if (EMPTY_AUTHORITY_RE.test(value)) {\n        throw new CliError('INVALID_INPUT', 'Zhihu write commands require a normal HTTPS Zhihu URL without malformed authority', 'Example: https://www.zhihu.com/question/123456');\n    }\n    if (value.startsWith('answer:') && !ANSWER_RE.test(value)) {\n        throw new CliError('INVALID_INPUT', 'Invalid answer target, expected answer:<questionId>:<answerId>', 'Example: opencli zhihu like answer:123:456 --execute');\n    }\n    let match = value.match(USER_RE);\n    if (match) {\n        return { kind: 'user', slug: match[1], url: `https://www.zhihu.com/people/${match[1]}` };\n    }\n    match = value.match(QUESTION_RE);\n    if (match) {\n        return { kind: 'question', id: match[1], url: `https://www.zhihu.com/question/${match[1]}` };\n    }\n    match = value.match(ANSWER_RE);\n    if (match) {\n        return {\n            kind: 'answer',\n            questionId: match[1],\n            id: match[2],","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/zhihu/target.js#L1-L35","documentation":"parseTarget first rejects inputs whose authority component is malformed (EMPTY_AUTHORITY_RE) — e.g. URLs like 'https://:password@' or 'https://@host' with an empty/degenerate userinfo. Zhihu write commands only accept normal HTTPS Zhihu URLs, so any input matching that pattern throws CliError with code INVALID_INPUT plus an example of the expected form.","triggerScenarios":"Passing a URL with an empty username or password segment in the authority, e.g. 'https://@www.zhihu.com/question/123' or 'https://:x@www.zhihu.com/...', to any zhihu write command target.","commonSituations":"Pasting credentials into the URL from a shell history or script template, shell variable interpolation leaving an empty '@' segment (e.g. 'https://$USER@www.zhihu.com' with USER unset), copy-paste artifacts.","solutions":["Pass a plain URL without any userinfo: https://www.zhihu.com/question/123456","Remove '@' and credentials from the argument; use the CLI's auth mechanism instead","Quote the argument in the shell to prevent '@' or variable expansion issues"],"exampleFix":"// before\nopencli zhihu like https://$USER@www.zhihu.com/answer/456 --execute\n// after\nopencli zhihu like https://www.zhihu.com/answer/456 --execute","handlingStrategy":"validation","validationCode":"function isCleanZhihuUrl(s) {\n  try {\n    const u = new URL(String(s).trim());\n    return u.protocol === 'https:' && u.hostname.endsWith('zhihu.com') &&\n      u.username === '' && u.password === '';\n  } catch { return false; }\n}\nif (!isCleanZhihuUrl(target)) throw new Error('strip credentials from the URL');","typeGuard":"function hasNoUserinfo(u) {\n  return u instanceof URL && u.username === '' && u.password === '' && u.port === '';\n}","tryCatchPattern":"try {\n  const t = parseTarget(rawInput);\n} catch (err) {\n  if (err.code === 'INVALID_INPUT') {\n    console.error('Bad target:', err.suggestion ?? err.message);\n  } else throw err;\n}","preventionTips":["Never embed credentials in URLs; use the CLI's auth flow","Quote shell arguments to avoid '@'/variable-expansion artifacts","Pre-validate targets with the URL constructor before invoking commands","Copy canonical https://www.zhihu.com/... links only"],"tags":["zhihu","input-validation","url"],"backgroundTag":"invalid-url-input","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}