Summary
Koha Library before version 23.05.10 contains a command injection vulnerability in the cover image upload workflow, where user-controlled filenames are passed directly into a shell command for unzipping.
An attacker who can upload a specially crafted ZIP file and trigger the "Process Images" action may execute arbitrary commands on the server, leading to full remote code execution.
Affected context
This vulnerability appears in Koha Library deployments that allow users (such as librarians or authenticated staff roles) to upload ZIP archives of cover images through the web interface.
The risk is present where:
- Uploaded filenames are derived from user-supplied data without strong validation or normalization
- The application invokes shell utilities (e.g.,
unzip) by constructing command strings rather than using safer APIs - The web server or application user account has significant privileges or access to backend systems
The vulnerable code path specifically uses the filename in a shell command to process images in upload-cover-image.pl.
Exploit preconditions
- Attack surface: Web interface endpoint for uploading and processing cover image ZIP files.
- Required access: Ability to authenticate and upload a ZIP file (often a librarian/staff role; exact role depends on deployment configuration).
- User interaction: The attacker or a privileged user must click "Process Images" (or equivalent action) to trigger the vulnerable code path.
- Trigger point: The construction and execution of the
unzipcommand inupload-cover-image.pl, which interpolates the user-controlled filename intoqx/unzip $filename -d $dirname/;. - Why exploitation is possible: The filename is not sanitized or safely escaped, so shell metacharacters in the filename are interpreted by the shell, allowing arbitrary commands to be injected and executed under the web server's privilege context.
Why this happens
The core issue is the direct interpolation of untrusted input into a shell command, a classic command injection pattern.
In this case, the script uses Perl's backtick-like qx// operator to run unzip $filename -d $dirname/, where $filename is derived from user-controlled data.
Because filenames are not constrained to a safe character set and are passed as part of a single command string, any embedded shell metacharacters (such as ;, &&, or backticks) are interpreted by the shell.
This maps to CWE-94: Improper Control of Generation of Code ('Code Injection') in the sense that attacker-controlled input influences executable logic, allowing arbitrary commands to be run.
Conceptually, the bug is an unsafe coupling of input handling and command execution, rather than using parameterized process execution or library-level ZIP handling.
Potential impact
- Execution of arbitrary system commands under the web server or application user account
- Unauthorized access to sensitive data on the Koha server (configuration files, databases, logs)
- Modification or deletion of library records, cover images, or other application data
- Installation of backdoors or malware, enabling long-term compromise of the system
- Lateral movement within the network if the compromised server has additional access
- Service disruption or complete denial of service by altering critical files or processes
General mitigation guidance
Mitigation generally focuses on eliminating direct command construction from untrusted input and reducing the privileges of the executing context.
Recommended defensive patterns include:
- Avoid shell command composition: Use language-native libraries or APIs (e.g., Perl modules for ZIP extraction) that do not invoke a shell, so user input cannot affect command parsing.
- Strict input validation: Constrain filenames to a safe, minimal character set and normalize paths, rejecting or sanitizing unexpected characters and patterns (including metacharacters and path traversal sequences).
- Argument-based process execution: When external tools are unavoidable, use APIs that pass arguments as separate parameters rather than a single shell-processed string, ensuring the input is not parsed by a shell.
- Least privilege execution: Run the web application and related processes under an account with minimal file system and system privileges, limiting the blast radius if command execution occurs.
- Defense-in-depth controls: Apply file system restrictions (e.g., chroot or containerization, restricted directories) and logging/monitoring around file upload and processing operations.
How teams detect issues like this
Effective detection typically combines code-level analysis with runtime monitoring:
- Static analysis and linting: Configure SAST tools or custom rules to flag:
- Use of
qx//, backticks, orsystem()calls that concatenate variables into command strings. - Any invocation of shell utilities (e.g.,
unzip,tar,sh,bash) where user input influences the command text. - Manual code review signals: During review, treat any call that:
- Executes external programs.
- Uses interpolated variables in command strings.
- Processes data derived from request parameters or uploaded files.
as high-risk, and confirm safe argument handling and validation.
- Policy enforcement: Enforce development policies requiring safe wrappers around external command execution, or prohibiting raw shell invocations in request-handling code.
- Runtime monitoring: Employ logging and intrusion detection to alert on unusual command executions (e.g.,
unzipfollowed by unexpected shell utilities) or anomalous child processes spawned by the web server.

