|
| 1 | +.. _security_considerations: Security Considerations |
| 2 | + |
| 3 | +======================= |
| 4 | +Security Considerations |
| 5 | +======================= |
| 6 | + |
| 7 | +RDFLib is designed to access arbitrary network and file resources, in some cases |
| 8 | +these are directly requested resources, in other cases they are indirectly |
| 9 | +referenced resources. |
| 10 | + |
| 11 | +An example of where indirect resources are access is JSON-LD processing, where |
| 12 | +network or file resources referenced by ``@context`` values will be loaded and |
| 13 | +processed. |
| 14 | + |
| 15 | +RDFLib also supports SPARQL, which has federated query capabilities that allow |
| 16 | +queries to query arbitrary remote endpoints. |
| 17 | + |
| 18 | +If you are using RDFLib to process untrusted documents or queries you should |
| 19 | +take measures to restrict file and network access. |
| 20 | + |
| 21 | +Some measures that can be taken to restrict file and network access are: |
| 22 | + |
| 23 | +* `Operating System Security Measures`_. |
| 24 | +* `Python Runtime Audit Hooks`_. |
| 25 | +* `Custom URL Openers`_. |
| 26 | + |
| 27 | +Of these, operating system security measures are recommended. The other |
| 28 | +measures work, but they are not as effective as operating system security |
| 29 | +measures, and even if they are used they should be used in conjunction with |
| 30 | +operating system security measures. |
| 31 | + |
| 32 | +Operating System Security Measures |
| 33 | +================================== |
| 34 | + |
| 35 | +Most operating systems provide functionality that can be used to restrict |
| 36 | +network and file access of a process. |
| 37 | + |
| 38 | +Some examples of these include: |
| 39 | + |
| 40 | +* `Open Container Initiative (OCI) Containers |
| 41 | + <https://www.opencontainers.org/>`_ (aka Docker containers). |
| 42 | + |
| 43 | + Most OCI runtimes provide mechanisms to restrict network and file access of |
| 44 | + containers. For example, using Docker, you can limit your container to only |
| 45 | + being access files explicitly mapped into the container and only access the |
| 46 | + network through a firewall. For more information refer to the |
| 47 | + documentation of the tool you use to manage your OCI containers: |
| 48 | + |
| 49 | + * `Kubernetes <https://kubernetes.io/docs/home/>`_ |
| 50 | + * `Docker <https://docs.docker.com/>`_ |
| 51 | + * `Podman <https://podman.io/>`_ |
| 52 | + |
| 53 | +* `firejail <https://firejail.wordpress.com/>`_ can be used to |
| 54 | + sandbox a process on Linux and restrict its network and file access. |
| 55 | + |
| 56 | +* File and network access restrictions. |
| 57 | + |
| 58 | + Most operating systems provide a way to restrict operating system users to |
| 59 | + only being able to access files and network resources that are explicitly |
| 60 | + allowed. Applications that process untrusted input could be run as a user with |
| 61 | + these restrictions in place. |
| 62 | + |
| 63 | +Many other measures are available, however, listing them outside the scope |
| 64 | +of this document. |
| 65 | + |
| 66 | +Of the listed measures OCI containers are recommended. In most cases, OCI |
| 67 | +containers are constrained by default and can't access the loopback interface |
| 68 | +and can only access files that are explicitly mapped into the container. |
| 69 | + |
| 70 | +Python Runtime Audit Hooks |
| 71 | +========================== |
| 72 | + |
| 73 | +From Python 3.8 onwards, Python provides a mechanism to install runtime audit |
| 74 | +hooks that can be used to limit access to files and network resources. |
| 75 | + |
| 76 | +The runtime audit hook system is described in more detail in `PEP 578 – Python |
| 77 | +Runtime Audit Hooks <https://peps.python.org/pep-0578/>`_. |
| 78 | + |
| 79 | +Runtime audit hooks can be installed using the `sys.addaudithook |
| 80 | +<https://docs.python.org/3/library/sys.html#sys.addaudithook>`_ function, and |
| 81 | +will then get called when audit events occur. The audit events raised by the |
| 82 | +Python runtime and standard library are described in Python's `audit events |
| 83 | +table <https://docs.python.org/3/library/audit_events.html>`_. |
| 84 | + |
| 85 | +RDFLib uses `urllib.request.urlopen` for HTTP, HTTPS and other network access, |
| 86 | +and this function raises a ``urllib.Request`` audit event. For file access, |
| 87 | +RDFLib uses `open`, which raises an ``open`` audit event. |
| 88 | + |
| 89 | +Users of RDFLib can install audit hooks that react to these audit events and |
| 90 | +raises an exception when an attempt is made to access files or network resources |
| 91 | +that are not explicitly allowed. |
| 92 | + |
| 93 | +RDFLib's test suite includes tests which verify that audit hooks can block |
| 94 | +access to network and file resources. |
| 95 | + |
| 96 | +RDFLib also includes an example that shows how runtime audit hooks can be |
| 97 | +used to restrict network and file access in :mod:`~examples.secure_with_audit`. |
| 98 | + |
| 99 | +Custom URL Openers |
| 100 | +================== |
| 101 | + |
| 102 | +RDFLib uses the `urllib.request.urlopen` for HTTP, HTTPS and other network |
| 103 | +access. This function will use a `urllib.request.OpenerDirector` installed with |
| 104 | +`urllib.request.install_opener` to open the URLs. |
| 105 | + |
| 106 | +Users of RDFLib can install a custom URL opener that raise an exception when an |
| 107 | +attempt is made to access network resources that are not explicitly allowed. |
| 108 | + |
| 109 | +RDFLib's test suite includes tests which verify that custom URL openers can be |
| 110 | +used to block access to network resources. |
| 111 | + |
| 112 | +RDFLib also includes an example that shows how a custom opener can be used to |
| 113 | +restrict network access in :mod:`~examples.secure_with_urlopen`. |
0 commit comments