Repository navigation
[TIKA-4408] Fix Python env shebang detection - #3473
Merged
Merged
Conversation
Match the complete env Python shebang at file offset zero, with the existing no-space and single-space convention. Add regression coverage for content-only detection, filename hints, and Shell controls. Generated-by: OpenAI Codex
This was referenced Nov 1, 2026
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the Python env-shebang MIME detection fix.
No open issues 🎉
What changed in this PR
Fixes TIKA-4408 by updating Tika’s MIME detection rules so Python scripts using /usr/bin/env python shebangs are recognized as text/x-python rather than shell scripts.
Changes:
- Replaces the incorrect offset-based rule with file-start prefixes supporting no-space and single-space shebang forms.
- Adds 18 regression checks covering Python variants, filename hints, shell controls, and file-start anchoring.
- Documents the fix in the unreleased changelog.
| File | Description |
|---|---|
| tika-core/src/test/java/org/apache/tika/mime/PythonShebangDetectionTest.java | Adds Python shebang detection regression tests. |
| tika-core/src/main/resources/org/apache/tika/mime/tika-mimetypes.xml | Corrects Python env shebang magic rules. |
| CHANGES.txt | Records the TIKA-4408 detection fix. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
TIKA-4408
Python scripts starting with
#!/usr/bin/env pythonor#!/usr/bin/env python3are detected asapplication/x-sh, including when a.pyfilename hint is supplied. The existing magic rule looks for/bin/env pythonat offset 1, where these inputs contain!.Changes
Replace that rule with two literal prefixes anchored at offset 0:
This follows the paired no-space/single-space convention of the neighboring direct-path Python rules and preserves prefix matching for
python,python3, and version suffixes such aspython3.11.Add 18 regression checks covering content-only detection,
.pyfilename hints, direct Python paths, Shell controls, filename-only detection, and file-start anchoring. Add a changelog entry for the unreleased version. No new dependencies or public API changes.General whitespace handling,
envoptions before the interpreter (such as-S), and stricter interpreter-token boundaries are outside this change.Validation
Fresh local checks on macOS with JDK 21, based on Apache
mainat9f8b48425fea3630020924f6ae96e98eabb9ce12:tika-coreunit suite: 1,418 tests reported, 0 failures, 0 errors, 2 skipped.TikaIT: 1 test passed; annotation processor: all 8 tests passed.BUILD SUCCESS../mvnw -B -ntp clean install -pl tika-core -am -Ppedantic \ -Dtika.test.locale=random -Dmaven.repo.local="$PWD/.local_m2_repo"The negative control used the same command with
-Dtest=PythonShebangDetectionTest -Dsurefire.failIfNoSpecifiedTests=false. These checks cover core and its reactor prerequisites; the full repository build has not been run.AI assistance
OpenAI Codex assisted with investigation, implementation, testing, and PR preparation. The commit includes a
Generated-bytrailer.