T09 · Insecure Skill Coding Practices
- Location
- scripts/login_pw.py:234
- Finding
- Alibaba session cookies are over-collected, flattened across domains, and stored without restrictive permissions<![CDATA[ ## Vulnerability Details **File Location**: `scripts/login_pw.py:234-263`; related cookie replay occurs at `scripts/tingwu.py:52-62` **Vulnerability Type**: Excessive credential collection and insecure plaintext credential storage **Risk Level**: High ### Vulnerable Code ```python raw = ctx.cookies() names = {c["name"] for c in raw} critical = ("login_aliyunid_ticket", "login_aliyunid", "XSRF-TOKEN", "JSESSIONID", "atpsida", "isg") missing = [k for k in critical if k not in names] if missing: print(f"!! 关键 cookie 缺失: {missing}") print(" 听悟 API 将判 [CMN.NotLogin] 并拒绝转录。请:") print(" 1) 重新运行本脚本登录;") print(" 2) 检查浏览器是否被广告拦截/隐私插件劫持 cookie。") browser.close() sys.exit(4) cookie_map = {} for c in raw: d = c.get("domain", "") if "aliyun.com" in d or "taobao.com" in d or "alicdn.com" in d: cookie_map[c["name"]] = c["value"] data = { "saved_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "cookies": cookie_map, "verified_critical_cookies": list(critical), } COOKIE_PATH.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8") ``` The flattened cookies are subsequently reassigned to the Alibaba domain: ```python def _load_cookies(self): if not self.cookie_path.exists(): raise FileNotFoundError( f"Cookie 文件不存在: {self.cookie_path}\n" "请先运行: python3 scripts/login.py" ) with open(self.cookie_path) as f: data = json.load(f) cookies = data.get("cookies", data) for name, value in cookies.items(): self.session.cookies.set(name, value, domain=".aliyun.com") ``` ### Technical Analysis The login script captures every browser-context cookie whose domain string contains `aliyun.com`, `taobao.com`, or `alicdn.com`. This exceeds the minimum credentials needed to authenticate to the Tingwu API. Cookies are then flattened into a name-to-value map, losing their original domain, path, expiry, `Secure`, `HttpOn ...[truncated 2233 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Capture only an explicit allowlist of cookies verified as necessary for Tingwu: - `login_aliyunid_ticket` - `login_aliyunid` - `XSRF-TOKEN` - `JSESSIONID` - Any additional cookie demonstrated to be required by the API 2. Reject cookies whose original domain is outside the exact approved Alibaba/Tingwu domains. 3. Preserve each cookie's original domain and path instead of flattening it into a map. 4. Never reassign Taobao or Alicdn cookies to `.aliyun.com`. 5. Create the credential file atomically with mode `0600`, for example using `os.open()` with `O_CREAT | O_EXCL` and permission `0o600`. 6. Verify and repair permissions on an existing cookie file before writing. 7. Prefer an operating-system credential store or encrypted secret storage over a project-local JSON file. 8. Delete cookies on logout and document a session-revocation procedure. 9. Add automated tests proving that unrelated-domain cookies are neither saved nor transmitted. ]]>
