jupyter_server icon indicating copy to clipboard operation
jupyter_server copied to clipboard

`metrics/metrics.py` file (and other files prefixed with "metrics") cannot be retrieved via contents API

Open krassowski opened this issue 2 years ago • 6 comments

Description

Following the reproduction instructions from https://github.com/jupyterlab/jupyterlab/issues/10859 (plus adding __init__.py) I narrowed down the issue to the following API call failure:

# does NOT work
http://localhost:8891/api/contents/metrics/metrics.py?format=text&type=file&content=1&1629221227894

whereas the actual metrics are retried from a very similar endpoint:

http://localhost:8891/api/contents/metrics?content=1&1629222005371

Renaming the file helps:

# works ok
http://localhost:8891/api/contents/metrics/test.py?format=text&type=file&content=1&1629221227894

as long as it does not start with metrics prefix:

# does NOT work
http://localhost:8891/api/contents/metrics/metrics_test.txt?format=text&type=file&content=1&1629221227894

Reproduce

See https://github.com/jupyterlab/jupyterlab/issues/10859 (but also add __init__.py).

Expected behavior

Files contents can be retrieved, regardless of how the file is named.

Context

  • Operating System and version: Ubuntu
  • Jupyter Server version: 1.10.2 (also tested with 1.8)

krassowski avatar Aug 17 '21 17:08 krassowski

I cannot reproduce this:

conda create -n dbg jupyter_server -y
conda activate dbg
cd <random empty dir>
mkdir metrics
< make empty __init__.py and metrics.py files >
jupyter server
< open http://localhost:<port>/api/contents/metrics/metrics.py?format=text&type=file&content=1&1629221227894 >

Returns:

{"name": "metrics.py", "path": "metrics/metrics.py", "last_modified": "2021-08-24T16:28:46.203385Z", "created": "2021-08-24T16:28:54.222115Z", "content": "", "format": "text", "mimetype": "text/x-python", "size": 0, "writable": true, "type": "file"}

vidartf avatar Aug 24 '21 16:08 vidartf

Thank you for looking into this. Indeed, it works well if I just open that address, but the request fails in JupyterLab - this is odd, Could it be some other headers that need to be present to reproduce it? Here is the exact request the lab is sending (I used Google Developer tools → network tab → copy as cURL):

curl 'http://localhost:8891/api/contents/metrics/metrics.py?format=text&type=file&content=1&1629823907904' \
  -H 'sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"' \
  -H 'Referer: http://localhost:8891/lab/tree/metrics' \
  -H 'DNT: 1' \
  -H 'X-XSRFToken: REDACTED' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36' \
  -H 'Content-Type: application/json' \
  --compressed

and I can confirm that this one triggers the issue. Is the issue on JupyterLab side (wrong headers), or on jupyter-server side?

krassowski avatar Aug 24 '21 16:08 krassowski

Whoa, the headers sent to metrics.py (and other files prefixed by metrics) differ a lot from headers sent to any other file in metrics/.

curl

metrics.py:

curl 'http://localhost:8891/api/contents/metrics/metrics.py?format=text&type=file&content=1&1629823907904' \
  -H 'sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"' \
  -H 'Referer: http://localhost:8891/lab/tree/metrics' \
  -H 'DNT: 1' \
  -H 'X-XSRFToken: REDACTED' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36' \
  -H 'Content-Type: application/json' \
  --compressed

Any other file:

curl 'http://localhost:8891/api/contents/metrics/saa.py?format=text&type=file&content=1&1629824927034' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"' \
  -H 'DNT: 1' \
  -H 'X-XSRFToken: REDACTED' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'User-Agent: XX' \
  -H 'Content-Type: application/json' \
  -H 'Accept: */*' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Referer: http://localhost:8891/lab/tree/metrics' \
  -H 'Accept-Language: en-GB,en-US;q=0.9,en;q=0.8,pl;q=0.7' \
  -H 'Cookie: _xsrf VERY LONG STRING"' \
  --compressed

fetch

matrics.py

fetch("http://localhost:8891/api/contents/metrics/metrics.py?format=text&type=file&content=1&1629825084904", {
  "headers": {
    "content-type": "application/json",
    "sec-ch-ua": "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\"",
    "sec-ch-ua-mobile": "?0",
    "x-xsrftoken": "REDACTED"
  },
  "referrer": "http://localhost:8891/lab/tree/metrics/saa.py",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": null,
  "method": "GET",
  "mode": "cors",
  "credentials": "omit"
});

any other file:

fetch("http://localhost:8891/api/contents/metrics/saa.py?content=0&1629824927057", {
  "headers": {
    "accept": "*/*",
    "accept-language": "en-GB,en-US;q=0.9,en;q=0.8,pl;q=0.7",
    "cache-control": "no-cache",
    "content-type": "application/json",
    "pragma": "no-cache",
    "sec-ch-ua": "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\"",
    "sec-ch-ua-mobile": "?0",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "x-xsrftoken": "REDACTED"
  },
  "referrer": "http://localhost:8891/lab/tree/metrics/saa.py",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": null,
  "method": "GET",
  "mode": "cors",
  "credentials": "include"
});

krassowski avatar Aug 24 '21 17:08 krassowski

I can reproduce this via lab (but not via the endpoint directly). It also reproduces for any file prefixed with metrics (but not metric!).

kevin-bates avatar Aug 24 '21 17:08 kevin-bates

The thing that bugs me is that metrics appears exactly zero times in the code of JupyterLab frontend: https://github.com/jupyterlab/jupyterlab/search?q=metrics (it does show up once in the docs which is not relevant).

krassowski avatar Aug 24 '21 17:08 krassowski

Could it be possible that the PrometheusMetricsHandler is interfering somehow? https://github.com/jupyter-server/jupyter_server/blob/b908f5518cadcf9b5c0a892f749c8c30b9b3ded2/jupyter_server/base/handlers.py#L972-L977

kevin-bates avatar Aug 24 '21 17:08 kevin-bates