Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser content display blocked by MIME type mismatch #3077

Closed
amrosado opened this issue Dec 26, 2019 · 5 comments
Closed

Browser content display blocked by MIME type mismatch #3077

amrosado opened this issue Dec 26, 2019 · 5 comments

Comments

@amrosado
Copy link

amrosado commented Dec 26, 2019

Issue description

For anyone who is interested, I had a problem running tensorboard in Windows preventing anything from displaying in every browser except for internet explorer. The error I received in Firefox Nightly is the following:

The resource from “http://localhost:6006/index.js” was blocked due to MIME type (“text/plain”) mismatch (X-Content-Type-Options: nosniff).

Tensorboard provides files from a prepared zip file (webfiles.zip), but these files were being provided with the 'text/plain' content-type header preventing browsers from using the javascript correctly. The ways to fix this is to go to the following file: tensoboard/backend/http_util.py and change to the following:

path_ext_split = request.path.split('.')
  if path_ext_split[-1] == 'js':
    content_type = 'application/javascript'
      
  if textual and not charset_match and mimetype not in _JSON_MIMETYPES:
    content_type += '; charset=' + charset

You could also disable nosniff by commenting out the following:

headers.append(('X-Content-Type-Options', 'nosniff'))

to

#headers.append(('X-Content-Type-Options', 'nosniff'))

I believe this might be similar to another issue referencing the projector.

The diagnose_tensorboard.py information is not useful. For this particular project, I'm attempting to use tensorboard for a pytorch project.

Environment information (required)

Diagnostics output
--- check: autoidentify
INFO: diagnose_tensorboard.py version d515ab103e2b1cfcea2b096187741a0eeb8822ef

--- check: general
INFO: sys.version_info: sys.version_info(major=3, minor=8, micro=0, releaselevel='final', serial=0)
INFO: os.name: nt
INFO: os.uname(): N/A
INFO: sys.getwindowsversion(): sys.getwindowsversion(major=10, minor=0, build=18362, platform=2, service_pack='')

--- check: package_management
INFO: has conda-meta: False
INFO: $VIRTUAL_ENV: None

--- check: installed_packages
WARNING: no installation among: ['tb-nightly', 'tensorboard', 'tensorflow-tensorboard']
WARNING: no installation among: ['tensorflow', 'tensorflow-gpu', 'tf-nightly', 'tf-nightly-2.0-preview', 'tf-nightly-gpu', 'tf-nightly-gpu-2.0-preview']
WARNING: no installation among: ['tensorflow-estimator', 'tensorflow-estimator-2.0-preview', 'tf-estimator-nightly']

--- check: tensorboard_python_version
Traceback (most recent call last):
  File "C:\Users\arosado\GitClone\tensorboard\tensorboard\tools\diagnose_tensorboard.py", line 470, in main
    suggestions.extend(check())
  File "C:\Users\arosado\GitClone\tensorboard\tensorboard\tools\diagnose_tensorboard.py", line 78, in wrapper
    result = fn()
  File "C:\Users\arosado\GitClone\tensorboard\tensorboard\tools\diagnose_tensorboard.py", line 246, in tensorboard_python_version
    from tensorboard import version
ModuleNotFoundError: No module named 'tensorboard'

--- check: tensorflow_python_version
Traceback (most recent call last):
  File "C:\Users\arosado\GitClone\tensorboard\tensorboard\tools\diagnose_tensorboard.py", line 470, in main
    suggestions.extend(check())
  File "C:\Users\arosado\GitClone\tensorboard\tensorboard\tools\diagnose_tensorboard.py", line 78, in wrapper
    result = fn()
  File "C:\Users\arosado\GitClone\tensorboard\tensorboard\tools\diagnose_tensorboard.py", line 253, in tensorflow_python_version
    import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'

--- check: tensorboard_binary_path
INFO: which tensorboard: b'C:\\ProgramData\\Anaconda3\\Scripts\\tensorboard.exe\r\n'

