kcov icon indicating copy to clipboard operation
kcov copied to clipboard

Generated files with `.json` extension do not conform to the JSON file format

Open chradcliffe opened this issue 7 years ago • 5 comments

The .json files generated by kcov contain variable definitions and therefore are not well-formed JSON files. These files are included in <script> tags as content-type "text/javascript" and so are possibly not meant to be pure JSON.

When used with a web server that sets X-Content-Type-Options: nosniff and sends files with the .json extension with content-type application/json, modern browsers will refuse to execute the contents of the .json file and the kcov report will not render properly.

I can see two possible resolutions to this issue:

  1. Rename generated .json files to .js. This is probably the least-effort solution.
  2. Modify the .json files to be proper JSON objects and reference members of a json object instead of the individual variables set inside the .json files.

I can submit a PR once the right solution is decided upon.

chradcliffe avatar Jul 20 '17 17:07 chradcliffe

Here is a Python web server script that sets the X-Content-Type-Options: nosniff header:

#!/usr/bin/env python3

from http.server import HTTPServer, SimpleHTTPRequestHandler

class Handler(SimpleHTTPRequestHandler):
    def do_GET(self):
        self.send_header("X-Content-Type-Options", "nosniff")
        super().do_GET()

server_address = ('', 8000)
httpd = HTTPServer(server_address, Handler)
httpd.serve_forever()

If you run this script with the current working directory as the root of the kcov output tree, the served pages will fail to render properly under at least: Chrome 59.0.3071.115 (Windows), IE7 (Windows), and MS Edge (Windows).

chradcliffe avatar Jul 20 '17 17:07 chradcliffe

I've been away on computer-less vacation for a while, so sorry for not responding.

Anyway, thanks for the report - I guess it shows I'm not really a JavaScript and JSON newbie, but a JS/JSON infant! I don't actually remember why the variables were introduced in the output, but at least the "Not really json, but anyway" comment shows that I at least was aware of it being a problem.

Solution 2 sounds more clean (being less web-browser centric), or is there some additional advantage of having it in JS format?

SimonKagstrom avatar Jul 22 '17 18:07 SimonKagstrom

I don't know about advantages of having a js format, but I was hoping to manipulate the json (with jq or something else) to ignore lines that should always have 0 hits (for example looking for WRONG macros in the Varnish code base, or unreachable! macros in Rust).

Also, I see that having a post-process step wouldn't be accurate anyway since covered lines are also pre-computed in several places. I may have a look at implementing that in kcov itself once I manage to build it again (if that's welcome, similar to lcov's lcov_excl_line).

dridi avatar Aug 11 '17 15:08 dridi

kcov actually supports LCOV_EXCL_LINE markers in master, or other lines via the --exclude-line=PATTERN command line option.

SimonKagstrom avatar Aug 11 '17 16:08 SimonKagstrom

That's good news, I'm looking forward to kcov 34 then, hopefully it will also work with binutils 2.29 (#215). This is off-topic here though, sorry for the noise.

dridi avatar Aug 11 '17 16:08 dridi