T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/session.py:15
- Finding
- Unrestricted API origin override can redirect credentials, session cookies, and source code<![CDATA[ ## Vulnerability Details **File Location**: `scripts/session.py:15-16`; affected data flows also occur in `scripts/get_problem.py:159-163`, `scripts/submit.py:25-28, 46-57`, and `scripts/check_result.py:32-35, 57-58` **Vulnerability Type**: Unvalidated network destination for sensitive data **Risk Level**: High ### Vulnerable Code ```python # scripts/session.py:15-16 BASE_URL = os.environ.get("NCCUOJ_BASE_URL", "https://nccuoj.ebg.tw") API_URL = f"{BASE_URL}/api" ``` ```python # scripts/get_problem.py:159-163 if args.username and args.password: api_post(opener, cookie_jar, f"{API_URL}/login", { "username": args.username, "password": args.password, }) ``` ```python # scripts/submit.py:46-57 with open(args.code_file, "r") as f: code = f.read() opener, cookie_jar, csrf = get_session() login(opener, cookie_jar, args.username, args.password) payload = { "problem_id": args.problem_id, "language": args.language, "code": code, } if args.contest: payload["contest_id"] = args.contest data = api_post(opener, cookie_jar, f"{API_URL}/submission", payload) ``` ### Technical Analysis The environment variable `NCCUOJ_BASE_URL` completely controls the origin used for API requests. The value is not validated against the documented NCCUOJ hostname, restricted to HTTPS, or subject to an explicit development-mode safeguard. The login, problem retrieval, source submission, and result-checking scripts all import the derived `API_URL`. Consequently, a process that can influence the environment can redirect requests to an arbitrary server. The affected requests include plaintext NCCUOJ usernames and passwords inside JSON request bodies, authentication cookies managed by the shared cookie jar, and complete contents of the selected source file. Although transmitting credentials and source code to the legitimate NCCUOJ service is necessary for authenticated submissions, permitting silent redirection to an arbitrary origin exc ...[truncated 1571 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Remove `NCCUOJ_BASE_URL` configurability from production operation and use the fixed documented origin `https://nccuoj.ebg.tw`. 2. If an override is necessary for development: - Require an explicit development-mode option. - Require the `https` scheme. - Validate the parsed hostname against a strict allowlist. - Reject URLs containing embedded credentials, fragments, unexpected paths, or unsupported ports. - Display the effective destination and require confirmation before sending credentials. 3. Prevent cross-origin redirects for requests containing passwords, cookies, or source code. Validate the final response URL after redirects. 4. Separate authenticated and public clients so public problem retrieval cannot accidentally reuse authentication cookies with an untrusted origin. 5. Add tests covering malicious values such as HTTP URLs, lookalike domains, embedded credentials, and redirect chains. ]]>
