Skip to content

Commit 1c2a256

Browse files
committed
Add x-xsrftoken to Access-Control-Allow-Headers
When starting a kernel using the Jupyter Notebook Kernel API, web browsers will automatically check for the presence of `x-xsrftoken` in the Access-Control-Allow-Headers during the preflight CORS check ([ref][ref]). [ref]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers Since we didn't allow this header before, web browsers would fail the preflight check even when the x-xsrftoken header isn't being used by the notebook server. This meant that running a webpage on localhost:8080 that used Javascript to start a kernel on a notebook server running on localhost:8888 would fail. How I tested this commit: 1. Start a notebook server using jupyter notebook --no-browser --NotebookApp.allow_origin="*" --NotebookApp.disable_check_xsrf=True --NotebookApp.token='' 2. Build the [web3](https://github.com/jupyter-widgets/ipywidgets/tree/master/examples/web3) example from ipywidgets. 3. In that directory, run `npm run host`. 4. Verify that visiting http://localhost:8080/ starts a kernel in the notebook server.
1 parent b884ff9 commit 1c2a256

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

notebook/base/handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ def finish(self, *args, **kwargs):
503503
return super(APIHandler, self).finish(*args, **kwargs)
504504

505505
def options(self, *args, **kwargs):
506-
self.set_header('Access-Control-Allow-Headers', 'accept, content-type, authorization')
506+
self.set_header('Access-Control-Allow-Headers',
507+
'accept, content-type, authorization, x-xsrftoken')
507508
self.set_header('Access-Control-Allow-Methods',
508509
'GET, PUT, POST, PATCH, DELETE, OPTIONS')
509510
self.finish()

0 commit comments

Comments
 (0)