--- check: addrinfos
socket.has_ipv6 = True
socket.AF_UNSPEC = <AddressFamily.AF_UNSPEC: 0>
socket.SOCK_STREAM = <SocketKind.SOCK_STREAM: 1>
socket.AI_ADDRCONFIG = <AddressInfo.AI_ADDRCONFIG: 1024>
socket.AI_PASSIVE = <AddressInfo.AI_PASSIVE: 1>
Loopback flags: <AddressInfo.AI_ADDRCONFIG: 1024>
Loopback infos: [(<AddressFamily.AF_INET6: 23>, <SocketKind.SOCK_STREAM: 1>, 0, '', ('::1', 0, 0, 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 0, '', ('127.0.0.1', 0))]
Wildcard flags: <AddressInfo.AI_PASSIVE: 1>
Wildcard infos: [(<AddressFamily.AF_INET6: 23>, <SocketKind.SOCK_STREAM: 1>, 0, '', ('::', 0, 0, 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 0, '', ('0.0.0.0', 0))]

--- check: readable_fqdn
INFO: socket.getfqdn(): 'DESKTOP-CLRUTHR'

--- check: stat_tensorboardinfo
INFO: directory: C:\Users\arosado\AppData\Local\Temp\.tensorboard-info
INFO: os.stat(...): os.stat_result(st_mode=16895, st_ino=1970324838089767, st_dev=2625741768, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1577322690, st_mtime=1577322690, st_ctime=1577222989)
INFO: mode: 0o40777

--- check: source_trees_without_genfiles
INFO: tensorboard_roots (0): []; bad_roots (0): []

--- check: full_pip_freeze
INFO: pip freeze --all:
pip==19.3.1
setuptools==41.6.0
virtualenv==16.7.7
virtualenvwrapper-win==1.2.5

@wchargin
Copy link
Contributor

Hi @amrosado! Thanks for the report. The content type is inferred from
the file name
, defaulting to application/octet-stream, so it’s
surprising that you’d see text/plain from a .js file. I can’t
reproduce this on my Linux machine; I see application/javascript as
the response Content-Type, as expected.

The X-Content-Type-Options: nosniff header is a security precaution,
so we can’t remove that. The if path_ext_split[-1] == "js" check
should be handled by the MIME type detection logic instead. If your
system isn’t reporting MIME types accurately, then this would presumably
break for other extensions, too. We don’t want to special-case all of
them if we can avoid it, so let’s instead try to find the root cause of
the problem.

Could you tell us the output of

python -c "import mimetypes; print(list(mimetypes.guess_type('index.js')))"

on your machine, and also please confirm what the Content-Type header
is on a response from http://localhost:6006/index.js? You can find the
response headers in the Firefox dev tools as described here:
https://developer.mozilla.org/en-US/docs/Tools/Network_Monitor/request_details

Also, please do provide the diagnose_tensorboard.py output. It may not
seem useful to you, but clearly this issue depends on your environment,
so cross-referencing the distinguishing factors among you and other
people experiencing this problem (if any) can in fact be critical to
debugging the root cause of issues like this. That’s why we ask for it
in the first place. :-)

@amrosado
Copy link
Author

amrosado commented Dec 27, 2019

My diagnose_tensorboard.py output is included in the first post like asked for. I don't think I have it working correctly because it doesn't find my installed tensorboard module for the virtualenv I'm using.

My headers for index.js without changing the html_util.py:

Response:

{"Response Headers (270 B)":{"headers":[{"name":"Cache-Control","value":"no-cache, must-revalidate"},{"name":"Content-Encoding","value":"gzip"},{"name":"Content-Length","value":"889767"},{"name":"Content-Type","value":"text/plain; charset=utf-8"},{"name":"Date","value":"Fri, 27 Dec 2019 13:14:49 GMT"},{"name":"Expires","value":"0"},{"name":"Server","value":"Werkzeug/0.16.0 Python/3.7.4"},{"name":"X-Content-Type-Options","value":"nosniff"}]},"Request Headers (540 B)":{"headers":[{"name":"Accept","value":"*/*"},{"name":"Accept-Encoding","value":"gzip, deflate"},{"name":"Accept-Language","value":"en-US,en;q=0.5"},{"name":"Connection","value":"keep-alive"},{"name":"Cookie","value":"username-localhost-8888=\"2|1:0|10:1576713253|23:username-localhost-8888|44:ZWZmOGIyYjJlNmM0NGNiNGE2MjE2MjRhMWE1MzNkMGU=|40af61406c75409c0823325ce93be8e1aedde846367a0e8493fae9bf290a4242\"; _xsrf=2|6a09fe0e|c8c5afcdaf7ecdb6712b65b2a2d6d73c|1576523211"},{"name":"DNT","value":"1"},{"name":"Host","value":"localhost:6006"},{"name":"Referer","value":"http://localhost:6006/"},{"name":"User-Agent","value":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0"}]}}

Request:

{"Request Headers (540 B)":{"RAW_HEADERS_ID":"GET /index.js HTTP/1.1\r\nHost: localhost:6006\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0\r\nAccept: */*\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nDNT: 1\r\nConnection: keep-alive\r\nReferer: http://localhost:6006/\r\nCookie: username-localhost-8888=\"2|1:0|10:1576713253|23:username-localhost-8888|44:ZWZmOGIyYjJlNmM0NGNiNGE2MjE2MjRhMWE1MzNkMGU=|40af61406c75409c0823325ce93be8e1aedde846367a0e8493fae9bf290a4242\"; _xsrf=2|6a09fe0e|c8c5afcdaf7ecdb6712b65b2a2d6d73c|1576523211\r\n\r\n"}}

The output of ``` python -c "import mimetypes; print(list(mimetypes.guess_type('index.js')))" ````:

['text/plain', None]

How does mimetypes determine the mimetype? It doesn't throw an exception even with an invalid url.

@wchargin
Copy link
Contributor

Great, thanks.

How does mimetypes determine the mimetype? It doesn't throw an
exception even with an invalid url.

It guesses based on the filename given a pre-defined list of MIME types
and extensions, not the file contents (as a magic database would).

The ['text/plain', None] result from mimetypes.guess_type indicates
that your setup of Python or Windows is broken. On Windows, mimetypes
reads from the (globally writable) registry, so it’s seems likely that
another program has written a bad entry to the registry. Indeed, while
I can’t repro this on a normal Windows install, if I open regedit and
navigate to HKEY_CLASSES_ROOT\.js and create a new string value
(REG_SZ) with name Content Type and value application/x-test-123,
then I can reproduce the problem:

Screenshot of regedit

The fact that the mimetypes module from the Python standard library
gives the wrong result suggests that this isn’t really a problem with
TensorBoard proper. You may be interested in bpo-10551, which
complains about this behavior but was marked as wontfix. I’d suggest
fixing your corrupted registry and trying again; if you can’t track down
the root cause, perhaps try asking on a community of Windows users (we
don’t have much expertise there).

Good luck! :-)

@amrosado
Copy link
Author

Thanks for the explanation. I have this same problem on two different windows installations. After changing the value in my registry tensorboard works like expected.

@wchargin
Copy link
Contributor

Great; glad to hear it, and thanks for letting us know! Sounds like some
program installed on those machines is writing bad values to the
registry. Some related discussions:

At least one user is claiming that the errant application is Visual
Studio, which would be quite unfortunate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants