subbudvk opened a new pull request, #3705: URL: https://github.com/apache/tika/pull/3705
## Problem
Tika has no JSON magic. The low priority `<html` magic matches anywhere in
the
first 8 KB, so a valid JSON document with an HTML tag inside a string
value is
detected as `text/html`, even when it is named `*.json` or sent as
`application/json`. `applyHint` does not help, because `application/json`
is not
a specialization of `text/html`.
Callers that compare the declared type with the detected type then reject
the
file as a mismatch. Whether a file fails depends on whether its first 8 KB
happens to contain such a tag.
Example (`findings.json`):
{"export_id": "a1b2c3", "findings": [{"severity": "info",
"proof": "HTTP/1.1 200 OK\r\n\r\n<html lang=\"en\"><body>It
works</body></html>"}]}
tika-app --detect findings.json
before: text/html
after: application/json
## Change
In `MimeTypes.detect`, when all of these hold, the JSON hint wins:
- the detected type is `text/html`
- the resource name or Content-Type hint is `application/json` or a
subtype
- the content opens with `{` or `[`, after an optional UTF-8 BOM and
whitespace
Real HTML opens with `<`, so it is unaffected. Other detected types, and
files
without a JSON hint, are unchanged. There is no change to
`tika-mimetypes.xml`
or `applyHint`.
## Testing
- `MimeDetectionTest.testJsonWithEmbeddedHtml`: nested and pretty-printed
JSON,
a tag in the first 128 bytes, a BOM, and leading whitespace, detected by
name,
by Content-Type, and by both.
- `MimeDetectionTest.testJsonHintDoesNotOverrideOtherContent`: no JSON
hint,
`.txt`, `.html`, Content-Type `text/html`, real HTML named `.json`, and
a PDF
named `.json` are unchanged.
- `tika-core` tests pass.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
