T09 · Insecure Skill Coding Practices
- Location
scripts/create_qwen_nothink_ollama.py:111- Finding
Unescaped System Message Permits Modelfile Directive Injection
- Content
View full analysis
str: lines = [ f"FROM {source}", f'TEMPLATE """{NO_THINK_TEMPLATE}"""', ] saw_stop = False for key, value in params: if key == "stop": saw_stop = True continue lines.append(f"PARAMETER {key} {value}") if not saw_stop: lines.append('PARAMETER stop "<|im_end|>"') else: lines.append('PARAMETER stop "<|im_end|>"') lines.append(f'SYSTEM """\n{system}\n"""') return "\n".join(lines) + "\n" ``` ```python parser.add_argument("--system", default=DEFAULT_SYSTEM, help="System message for the derived tag.") ``` ```python modelfile = build_modelfile(source, parameters, args.system) ``` The generated file is subsequently passed to Ollama: ```python with tempfile.TemporaryDirectory(prefix="qwen-nothink-") as tmp: modelfile_path = Path(tmp) / "Modelfile" modelfile_path.write_text(modelfile, encoding="utf-8") run(["ollama", "create", target, "-f", str(modelfile_path)]) ``` ### Technical Analysis The user-controlled `--system` value is interpolated directly into a triple-quoted Modelfile block. The implementation neither escapes nor rejects the terminating `"""` sequence. An input containing `"""` can therefore terminate the intended `SYSTEM` block and append additional Modelfile content. Although the subprocess is invoked with an argument list and is not vulnerable to shell metacharacter injection, the generated Modelfile itself is a secondary command/configuration language. Consequently, avoiding `shell=True` does not prevent this form of configuration-language injection. The injected content is interpreted by `ollama create`, not merely stored as system- ...[truncated 1409 chars]- Remediation
View remediation
