T09 · Insecure Skill Coding Practices
- Location
scripts/cli.py:82- Finding
Invalid Library Selector Can Clear the Wrong Vocabulary Library
- Content
View full analysis
2 else "-n" lib = NewWordLibrary() if target == "-n" else KnownWordLibrary() lib.clear() print(f"{'生词库' if target == '-n' else '熟词库'} 已清空") ``` ### Technical Analysis The `clear` command does not verify that the library selector is one of the two documented values, `-n` or `-k`. It treats every value other than `-n` as a request to select `KnownWordLibrary`. Because `lib.clear()` immediately saves an empty list to the selected library file, a typographical error or attacker-influenced argument can erase the known-word library without warning. Similar binary selector handling appears in the `delete` and `list` commands, but the `clear` path has the greatest impact because it removes every entry from the selected library. This does not permit arbitrary file deletion: the affected path remains constrained to the Skill-managed `data/known_words.json` file through the fixed `KnownWordLibrary` class. ### Attack Path 1. A user, script, or attacker-influenced automation invokes the destructive command with an invalid selector, for example: ```bash python scripts/cli.py clear --known ``` 2. The value of `target` becomes `--known`. 3. Since `target == "-n"` is false, the conditional silently constructs `KnownWordLibrary`. 4. `lib.clear()` replaces the in-memory word list with an empty list and saves it. 5. The contents of `data/known_words.json` are consequently erased. No elevated privileges, shell injection, or external access are required. Exploitation requires the ability to invoke the local CLI or influence its arguments. ### Impact Assessment The vulnerability can cause unintended loss of locally stored known-word vocabulary data. The attacker gains no additional ...[truncated 310 chars]- Remediation
View remediation
2 else "-n" if target not in ("-n", "-k"): print("Error: target must be -n or -k") sys.exit(1) lib = NewWordLibrary() if target == "-n" else KnownWordLibrary() lib.clear() ``` 2. Apply the same selector validation to the `delete` and `list` commands so malformed arguments cannot silently target another library. 3. Require explicit confirmation before clearing a nonempty library, or require a deliberate `--force` flag for noninteractive use. 4. Create a backup before destructive updates, or implement a recoverable archive operation instead of immediate deletion. 5. Save changes atomically by writing validated JSON to a temporary file in the same directory and then replacing the destination file. This reduces the risk of corruption if the process is interrupted. 6. Add automated tests confirming that invalid, missing, and mistyped selectors do not modify either vocabulary library. ]]>
