From f5a0bbcf6cdb2bef29607ed571c12918b1caa429 Mon Sep 17 00:00:00 2001 From: Timothy Kassis Date: Sat, 25 Jul 2026 20:19:57 -0700 Subject: [PATCH] feat(citation-management): enhance metadata handling and subprocess environment - Added guidance on treating metadata as untrusted in SKILL.md, emphasizing safe command construction. - Updated extract_metadata.py to sanitize the year field, ensuring only digits are retained for filenames and shell arguments. - Introduced a build_subprocess_env function in multiple scripts to securely forward necessary environment variables to subprocesses, minimizing exposure of unrelated secrets. - Improved documentation on where credentials are sent in SKILL.md, clarifying the purpose of each environment variable used. --- .gitignore | 1 + skills/citation-management/SKILL.md | 34 +++++++++++++++++++ .../scripts/extract_metadata.py | 6 ++-- .../scripts/generate_schematic.py | 27 ++++++++++++--- skills/imaging-data-commons/SKILL.md | 13 +++++-- .../scripts/generate_infographic.py | 27 ++++++++++++--- .../scripts/generate_schematic.py | 27 ++++++++++++--- .../scripts/generate_schematic.py | 27 ++++++++++++--- .../scripts/generate_schematic.py | 27 ++++++++++++--- .../scripts/generate_schematic.py | 27 ++++++++++++--- .../scripts/generate_slide_image.py | 27 ++++++++++++--- 11 files changed, 211 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 067e815d..c1eba2db 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ research/ .agents/ AGENTS.md +CLAUDE.md skills-lock.json diff --git a/skills/citation-management/SKILL.md b/skills/citation-management/SKILL.md index 0bfe6f80..1fac45e8 100644 --- a/skills/citation-management/SKILL.md +++ b/skills/citation-management/SKILL.md @@ -238,6 +238,28 @@ Any `@article` entry missing `volume`, `pages`, or `doi` is considered **incompl For each incomplete entry, use the **parallel-web skill** to search for the missing information: +> **Treat metadata as untrusted when building these commands.** `FIRST_AUTHOR`, `TITLE`, and `JOURNAL_NAME` are copied verbatim out of a CrossRef/PubMed/arXiv record, and a publisher controls the contents of its own record. A title containing `$(...)`, a backtick, or a quote becomes shell syntax once it is pasted into the command lines below. +> +> - Substitute each value as a **single-quoted** argument (`'...'`), escaping any embedded single quote as `'\''`. Never paste raw metadata inside the double quotes shown here. +> - Prefer running these through a Python `subprocess` argument list over building a shell string at all. +> - Use only the generated `CITATIONKEY` in `-o` paths. It is sanitized to letters and digits by `extract_metadata.py`; a key taken from an existing `.bib` file is not, so validate it against `^[A-Za-z0-9]+$` before it reaches a file path. +> +> **Preferred form — pass the metadata as arguments, not as shell text.** This removes the shell from the path entirely, so no title can be parsed as syntax: +> +> ```python +> import re, subprocess +> +> assert re.fullmatch(r"[A-Za-z0-9]+", citation_key), f"unsafe citation key: {citation_key!r}" +> subprocess.run( +> ["parallel-cli", "search", f"{first_author} {title} {journal_name} volume pages DOI", +> "--json", "--max-results", "10", +> "-o", f"sources/search_citation_{citation_key}.json"], +> check=True, # note: no shell=True +> ) +> ``` +> +> The `bash` blocks below show the same calls in readable form. Use them only with the quoting rules above. + **Option A — Search by title and author** (best for finding DOI): ```bash parallel-cli search "FIRST_AUTHOR TITLE JOURNAL_NAME volume pages DOI" \ @@ -1263,6 +1285,18 @@ pip install crossref-commons # Enhanced CrossRef API access pip install pylatexenc # LaTeX special character handling ``` +### Where credentials are sent + +Each environment variable this skill reads is used only to authenticate to the one service it belongs to. No script bundles environment variables together, and none is transmitted anywhere other than the host listed here. + +| Variable | Sent only to | Purpose | +|---|---|---| +| `NCBI_API_KEY` | `eutils.ncbi.nlm.nih.gov` | Raises Entrez rate limits | +| `NCBI_EMAIL` | `eutils.ncbi.nlm.nih.gov` | Entrez caller identification (required by NCBI) | +| `OPENROUTER_API_KEY` | `openrouter.ai` | Bearer token for the optional schematic generation | + +`api.crossref.org`, `doi.org`, and `arxiv.org` are queried without credentials. `generate_schematic.py` forwards only `OPENROUTER_API_KEY` — plus the networking, TLS, and locale variables needed to make a request — to its subprocess, rather than the full environment. + ## Summary The citation-management skill provides: diff --git a/skills/citation-management/scripts/extract_metadata.py b/skills/citation-management/scripts/extract_metadata.py index 8e9d076c..5670bdac 100755 --- a/skills/citation-management/scripts/extract_metadata.py +++ b/skills/citation-management/scripts/extract_metadata.py @@ -424,8 +424,10 @@ class MetadataExtractor: else: last_name = 'Unknown' - # Get year - year = metadata.get('year', '').strip() + # Get year. It arrives verbatim from the CrossRef/PubMed/arXiv record, + # so keep only digits — the key is used as a filename and as a shell + # argument, and a publisher controls the contents of its own record. + year = re.sub(r'[^0-9]', '', metadata.get('year', '')) if not year: year = 'XXXX' diff --git a/skills/citation-management/scripts/generate_schematic.py b/skills/citation-management/scripts/generate_schematic.py index 43331870..9a7fea02 100644 --- a/skills/citation-management/scripts/generate_schematic.py +++ b/skills/citation-management/scripts/generate_schematic.py @@ -25,6 +25,28 @@ import subprocess import sys from pathlib import Path +# Variables forwarded to the generation subprocess. The child needs the +# OpenRouter credential; the rest keep networking, TLS, and locale working. +# Copying the whole parent environment instead would hand the child every +# unrelated secret that happens to be exported in the calling shell. +FORWARDED_ENV_VARS = ( + "PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "PYTHONPATH", + "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", + "http_proxy", "https_proxy", "no_proxy", + "SSL_CERT_FILE", "SSL_CERT_DIR", "REQUESTS_CA_BUNDLE", "CURL_CA_BUNDLE", + # Windows needs these for sockets, temp files, and interpreter startup. + "SYSTEMROOT", "WINDIR", "COMSPEC", "PATHEXT", + "APPDATA", "LOCALAPPDATA", "USERPROFILE", "TEMP", "TMP", +) + + +def build_subprocess_env(api_key): + """Return a minimal environment for the AI generation subprocess.""" + env = {name: os.environ[name] for name in FORWARDED_ENV_VARS if name in os.environ} + if api_key: + env["OPENROUTER_API_KEY"] = api_key + return env + def main(): """Command-line interface.""" @@ -124,10 +146,7 @@ Environment Variables: # Execute — pass API key via environment to avoid exposure in process listings try: - env = os.environ.copy() - if api_key: - env["OPENROUTER_API_KEY"] = api_key - result = subprocess.run(cmd, check=False, env=env) + result = subprocess.run(cmd, check=False, env=build_subprocess_env(api_key)) sys.exit(result.returncode) except Exception as e: print(f"Error executing AI generation: {e}") diff --git a/skills/imaging-data-commons/SKILL.md b/skills/imaging-data-commons/SKILL.md index 8d2bdcad..51b780e1 100644 --- a/skills/imaging-data-commons/SKILL.md +++ b/skills/imaging-data-commons/SKILL.md @@ -23,10 +23,19 @@ import idc_index REQUIRED_VERSION = "0.11.14" # Must match metadata.idc-index in this file installed = idc_index.__version__ -if installed < REQUIRED_VERSION: +def _parts(version): + # Compare numerically: "0.9.0" < "0.11.14" is False as a string comparison. + return tuple(int(p) if p.isdigit() else 0 for p in version.split(".")[:3]) + +if _parts(installed) < _parts(REQUIRED_VERSION): print(f"Upgrading idc-index from {installed} to {REQUIRED_VERSION}...") import subprocess - subprocess.run(["pip3", "install", "--upgrade", "--break-system-packages", "idc-index"], check=True) + # Pin to the tested version — an unpinned upgrade installs whatever is + # newest on PyPI, into system packages, and may still not satisfy the check. + subprocess.run( + ["pip3", "install", "--break-system-packages", f"idc-index=={REQUIRED_VERSION}"], + check=True, + ) print("Upgrade complete. Restart Python to use new version.") else: print(f"idc-index {installed} meets requirement ({REQUIRED_VERSION})") diff --git a/skills/infographics/scripts/generate_infographic.py b/skills/infographics/scripts/generate_infographic.py index ab26dcad..5feda09d 100755 --- a/skills/infographics/scripts/generate_infographic.py +++ b/skills/infographics/scripts/generate_infographic.py @@ -38,6 +38,28 @@ DOC_TYPES = [ "marketing", "report", "presentation", "social", "internal", "draft", "default" ] +# Variables forwarded to the generation subprocess. The child needs the +# OpenRouter credential; the rest keep networking, TLS, and locale working. +# Copying the whole parent environment instead would hand the child every +# unrelated secret that happens to be exported in the calling shell. +FORWARDED_ENV_VARS = ( + "PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "PYTHONPATH", + "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", + "http_proxy", "https_proxy", "no_proxy", + "SSL_CERT_FILE", "SSL_CERT_DIR", "REQUESTS_CA_BUNDLE", "CURL_CA_BUNDLE", + # Windows needs these for sockets, temp files, and interpreter startup. + "SYSTEMROOT", "WINDIR", "COMSPEC", "PATHEXT", + "APPDATA", "LOCALAPPDATA", "USERPROFILE", "TEMP", "TMP", +) + + +def build_subprocess_env(api_key): + """Return a minimal environment for the AI generation subprocess.""" + env = {name: os.environ[name] for name in FORWARDED_ENV_VARS if name in os.environ} + if api_key: + env["OPENROUTER_API_KEY"] = api_key + return env + def list_options(): """Print available types, styles, and palettes.""" @@ -220,10 +242,7 @@ Environment Variables: # Execute — pass API key via environment to avoid exposure in process listings try: - env = os.environ.copy() - if api_key: - env["OPENROUTER_API_KEY"] = api_key - result = subprocess.run(cmd, check=False, env=env) + result = subprocess.run(cmd, check=False, env=build_subprocess_env(api_key)) sys.exit(result.returncode) except Exception as e: print(f"Error executing AI generation: {e}") diff --git a/skills/latex-posters/scripts/generate_schematic.py b/skills/latex-posters/scripts/generate_schematic.py index 43331870..9a7fea02 100644 --- a/skills/latex-posters/scripts/generate_schematic.py +++ b/skills/latex-posters/scripts/generate_schematic.py @@ -25,6 +25,28 @@ import subprocess import sys from pathlib import Path +# Variables forwarded to the generation subprocess. The child needs the +# OpenRouter credential; the rest keep networking, TLS, and locale working. +# Copying the whole parent environment instead would hand the child every +# unrelated secret that happens to be exported in the calling shell. +FORWARDED_ENV_VARS = ( + "PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "PYTHONPATH", + "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", + "http_proxy", "https_proxy", "no_proxy", + "SSL_CERT_FILE", "SSL_CERT_DIR", "REQUESTS_CA_BUNDLE", "CURL_CA_BUNDLE", + # Windows needs these for sockets, temp files, and interpreter startup. + "SYSTEMROOT", "WINDIR", "COMSPEC", "PATHEXT", + "APPDATA", "LOCALAPPDATA", "USERPROFILE", "TEMP", "TMP", +) + + +def build_subprocess_env(api_key): + """Return a minimal environment for the AI generation subprocess.""" + env = {name: os.environ[name] for name in FORWARDED_ENV_VARS if name in os.environ} + if api_key: + env["OPENROUTER_API_KEY"] = api_key + return env + def main(): """Command-line interface.""" @@ -124,10 +146,7 @@ Environment Variables: # Execute — pass API key via environment to avoid exposure in process listings try: - env = os.environ.copy() - if api_key: - env["OPENROUTER_API_KEY"] = api_key - result = subprocess.run(cmd, check=False, env=env) + result = subprocess.run(cmd, check=False, env=build_subprocess_env(api_key)) sys.exit(result.returncode) except Exception as e: print(f"Error executing AI generation: {e}") diff --git a/skills/literature-review/scripts/generate_schematic.py b/skills/literature-review/scripts/generate_schematic.py index 43331870..9a7fea02 100644 --- a/skills/literature-review/scripts/generate_schematic.py +++ b/skills/literature-review/scripts/generate_schematic.py @@ -25,6 +25,28 @@ import subprocess import sys from pathlib import Path +# Variables forwarded to the generation subprocess. The child needs the +# OpenRouter credential; the rest keep networking, TLS, and locale working. +# Copying the whole parent environment instead would hand the child every +# unrelated secret that happens to be exported in the calling shell. +FORWARDED_ENV_VARS = ( + "PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "PYTHONPATH", + "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", + "http_proxy", "https_proxy", "no_proxy", + "SSL_CERT_FILE", "SSL_CERT_DIR", "REQUESTS_CA_BUNDLE", "CURL_CA_BUNDLE", + # Windows needs these for sockets, temp files, and interpreter startup. + "SYSTEMROOT", "WINDIR", "COMSPEC", "PATHEXT", + "APPDATA", "LOCALAPPDATA", "USERPROFILE", "TEMP", "TMP", +) + + +def build_subprocess_env(api_key): + """Return a minimal environment for the AI generation subprocess.""" + env = {name: os.environ[name] for name in FORWARDED_ENV_VARS if name in os.environ} + if api_key: + env["OPENROUTER_API_KEY"] = api_key + return env + def main(): """Command-line interface.""" @@ -124,10 +146,7 @@ Environment Variables: # Execute — pass API key via environment to avoid exposure in process listings try: - env = os.environ.copy() - if api_key: - env["OPENROUTER_API_KEY"] = api_key - result = subprocess.run(cmd, check=False, env=env) + result = subprocess.run(cmd, check=False, env=build_subprocess_env(api_key)) sys.exit(result.returncode) except Exception as e: print(f"Error executing AI generation: {e}") diff --git a/skills/scientific-schematics/scripts/generate_schematic.py b/skills/scientific-schematics/scripts/generate_schematic.py index 43331870..9a7fea02 100644 --- a/skills/scientific-schematics/scripts/generate_schematic.py +++ b/skills/scientific-schematics/scripts/generate_schematic.py @@ -25,6 +25,28 @@ import subprocess import sys from pathlib import Path +# Variables forwarded to the generation subprocess. The child needs the +# OpenRouter credential; the rest keep networking, TLS, and locale working. +# Copying the whole parent environment instead would hand the child every +# unrelated secret that happens to be exported in the calling shell. +FORWARDED_ENV_VARS = ( + "PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "PYTHONPATH", + "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", + "http_proxy", "https_proxy", "no_proxy", + "SSL_CERT_FILE", "SSL_CERT_DIR", "REQUESTS_CA_BUNDLE", "CURL_CA_BUNDLE", + # Windows needs these for sockets, temp files, and interpreter startup. + "SYSTEMROOT", "WINDIR", "COMSPEC", "PATHEXT", + "APPDATA", "LOCALAPPDATA", "USERPROFILE", "TEMP", "TMP", +) + + +def build_subprocess_env(api_key): + """Return a minimal environment for the AI generation subprocess.""" + env = {name: os.environ[name] for name in FORWARDED_ENV_VARS if name in os.environ} + if api_key: + env["OPENROUTER_API_KEY"] = api_key + return env + def main(): """Command-line interface.""" @@ -124,10 +146,7 @@ Environment Variables: # Execute — pass API key via environment to avoid exposure in process listings try: - env = os.environ.copy() - if api_key: - env["OPENROUTER_API_KEY"] = api_key - result = subprocess.run(cmd, check=False, env=env) + result = subprocess.run(cmd, check=False, env=build_subprocess_env(api_key)) sys.exit(result.returncode) except Exception as e: print(f"Error executing AI generation: {e}") diff --git a/skills/scientific-slides/scripts/generate_schematic.py b/skills/scientific-slides/scripts/generate_schematic.py index 43331870..9a7fea02 100644 --- a/skills/scientific-slides/scripts/generate_schematic.py +++ b/skills/scientific-slides/scripts/generate_schematic.py @@ -25,6 +25,28 @@ import subprocess import sys from pathlib import Path +# Variables forwarded to the generation subprocess. The child needs the +# OpenRouter credential; the rest keep networking, TLS, and locale working. +# Copying the whole parent environment instead would hand the child every +# unrelated secret that happens to be exported in the calling shell. +FORWARDED_ENV_VARS = ( + "PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "PYTHONPATH", + "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", + "http_proxy", "https_proxy", "no_proxy", + "SSL_CERT_FILE", "SSL_CERT_DIR", "REQUESTS_CA_BUNDLE", "CURL_CA_BUNDLE", + # Windows needs these for sockets, temp files, and interpreter startup. + "SYSTEMROOT", "WINDIR", "COMSPEC", "PATHEXT", + "APPDATA", "LOCALAPPDATA", "USERPROFILE", "TEMP", "TMP", +) + + +def build_subprocess_env(api_key): + """Return a minimal environment for the AI generation subprocess.""" + env = {name: os.environ[name] for name in FORWARDED_ENV_VARS if name in os.environ} + if api_key: + env["OPENROUTER_API_KEY"] = api_key + return env + def main(): """Command-line interface.""" @@ -124,10 +146,7 @@ Environment Variables: # Execute — pass API key via environment to avoid exposure in process listings try: - env = os.environ.copy() - if api_key: - env["OPENROUTER_API_KEY"] = api_key - result = subprocess.run(cmd, check=False, env=env) + result = subprocess.run(cmd, check=False, env=build_subprocess_env(api_key)) sys.exit(result.returncode) except Exception as e: print(f"Error executing AI generation: {e}") diff --git a/skills/scientific-slides/scripts/generate_slide_image.py b/skills/scientific-slides/scripts/generate_slide_image.py index 08f2aa9c..02e1cf5a 100644 --- a/skills/scientific-slides/scripts/generate_slide_image.py +++ b/skills/scientific-slides/scripts/generate_slide_image.py @@ -28,6 +28,28 @@ import subprocess import sys from pathlib import Path +# Variables forwarded to the generation subprocess. The child needs the +# OpenRouter credential; the rest keep networking, TLS, and locale working. +# Copying the whole parent environment instead would hand the child every +# unrelated secret that happens to be exported in the calling shell. +FORWARDED_ENV_VARS = ( + "PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "PYTHONPATH", + "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", + "http_proxy", "https_proxy", "no_proxy", + "SSL_CERT_FILE", "SSL_CERT_DIR", "REQUESTS_CA_BUNDLE", "CURL_CA_BUNDLE", + # Windows needs these for sockets, temp files, and interpreter startup. + "SYSTEMROOT", "WINDIR", "COMSPEC", "PATHEXT", + "APPDATA", "LOCALAPPDATA", "USERPROFILE", "TEMP", "TMP", +) + + +def build_subprocess_env(api_key): + """Return a minimal environment for the AI generation subprocess.""" + env = {name: os.environ[name] for name in FORWARDED_ENV_VARS if name in os.environ} + if api_key: + env["OPENROUTER_API_KEY"] = api_key + return env + def main(): """Command-line interface.""" @@ -126,10 +148,7 @@ Environment Variables: # Execute — pass API key via environment to avoid exposure in process listings try: - env = os.environ.copy() - if api_key: - env["OPENROUTER_API_KEY"] = api_key - result = subprocess.run(cmd, check=False, env=env) + result = subprocess.run(cmd, check=False, env=build_subprocess_env(api_key)) sys.exit(result.returncode) except Exception as e: print(f"Error executing AI generation: {e}")