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.
This commit is contained in:
Timothy Kassis committed 2026-07-25 20:19:57 -07:00
1 parent 665663cac1
commit f5a0bbcf6c
11 files changed
+211 -32

No files matched your search

+1
View File
@@ -15,6 +15,7 @@ research/
.agents/
AGENTS.md
CLAUDE.md
skills-lock.json
+34
View File
@@ -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:
@@ -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'
@@ -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}")
+11 -2
View File
@@ -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})")
@@ -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}")
@@ -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}")
@@ -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}")
@@ -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}")
@@ -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}")
@@ -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}")