T09 · Insecure Skill Coding Practices
- Location
wechat_template_generator.py:7- Finding
Unrestricted Theme Path Allows Arbitrary Local File Disclosure
- Content
View full analysis
Vulnerability Details
File Location:
skill_entry.py:37-44;wechat_template_generator.py:7-14
Vulnerability Type: Path traversal and unrestricted local file read
Risk Level: MediumComplete Code Snippet
python # skill_entry.py if __name__ == "__main__": input_file = sys.argv[1] if len(sys.argv) > 1 else "article.md" theme_choice = sys.argv[2] if len(sys.argv) > 2 else "theme_orange.css" if os.path.exists(input_file): with open(input_file, 'r', encoding='utf-8') as f: content = f.read() result = generate_wechat_article( content, title="悟空极度舒适", theme=theme_choice )python # wechat_template_generator.py class WeChatTemplate: def __init__(self, theme_css='theme_orange.css'): current_dir = os.path.dirname(os.path.abspath(__file__)) full_css_path = os.path.join(current_dir, 'themes', theme_css) if os.path.exists(full_css_path): with open(full_css_path, 'r', encoding='utf-8') as f: self.css_content = f.read() else: self.css_content = "/* 默认样式 */ body { font-family: sans-serif; }"Technical Analysis
The second command-line argument is accepted as a theme path and passed unchanged to
WeChatTemplate. The code joins that value to thethemesdirectory but does not validate the resulting canonical path or restrict it to the ten documented CSS files.A path containing
../segments can escape the intended directory. In addition, Python'sos.path.joindiscards earlier components when the final component is an absolute path. Therefore, an attacker who can control the theme argument can select an arbitrary readable local file.The selected file is loaded into
self.css_contentand later embedded in the generated HTML document's<style>element. This converts the generat ...[truncated 1271 chars]- Remediation
View remediation
Remediation Suggestions
- Define an explicit allowlist containing only the ten supported theme filenames.
- Reject absolute paths, directory separators, traversal components, and unknown filenames.
- Resolve both the theme directory and candidate file with
pathlib.Path.resolve(). - Verify that the candidate is a direct child of the theme directory before opening it.
- Verify that the candidate is a regular
.cssfile rather than a directory or unexpected special file. - Return a clear error instead of silently accepting unsafe values.
Example hardening:
python from pathlib import Path ALLOWED_THEMES = { "theme_orange.css", "theme_blue.css", "theme_green.css", "theme_purple.css", "theme_gray.css", "theme_pink.css", "theme_brown.css", "theme_gold.css", "theme_cyan.css", "theme_navy.css", } theme_dir = (Path(__file__).resolve().parent / "themes").resolve() if theme_css not in ALLOWED_THEMES: raise ValueError("Unsupported theme") candidate = (theme_dir / theme_css).resolve() if candidate.parent != theme_dir or not candidate.is_file(): raise ValueError("Invalid theme path") self.css_content = candidate.read_text(encoding="utf-8")
