[ 
https://issues.apache.org/jira/browse/TIKA-5067?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18154040#comment-18154040
 ] 

ASF GitHub Bot commented on TIKA-5067:
--------------------------------------

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.                                                  
                                                                                
                                                                                
      




>  JSON with an HTML tag inside a string value is detected as text/html
> ---------------------------------------------------------------------
>
>                 Key: TIKA-5067
>                 URL: https://issues.apache.org/jira/browse/TIKA-5067
>             Project: Tika
>          Issue Type: Bug
>          Components: detector
>            Reporter: Subbu
>            Priority: Major
>         Attachments: findings.json
>
>
> Tika has no JSON magic or detector. The <html magic matches anywhere in the 
> first 8 KB of text, so a valid JSON document with an HTML tag inside a string 
> value is reported as text/html. This happens even when the file is named 
> *.json
> *Steps to reproduce:* 
> 1) Check attached json 
> 2) Run detect with the attached json. 
> *Actual:* text/html                                                           
>                                                                     
> *Expected:* application/json 
> *Fix options*
> I see two ways to fix this: (A) a content-only JSON magic rule in 
> tika-mimetypes.xml, which is small but can't see the extension and would also 
> turn .txt, .csv and .js files that start like JSON into application/json, or
> (B) a strict JSON check in MimeTypes.detect that overrides only a weak 
> text/html or text/plain verdict and respects the name and Content-Type hints. 
>                                                                               
>                                  
> Which approach do you prefer, or would you suggest another?
>  



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to