Reader small image

You're reading from  Jupyter Cookbook

Product typeBook
Published inApr 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788839440
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Dan Toomey
Dan Toomey
author image
Dan Toomey

Dan Toomey has been developing application software for over 20 years. He has worked in a variety of industries and companies, in roles from sole contributor to VP/CTO-level. For the last few years, he has been contracting for companies in the eastern Massachusetts area. Dan has been contracting under Dan Toomey Software Corp. Dan has also written R for Data Science, Jupyter for Data Sciences, and the Jupyter Cookbook, all with Packt.
Read more about Dan Toomey

Right arrow

Chapter 10. Jupyter Security

In this chapter, we will cover the following recipes:

  • Security mechanisms built into Jupyter
  • Using SSL
  • The Jupyter trust model
  • Controlling network access
  • Additional practices

Introduction


Web security is concerned with assets or information that you have exposed on the internet via a web application.

In this chapter, we investigate the various security mechanisms available for our Jupyter Notebook.

How much risk?

An application would need more security if critical or personal information were used on it; for example, credit cards. At the other end of the spectrum would be a site that is only providing information that is generally known.

In the case of a Jupyter application, you have to make that decision. Is the application or data being exposed of high importance to your company or project? There are many Jupyter applications (or web applications in general) that do not require a high degree of security as the information/algorithms being used are generally known.

Known vulnerabilities

Many web applications are built upon a well-known framework that has been in use for some time. As such, these frameworks have already worked through the known vulnerabilities they...

Security mechanisms built into Jupyter


Jupyter has a variety of security mechanisms available depending on your needs, which we'll discuss in the following points.

How to do it...

Authentication is the process of proving that the user is as originally presented.

Jupyter can use:

  • Token-based authentication
  • Password authentication
  • No authentication

Current versions of Jupyter use token-based authentication by default. If you enable password protection for your application (the typical username and password that you have seen many times), then token-based authentication is disabled.

Token-based authentication

Token-based authentication is where a token is exchanged for all of a user's requests and it must be present in order for any user request to proceed into your application. For example:

  • User K connects to your application
  • The response from the application has a built-in token that is generated automatically and passed using web headers in the response
  • As the application is running in such a web server...

Using SSL


Along the same lines, if you were to determine that the contents of your Notebook are valuable, you may want to use SSL to encrypt all transmissions between your Notebook. At a minimum, then, any authentication information will be encrypted as well and this will prevent hijacking.

How to do it...

A well-known service for providing free SSL certificates is Let's Encrypt (https://letsencrypt.org/). Let's take a look at how to create and apply an SSL certificate in the following sections.

Creating an SSL certificate

You can create a certificate using openssl with this command:

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mykey.key -out mycert.pem

Where the several options specified are as follows:

  • -x509: We are using x509 protocol
  • -days 365: Good for one year
  • -newkey: We are generating a new key
  • rsa:2048: Use the 2,048-bit RSA algorithm
  • -keyout: Location to place the key
  • -out: Location to place the certificate

Apply the SSL certificate

You can set the SSL certificate to be used by...

The Jupyter trust model


Jupyter has specific parts of the application that are trusted or not:

  • Untrusted HTML is always sanitized
  • Untrusted JavaScript is never executed
  • HTML and JavaScript in Markdown cells are never trusted
  • Outputs generated by the user are trusted
  • Any other HTML or JavaScript (in Markdown cells or output generated by others) is never trusted

Sanitized, untrusted coding is crippled by not allowing access to resources, such as accessing the internet. This can be a problem as many applications would naturally store JavaScript and/or actionable CSS in cells that are not visible to the user but would be crippled as part of the trust model.

Jupyter develops trust for an application by comparing digital signatures. When a Notebook is stored, a digital signature is made using the contents of the Notebook and a secret. The digital signature is stored on a disk accessible by the server. Then, whenever a Notebook is accessed, the signature is regenerated and compared to the stored value...

Controlling network access


A Jupyter Notebook can control what domains can originate requests to Jupyter and/or what IP addresses can access the Notebook.

By default, notebooks allow for localhost access to a Notebook.

How to do it...

It typically means only you can access your Notebook on your machine. This is enforced with the following parameters in the configuration file:

c.NotebookApp.allow_origin = ''
c.NotebookApp.ip = 'localhost'

Controlling domain access

You can open access to users on other domains by adjusting the allow_origin setting, such as:

c.NotebookApp.allow_origin = yourdomain.com'

This gives all users in the domain access to your Notebook.

Controlling IP access

Alternatively, and conjunctively, you can control which IP addresses can access your Notebook by setting the ip value in the configuration. Using 0.0.0.0 allows all users to access your Notebook like this:

c.NotebookApp.ip = '0.0.0.0'

Additional practices


There are several miscellaneous steps that can be taken to further the security of your system.

How to do it...

Let us take a look at some of the ways to enhance the security of our system in the following sections.

Server IP address

You can specify the IP address to be used by your Notebook rather than using the default. Many hacking scenarios count on you using default values, such as the port, to quickly acquire targets.

The IP address used by the Notebook can be changed with this configuration command:

c.NotebookApp.port = 9732

Note that once you determine the port for Jupyter, you need to apply the filter to your firewall so that communication to the Notebook will get through.

URL prefix

The default installation for Jupyter runs at http://localhost:8888. While this is convenient for individual use, it can cause problems where other applications may be running at the same port at the server root address. One more tool available is to apply a prefix to the Notebook url using...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Jupyter Cookbook
Published in: Apr 2018Publisher: PacktISBN-13: 9781788839440
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Dan Toomey

Dan Toomey has been developing application software for over 20 years. He has worked in a variety of industries and companies, in roles from sole contributor to VP/CTO-level. For the last few years, he has been contracting for companies in the eastern Massachusetts area. Dan has been contracting under Dan Toomey Software Corp. Dan has also written R for Data Science, Jupyter for Data Sciences, and the Jupyter Cookbook, all with Packt.
Read more about Dan Toomey