Summary
Gammapy evaluates the WCSSHAPE FITS header value in WcsGeom.from_header(), which can allow a crafted FITS file to execute Python code. The risk arises when a user loads a file containing a malicious header through Map.read(), causing the code to run with that user's permissions.
Affected context
The issue occurs in Gammapy's FITS file handling when WcsGeom.from_header() processes the WCSSHAPE header card using eval(). It may affect workflows that load FITS files through Map.read() when an attacker can influence the file being opened.
Exploit preconditions
- Attack surface: FITS file ingestion through Gammapy's map-reading functionality
- Required access: Ability to provide or influence a FITS file that a user will load
- Trigger point: Processing of the
WCSSHAPEheader byWcsGeom.from_header()duringMap.read() - Why exploitation is possible: Header content is evaluated as Python code rather than handled strictly as data
Why this happens
The underlying bug is the use of eval() on a value taken from a FITS header. Because the header can be influenced through the input file, treating its contents as executable Python creates a code-injection weakness. This maps to CWE-95: Improper Neutralization of Directives in Dynamically Evaluated Code.
Potential impact
- Arbitrary code could run with the permissions of the user loading the file.
- An attacker may be able to access or modify data available to that user.
- The affected process or environment could be compromised.
- Malicious code could disrupt analysis workflows or access local resources.
General mitigation guidance
Defensive patterns focus on preventing file metadata from being interpreted as code:
- Replace dynamic evaluation with explicit parsing of the expected header format.
- Validate parsed values against a strict schema, including type, range, and structure checks.
- Treat externally supplied FITS files and their metadata as untrusted.
- Load and process files in isolated environments with least-privilege permissions.
- Apply resource and filesystem restrictions to limit the impact of unexpected input.
How teams detect issues like this
- Use static analysis to identify
eval()and similar dynamic execution functions applied to file contents or metadata. - Review parsing code for paths that pass FITS header values into interpreters or general-purpose expression evaluators.
- Add automated checks that reject unsafe evaluation patterns in code review and build policies.
- Test file parsers with malformed and unexpected metadata, verifying that inputs are treated only as data.

