Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 39 ยท Best Practices

Notebook Security and Secrets

Keep notebooks safe: never store secrets in cells or outputs, use environment variables, understand trusted notebooks, secure the server with tokens and HTTPS, and scan for leaks.

A Jupyter server runs whatever code you give it, with your permissions. That power is the point of the tool, and also the source of nearly every security problem around it. Notebooks leak passwords because people paste a key into a cell "just for now", servers get exposed to the internet without a password, and a notebook downloaded from a stranger can run code the moment you execute it.

This lesson covers the habits and settings that prevent the common mistakes. Server settings and trust commands are read-only here; the runnable cells include a secret scanner and a safe-configuration pattern.

Threat 1: secrets in notebooks

A notebook stores more than code: it stores outputs, and they end up in Git, in exports and in shared HTML. These all leak secrets:

Python
API_KEY = "sk-live-9f3a..."          # hard-coded in a cell
print(os.environ)                    # prints every secret into the output
requests.get(url, headers={"Authorization": f"Bearer {token}"}).request.headers   # shows the token

Once a secret is committed it is public history, even if you delete the cell later. Treat it as stolen: rotate (revoke and replace) the key, then clean the repository.

Where to keep secrets instead

  • Environment variables, set outside the notebook:
Python
import os
API_KEY = os.environ["API_KEY"]        # fails loudly if missing
  • A .env file that is listed in .gitignore, loaded with python-dotenv (load_dotenv()).
  • A secrets manager (cloud provider's, HashiCorp Vault, 1Password CLI) for shared and production use.
  • getpass.getpass() for a one-time interactive prompt that does not echo or store the input.

Never print a secret, and avoid displaying objects that contain one (a configured client, a request with headers).

Python Scan a notebook's text for likely secrets

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The scanner finds the hard-coded password and the token printed in an output, but not the safe os.environ lookup. Real tools (detect-secrets, gitleaks, trufflehog) have far larger rule sets and run as pre-commit hooks or in CI; scanning outputs as well as source is important for notebooks. Combine with output stripping (see version control for notebooks).

Threat 2: personal and sensitive data

Outputs also contain data: a df.head() of customers shows real names and emails. Before sharing, clear outputs or export only what is needed, and use synthetic or anonymized data in examples. Data protection rules (GDPR and similar) apply to a notebook just as to a database. Also check cell metadata and file paths that reveal usernames.

Threat 3: untrusted notebooks

Notebooks can contain HTML and JavaScript outputs. Jupyter handles this with trust: when you open a notebook you did not save yourself, its stored HTML and JavaScript outputs are not run until you trust it. Code is never run automatically (you press run), but you should read before running.

Terminal
jupyter trust notebook.ipynb         # mark as trusted: only for notebooks you checked

Habits:

  • Read a downloaded notebook before executing it. Look for os.system, subprocess, requests, eval, exec, network calls and strange installs.
  • Run unknown notebooks in a container, VM or disposable cloud environment with no access to your credentials or files.
  • Do not run %pip install lines you did not inspect: a package name can be a typo of a popular library on purpose.

Threat 4: an exposed Jupyter server

A reachable Jupyter server without authentication is a remote shell for anyone who finds it. Defaults protect you: it listens on localhost and requires a token. Problems start when people loosen them.

Terminal
jupyter lab --ip=0.0.0.0 --NotebookApp.token='' --NotebookApp.password=''   # NEVER do this

Instead:

  • Keep the default 127.0.0.1 binding and use an SSH tunnel (ssh -L 8888:localhost:8888 user@server) to reach a remote machine.
  • If you must expose it, set a password (jupyter server password), serve it over HTTPS behind a reverse proxy (nginx, Caddy) and ideally behind your organization's single sign-on.
  • Use --ServerApp.allow_remote_access=False unless you need otherwise; keep the server software updated.
  • On a shared hub (see sharing with JupyterHub) isolate users and limit resources.
  • Do not run Jupyter as root. In containers, run as an unprivileged user.
Python Audit a server configuration (model)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first configuration triggers four warnings, the second (the default-like one) none.

Threat 5: dependencies

Libraries run with the same permissions as your code. Pin versions and install from trusted indexes (see pinning dependencies), use hashes in requirements for important projects, and run a vulnerability scanner such as pip-audit in CI. Keep JupyterLab and extensions updated; extensions run in your browser session with access to the whole app, so install only ones from trusted maintainers.

Security checklist

  • No secrets in cells or outputs; use environment variables or a manager.
  • .env, data and credentials in .gitignore; a secret scanner in pre-commit.
  • Strip or review outputs before sharing; use anonymized data.
  • Inspect unknown notebooks and run them in isolation.
  • Keep the server on localhost; otherwise password, HTTPS, proxy, non-root.
  • Pin and audit dependencies.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You committed an API key in a notebook and then deleted the cell. What must you do?

    Deleting a cell does not remove the secret from earlier commits, so assume it is compromised and replace it.
  2. 2. What is a safer way to use a secret in a notebook?

    Environment variables keep the value out of the file and its outputs.
  3. 3. Why does Jupyter's trust model exist?

    Untrusted notebooks cannot execute their embedded active content until you trust them.
  4. 4. What is the safest way to reach a Jupyter server on a remote machine?

    A tunnel keeps the server bound to localhost while giving you encrypted access.

Summary

  • Notebooks store outputs as well as code, so secrets and personal data can leak from either.
  • Use environment variables or a secrets manager; scan source and outputs; rotate any leaked key.
  • Read unknown notebooks before running and isolate them.
  • Keep servers on localhost, with a token or password, HTTPS and no root; pin and audit dependencies.

Next: Project: A Notebook Linter