What is Ponn?
JSON, the ubiquitous JavaScript Object Notation is a nice format for data exchange between machines, specially when humans may want to peek and quickly understand the exchanged messages. JSON, however, is very limited in its data types and it can be brittle to write by hand (no comments, no trailing commas, no multi-line strings, etc).
Ponn is a simple subset of Python that represents objects as namespaces. It is designed to be a more human-friendly alternative to JSON for configuration files and sacrifices performance for ease of use (at least when used by Python developers). It is very similar to PON, the Python Object Notation, but it represents objects as Python modules instead of top-level dictionaries.
Ponn is also more flexible than PON (and JSON) in that it allows for more complex data structures and some limited form of programming (e.g. you can use variables, conditional statements, function execution, and imports).
Ponn files are thus valid Python files that can be executed and imported as modules. This module simply provides a secure way to load Ponn files by checking if they contain only valid Ponn constructs before executing them.
Usage
To load a Ponn file, simply use the load function from the ponn module:
from ponn import load
config = load("config.ponn")
print(config["database"]["host"]) # Accessing some inner structure
The load() function returns a dictionary object mapping top-level variable
names to their values.
The Ponn subset
Ponn supports a subset of Python syntax that is safe to execute from untrusted sources. Ponn can execute imports and call arbitrary functions from a whitelist, so obviously the security depends on the security of the functions you allow to be executed.
What is allowed in Ponn files?
- Comments
- Variable assignments (except dunder variables)
- All python literals (strings, numbers, lists, tuples, dictionaries, sets, etc)
- Function calls to whitelisted functions
- Imports of whitelisted modules
- Conditional statements (if, elif, else) and ternary expressions
- Match statements
- Class definitions using dataclasses (and without methods)
What is forbidden in Ponn files?
- Any function calls not explicitly allowed in the whitelist
- Attribute access to dunder methods or attributes (e.g.
__import__,__class__, etc) - Some built-in functions that are not in the whitelist (e.g.
open,eval,exec, etc) - Some functions are converted to no-op to prevent side effects (e.g.
print). - Loops and comprehensions (for, while) are not allowed to prevent unbounded execution.
- Function definitions (def, lambda) are not allowed to prevent infinite recursion.
- Class definitions of arbitrary classes.
- The with statement is not allowed to prevent resource leaks.
- The async and await keywords.
How does it work?
Ponn uses the ast module to parse the Ponn file and check if it contains only
valid Ponn syntax. If the syntax is valid, it executes the file in a restricted
environment where only the whitelisted functions and modules are available.
Some builtins are modified to prevent side effects or trigger errors when considered
unsafe (e.g. print is converted to a no-op, open raises an error, etc).
How safe is it?
Probably safe. Ponn is designed to be a safe subset of Python, but given the extreme flexibility and reflective capabilities of Python, it is very difficult to guarantee absolute safety. Many sandobox implementations have been broken in the past, but they all aimed for a more generic sandboxed environment execution (turing complete subsets of Python, not only flexible data representation).
Metadata
Release files for ponn 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ponn-0.1.0.tar.gz | 9.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ponn-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.3 kB
Release files / ponn-0.1.0.tar.gz
| Download URL | ponn-0.1.0.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
346f8a29dd8d9a89d16a6d01372a93d018b930f2055dc4b2c6d0b6d87f008ed2
|
|
BLAKE2b-256 checksum How to use checksums |
c2999ab0ec9d5eea499585c7e5e4005a623d8c2b515a78c5f1d7bc08553ccf90
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.22
|
Release files / ponn-0.1.0-py3-none-any.whl
| Download URL | ponn-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6a0af2650f5ff505c270fa4cfed0b501a4d86710f39121f9ea93ce6b3ececcbe
|
|
BLAKE2b-256 checksum How to use checksums |
721f04575195a9cc675dc81482f5d87b303129b4eae83dadee6c038c80421214
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.22
|