BEJSON 104 Core SH/PY Libraries

BEJSON 104 - Core SH/PY Libraries

BEJSON 104 - Core SH/PY Libraries

By Leethaxor69

Table of Contents


Chapter 1: Chapter 1: Architecture of BEJSON SH/PY Core Libraries & Low-Level Fundamentals

The Core Library Architecture: Philosophy and Design

The BEJSON (Boehnen Elton JSON) ecosystem relies on a strictly partitioned architecture to prevent the "dependency hell" common in modern development. Whether you are running on a bare-metal Linux server or a restricted Python container, the core libraries—collectively known as lib_bejson_Core—are designed for zero-config, low-level operational security.

The core is split into two primary language families: SH (Bash) for system-native, dependency-free file interaction, and PY (Python) for high-throughput, structural data manipulation. The fundamental axiom across both is Positional Integrity: the libraries assume the data matrix is valid based on the document's own Fields header. They do not guess types; they enforce them.

The SH/PY Operational Split

Why do we maintain two distinct language implementations for the same core logic?

  • SH (The "Bare-Metal" Layer): Used for scripts where pip or heavy runtimes are a liability. The Bash libraries (e.g., lib_bejson_core.sh) utilize standard awk, sed, and jq (if present) to manipulate Values records. These are intended for system integration, log rotation, and infrastructure orchestration where the shell environment is the execution boundary.
  • PY (The "High-Performance" Layer): Used for intensive tasks where recursive chunking, schema validation, or cryptographic hashing of binary payloads (lib_bejson_Core_bejson_chunking.py) is required. Python provides the necessary performance overhead to handle multi-gigabyte entity files while maintaining the strict memory safety mandated by the spec.

Foundational Principles of the Core Libraries

Every function within these libraries—whether it's bejson_safe_join for path traversal prevention or bejson_core_chunking_hash_file_bytes for data integrity—follows three hard rules:

  1. Strict Isolation: No library is allowed to reach outside its assigned Data/ or Root/ directory without passing through the path guard (lib_bejson_Core_bejson_path_guard.py).
  2. No Schema Guessing: If the Fields definition in a document says a column is a number, the code will not attempt to treat it as a string. If the document breaks the matrix—by having a row shorter than the Fields length—the library throws a BEJSONValidationError immediately.
  3. Atomic Persistence: All write operations follow the "Write-to-Temp-and-Rename" protocol. We never perform in-place edits on a live .bejson file, as a crash during the write would corrupt the positional index.

Anatomy of a Core Operation: Path Guarding

Security is not an afterthought; it is baked into the library loading order. The lib_bejson_Core_bejson_path_guard.py is the most critical component in the entire suite. Every file-accessing function in the core libraries imports this module first.

# lib_bejson_Core_bejson_path_guard.py
from pathlib import Path

def bejson_safe_join(base: str, target: str) -> Path:
    """
    Prevents directory traversal by resolving the absolute path
    and verifying the prefix matches the base directory.
    """
    base_path = Path(base).resolve()
    target_path = (base_path / target).resolve()
    
    if not str(target_path).startswith(str(base_path)):
        raise ValueError("Security Violation: Path traversal attempt detected.")
    
    return target_path

This function is the shield. If an attacker attempts to point a file_path to /etc/shadow, the library catches the divergence before the file descriptor is ever opened.

Why "Low-Level" Matters

You might wonder why we don't just use a standard ORM or database driver. The answer is simple: control. When you build on top of these libraries, you are writing code that interprets the document as a matrix, not as a black-box object.

  • Predictability: Because the library relies on field indexing rather than key-value mapping, your memory footprint remains constant regardless of whether you are reading 10 records or 10,000.
  • Binary Safety: Since the 2026-08-02 update, binary content is no longer a risk. Our core library uses Base64 encoding/decoding as the standard intermediary, ensuring that image assets or encrypted blobs can live directly inside a BEJSON entity without corrupting the ASCII-based document structure.

In the chapters that follow, we will dissect how these libraries handle the complex relational mappings of 104db and the recursive requirements of MFDB 1.32. If you are looking to integrate BEJSON into your production stack, master these primitives first. The schema is the law, and these libraries are the enforcers.


Chapter 2: Chapter 2: Python Core Engine - Parsing, Matrix Operations, and Positional Access

Stop Parsing JSON Like a Noob: The Positional Matrix Paradigm

If you are still pulling data out of JSON documents by iterating through dictionary keys on every single row like a script kiddie who just discovered json.loads(), stop embarrassingly wasting CPU cycles. Standard JSON parsing is a performance disaster. Every time you access record["username"] inside a loop of 100,000 objects, your runtime is doing string comparisons, hash calculations, and memory lookups for keys that repeat endlessly on every single line. It is bloat, it is slow, and it is why your code crawls.

As my coworker lazily pointed out in the architectural overview, the entire BEJSON ecosystem—and specifically our Python core engine in lib_bejson_Core—operates on Positional Integrity.

In BEJSON, field names are declared once in the top-level Fields array. The actual records inside Values are raw, ordered lists (matrices). You don't look up keys inside rows. You resolve the field name to a zero-based column index once via Field Map Cache (bejson_core_get_field_map), and then you hit row[col_index] for instantaneous $O(1)$ matrix access.

Data Paradigm Row Structure Lookup Time Complexity Memory Overhead Field Duplication
Standard JSON [{"id": 1, "val": "A"}, {"id": 2, "val": "B"}] $O(N)$ key hash/comparison per row Extremely High (Keys repeated per row) 100% Repetitive
BEJSON Matrix [["id", "val"], [[1, "A"], [2, "B"]]] $O(1)$ positional array offset Extremely Low (Single schema declaration) 0% Repetitive

Lmao, look at that table. If you can't see why positional access pwns standard JSON object parsing, you should probably switch back to writing HTML.


Python Core Engine: Ingesting and Mapping the Matrix

Let me break down how the Python core engine parses a BEJSON document. When lib_bejson_Core ingests a raw JSON payload, it doesn't build heavy object wrappers or instantiate bloat-ware ORM classes. It validates the foundational structural contract, builds an in-memory field map, and leaves the underlying Values matrix as native Python lists for raw, unadulterated execution speed.

Step 1: Baseline Structural Verification

Before touching a single row, the engine verifies the mandatory top-level structure. Every valid BEJSON document—whether it's 104, 104a, or 104db—must contain the exact six mandatory top-level keys: Format, Format_Version, Format_Creator, Records_Type, Fields, and Values. Furthermore, Format_Creator must literally equal "Elton Boehnen". If any of these are missing or wrong, we blow up immediately with a validation error before wasting microsecond one on row parsing.

Step 2: Building the Field Index Map

To achieve $O(1)$ positional access, the core engine scans the Fields array and generates a lookup dictionary mapping each field's name string to its exact positional index integer.

Here is the low-level positional indexing implementation used throughout our core Python routines:

from typing import Dict, List, Any, Optional

class BEJSONValidationError(Exception):
    """Raised when positional integrity or structural rules are violated."""
    pass

def bejson_core_build_field_map(doc: Dict[str, Any]) -> Dict[str, int]:
    """
    Scans the Fields header of a BEJSON document and returns a hash map
    of field_name -> column_index.
    
    Provides O(1) positional lookups for all subsequent matrix operations.
    """
    fields: Optional[List[Dict[str, Any]]] = doc.get("Fields")
    if not isinstance(fields, list):
        raise BEJSONValidationError("Invalid BEJSON: 'Fields' must be an array.")
        
    field_map: Dict[str, int] = {}
    for index, field_def in enumerate(fields):
        if not isinstance(field_def, dict) or "name" not in field_def:
            raise BEJSONValidationError(f"Invalid field definition at index {index}.")
            
        field_name = field_def["name"]
        if field_name in field_map:
            raise BEJSONValidationError(f"Duplicate field name detected: '{field_name}'")
            
        field_map[field_name] = index
        
    return field_map

def bejson_core_get_field_index(field_map: Dict[str, int], field_name: str) -> int:
    """
    Returns the zero-based column index for a given field name.
    Raises BEJSONValidationError if the field does not exist in the schema.
    """
    if field_name not in field_map:
        raise BEJSONValidationError(f"Field '{field_name}' not found in BEJSON schema.")
    return field_map[field_name]

Do you see how simple that is? You build field_map once. After that, finding where temperature or user_id lives takes a single dictionary lookup. You never search through string keys inside individual data rows again.


Matrix Traversal, Structural Enforcement, and Mutation

Once the field map is established, the engine operates on the Values array. Here is the golden rule of BEJSON positional integrity: The length of every single row inside Values must EXACTLY match the length of the Fields array.

If a row is too short or too long, the matrix is broken. In BEJSON, missing data is NEVER omitted—it is explicitly padded with null (Python None). If a row has 5 items and Fields has 6 items, that is not "optional data," that is a corrupted file, and the core engine will shut it down instantly.

High-Performance Traversal & Direct In-Place Mutation

Because Values is a list of lists, updating a value in a BEJSON document does not require re-building objects, copying dictionaries, or re-allocating keys. You resolve the column index and mutate the element in place at Values[row_index][col_index].

Here is how lib_bejson_Core handles matrix verification, row traversal, and zero-allocation mutations in Python:

def bejson_core_process_and_update_matrix(
    doc: Dict[str, Any], 
    target_field: str, 
    search_value: Any, 
    update_field: str, 
    new_value: Any
) -> int:
    """
    Validates positional integrity across the matrix, searches for records
    matching target_field == search_value, and updates update_field in-place.
    
    Returns the count of modified rows.
    """
    # 1. Mandatory top-level validation
    for key in ("Format", "Format_Version", "Format_Creator", "Records_Type", "Fields", "Values"):
        if key not in doc:
            raise BEJSONValidationError(f"Missing mandatory top-level key: {key}")

    if doc["Format_Creator"] != "Elton Boehnen":
        raise BEJSONValidationError("Invalid Format_Creator anchor. Must be 'Elton Boehnen'.")

    fields = doc["Fields"]
    values = doc["Values"]
    expected_length = len(fields)

    # 2. Build the field index map
    field_map = bejson_core_build_field_map(doc)
    target_idx = bejson_core_get_field_index(field_map, target_field)
    update_idx = bejson_core_get_field_index(field_map, update_field)

    modified_count = 0

    # 3. Traversal and Positional Integrity Enforcement
    for row_num, row in enumerate(values):
        if not isinstance(row, list):
            raise BEJSONValidationError(f"Row {row_num} is not an array.")

        # POSITIONAL INTEGRITY CHECK: Row length MUST equal Fields length
        if len(row) != expected_length:
            raise BEJSONValidationError(
                f"Positional Integrity Failure at row {row_num}: "
                f"Expected {expected_length} items, got {len(row)}."
            )

        # Direct O(1) positional read
        if row[target_idx] == search_value:
            # Direct O(1) in-place matrix mutation
            row[update_idx] = new_value
            modified_count += 1

    return modified_count

Notice what happened there. We validated every single row's positional length against expected_length. If a noob passed us a corrupted row with missing elements, BEJSONValidationError blew up on the spot. If the row was clean, we did a direct array index read and an in-place mutation. No new memory allocations. No object overhead. Pure speed.


Multi-Entity Matrix Logic: 104db Discriminators vs. Dense 104/MFDB Arrays

Don't confuse BEJSON 104db with standard 104 or MFDB entity files. I see noobs make this mistake constantly.

  1. BEJSON 104 & MFDB Entity Files: These are dense arrays. Every row in the file belongs to the exact same entity type (declared in Records_Type). A null in a row means the data for that field is genuinely missing or empty for that entity instance.
  2. BEJSON 104db Files: This is a single-file multi-entity relational format. Because multiple entity types (e.g., ["User", "Item"]) share a single Fields and Values array, fields that belong to User MUST be set to null in rows where the entity is Item.

In 104db, the engine mandates that index 0 of the Fields array is defined exactly as: {"name": "Record_Type_Parent", "type": "string"}

Position 0 of every single row in Values acts as the discriminator. It tells the engine which entity type that row represents.

Python Engine Logic for 104db Filtering

When processing a 104db file in Python, the core engine inspects index 0 to determine entity ownership before touching the remaining matrix columns.

def bejson_core_extract_104db_entity_rows(
    doc: Dict[str, Any], 
    target_entity: str
) -> List[Dict[str, Any]]:
    """
    Extracts rows belonging to target_entity from a BEJSON 104db matrix.
    Filters out structural nulls corresponding to other entities and returns
    a list of clean, non-null field-value dictionaries for the target entity.
    """
    if doc.get("Format_Version") != "104db":
        raise BEJSONValidationError("Document is not BEJSON 104db format.")

    records_type = doc.get("Records_Type", [])
    if not isinstance(records_type, list) or len(records_type) < 2:
        raise BEJSONValidationError("BEJSON 104db requires 2 or more entity types in Records_Type.")

    if target_entity not in records_type:
        raise BEJSONValidationError(f"Entity '{target_entity}' not declared in Records_Type.")

    fields = doc["Fields"]
    values = doc["Values"]

    # Validate Discriminator Field at Index 0
    if not fields or fields[0].get("name") != "Record_Type_Parent":
        raise BEJSONValidationError("104db violation: Field 0 must be 'Record_Type_Parent'.")

    field_map = bejson_core_build_field_map(doc)
    
    # Identify fields that belong specifically to target_entity
    entity_field_indices = [
        idx for idx, f_def in enumerate(fields)
        if f_def.get("Record_Type_Parent") == target_entity
    ]

    extracted_records = []

    for row_num, row in enumerate(values):
        if len(row) != len(fields):
            raise BEJSONValidationError(f"Row {row_num} length mismatch in 104db matrix.")

        # Read the discriminator at Index 0
        row_discriminator = row[0]
        if row_discriminator == target_entity:
            # Build clean entity record ignoring structural nulls of OTHER entities
            record_dict = {}
            for idx in entity_field_indices:
                fname = fields[idx]["name"]
                val = row[idx]
                record_dict[fname] = val
            extracted_records.append(record_dict)

    return extracted_records

Look at how clean that is. When reading a 104db document, position 0 tells you immediately whether to process or skip the row. If the row discriminator matches target_entity, we extract only the indices mapped to that specific parent entity, effortlessly ignoring the structural null padding forced by the other entities in the file.

Error Handling: Never Let Corrupted Data Pass

If you write Python code for BEJSON, your error handling must be unforgiving. If a field value is supposed to be an integer according to Fields, but someone put "thirty-two" in Values, or if a row is missing a column, raise BEJSONValidationError immediately.

As my coworker mentioned regarding atomic operations: never perform partial updates on corrupted files. Validate positional integrity, check row lengths, verify top-level anchors, and only then execute your matrix operations. That is how you write rock-solid Python code in the BEJSON ecosystem. Now go fix your slow JSON parsers.


Chapter 3: Chapter 3: Bash Core Utilities - Shell Native Data Extraction & Matrix Traversal

If you’re still trying to manipulate complex data in Bash using sed or awk without a rigid structural contract, you’re just inviting disaster. You’ll be chasing escaped characters and regex bugs until the end of time. Bash is not an object-oriented language; it’s an environment for stream processing. BEJSON’s positional matrix paradigm is the only thing that makes this sane, because it turns a messy JSON structure into a predictable, line-delimited stream that read and cut can actually handle without losing their minds.

In the shell, we don’t instantiate Python objects. We treat the Values matrix as a raw text stream. When you parse a BEJSON document in Bash, you are essentially performing a two-stage process:

  1. Metadata Stripping: Isolating the Values matrix from the JSON header noise.
  2. Positional Extraction: Using the field index map derived from the Fields array to slice the stream.

Stage 1: Native Stream Extraction

Bash doesn't have a native JSON parser, and if you try to grep your way through a minified file, you'll be pwned by every edge case in the RFC. The professional approach—and the one implemented in our core lib_bejson_Core shell wrappers—is to use jq as a surgical instrument to extract the Values array into a format Bash can consume, or to use read arrays if the document size is manageable.

# Example: Extracting the Values array into a temp file for line-by-line processing
# This is O(1) in terms of traversal logic once jq isolates the matrix.
jq -c '.Values[]' source.bejson > .matrix.tmp

# Now we can iterate over the matrix rows directly in the shell
while IFS= read -r row; do
    # 'row' is now a valid JSON array string like: ["U01", "alice", 100]
    # We strip the brackets and use internal field separators (IFS) to tokenize
    clean_row=${row//[\[\]]/}
    IFS=',' read -r col1 col2 col3 <<< "$clean_row"
    
    # Direct access: col1 is the field at index 0, col2 at index 1...
    echo "Processing User: $col2 (ID: $col1)"
done < .matrix.tmp

Stage 2: Positional Traversal Logic

The secret to not being a noob in Bash is realizing that the Fields array provides the index map for your cut or awk commands. If you know that email is at index 2 (the third column), you don't look for the string "email" in the record. You tell the shell to extract the 3rd field.

This implementation of bejson_core_get_value shows how to perform the index lookup and extraction using the jq path traversal mechanism, ensuring we don't manually parse the JSON ourselves.

# Extract a specific value by index from a row record
# $1: The raw JSON array row string
# $2: The zero-based column index
get_val_by_index() {
    local row_data="$1"
    local col_idx="$2"
    # Extract the value at index using jq
    echo "$row_data" | jq -r ".[$col_idx]"
}

# Usage:
row='["U01", "alice", "alice@example.com"]'
email=$(get_val_by_index "$row" 2)
echo "Extracted Email: $email" # Outputs: alice@example.com

The "Field Shifting" Defense: Strict Validation

Because Bash is loose with types, the greatest risk is "field shifting." If a document is corrupted and a null is missing, your read command might assign the wrong data to the wrong variable. You must validate the row length before processing.

Never trust the stream. Always enforce the matrix contract at the shell gate.

# Validate column count against expected schema length
validate_row_length() {
    local row_data="$1"
    local expected_len="$2"
    
    # Count commas in the JSON array to determine field count (naive but effective for 104)
    # BEJSON 104 requires length match for positional integrity
    local actual_len=$(echo "$row_data" | jq 'length')
    
    if [ "$actual_len" -ne "$expected_len" ]; then
        echo "CRITICAL: Positional integrity failure. Expected $expected_len, got $actual_len" >&2
        return 1
    fi
    return 0
}

Why Shell Natives Should Prefer BEJSON

Standard JSON in Bash is a nightmare because of the escaping required to pass objects between tools. If your JSON field contains a double quote or a newline, your sed or awk command will break.

BEJSON 104's positional design allows you to:

  1. Pass by Index: Once you’ve split a row, you’re dealing with flat variables, not JSON strings.
  2. Minimize jq Overhead: You only call jq to serialize/deserialize the matrix rows. Everything else happens in native Bash variables, which is orders of magnitude faster.
  3. Avoid Key Resolution: You never perform a string lookup for a key inside a Bash loop. You define the index once at the start of your script and use it forever.

If you’re writing system administration scripts to audit configurations or handle log data in the BEJSON format, keep the logic in the shell as flat as possible. Use the Fields array to document your indexes at the top of your script, and treat the Values as a simple, delimited matrix. If you find yourself doing complex object manipulation, you’ve stopped being a shell engineer and started trying to be a database engine—which is exactly why lib_bejson_Core exists in Python. Use the right tool for the job.


Chapter 4: Chapter 4: Validation Implementations - Structural and Strict Type Checks in PY & SH

If you're still relying on "try-catch" blocks to handle bad data, you're doing it wrong. In the BEJSON ecosystem, validation is not an afterthought; it’s a hard gate. If a document fails the schema contract, it shouldn't even be parsed.

Structural validation ensures the "matrix" is physically intact, while strict type checking prevents the type-coercion bugs that turn production environments into a dumpster fire. We use two distinct strategies: Python for the heavy-lifting logic and Bash for the lightweight, stream-based enforcement.

The Python Validator: lib_bejson_Core_bejson_validators.py

Python is the source of truth for schema adherence. Our validator is designed for non-negotiable compliance. It doesn't just check if the JSON is valid—it checks if the document follows the 104/104a/104db specifications precisely.

The core of the Python validator relies on checking the Fields definition against the Values matrix. If the Values row length deviates from the Fields length, the document is rejected. This prevents "field shifting"—the silent killer of data integrity.

def validate_104_structure(doc: Dict[str, Any]):
    """Strict structural check for BEJSON 104."""
    # 1. Mandatory Keys
    mandatory = ["Format", "Format_Version", "Format_Creator", "Records_Type", "Fields", "Values"]
    for key in mandatory:
        if key not in doc:
            raise ValueError(f"Missing mandatory key: {key}")

    # 2. Positional Integrity Check
    field_count = len(doc["Fields"])
    for i, row in enumerate(doc["Values"]):
        if not isinstance(row, list):
            raise TypeError(f"Row {i} is not an array")
        if len(row) != field_count:
            raise ValueError(f"Positional integrity failure at row {i}. "
                             f"Expected {field_count} fields, found {len(row)}.")
    
    return True

This function is the first line of defense. By enforcing len(row) == len(Fields), we guarantee that for every record, index n always points to the same field, regardless of whether that field contains actual data or a mandatory null placeholder.

Strict Type Enforcement

Since BEJSON 104 declares types (string, integer, number, boolean, array, object) in the Fields array, our Python libraries include a strict type-checker. This function verifies that every item in the Values matrix matches the corresponding type definition.

def check_row_types(doc: Dict[str, Any]):
    """Iterate and verify types defined in the schema."""
    field_types = [f["type"] for f in doc["Fields"]]
    
    for row in doc["Values"]:
        for i, val in enumerate(row):
            expected = field_types[i]
            if val is None: continue # null is valid for all types
            
            # Simple mapping check
            if expected == "integer" and not isinstance(val, int):
                raise TypeError(f"Col {i} expected int, got {type(val)}")
            elif expected == "boolean" and not isinstance(val, bool):
                raise TypeError(f"Col {i} expected bool, got {type(val)}")
            # (Add additional mapping logic as needed)

The Bash Validator: Gatekeeping at the Edge

In the shell, we don't have the luxury of Python's object introspection. We have to be surgical. If you are piping a massive log file or a config dump through a processing pipeline, you don't want to load it into Python just to check if it's broken. You use jq to enforce the matrix integrity at the edge.

The goal in Bash is "Fail Fast." If a row is malformed, we drop the line and log the error before it hits your production awk logic.

# Validate against field_count defined in the document manifest or schema
validate_stream_integrity() {
    local row="$1"
    local expected_count="$2"
    
    # Use jq to get the actual array length
    local actual_count=$(echo "$row" | jq 'length')
    
    if [[ "$actual_count" -ne "$expected_count" ]]; then
        # Log to stderr to avoid polluting the stdout data stream
        echo "[ERROR] Schema violation: Expected $expected_count fields, found $actual_count" >&2
        return 1
    fi
}

# Example usage in a stream
cat data.json | jq -c '.Values[]' | while read -r row; do
    if validate_stream_integrity "$row" 5; then
        # Process the clean data
        process_row "$row"
    fi
done

Why Strict Validation Matters

Most people try to "parse" JSON. They try to handle missing keys with complex if-else blocks or default value injection. That is how you get security vulnerabilities.

By enforcing the BEJSON 104 structure:

  1. No Implicit Defaults: If the data isn't there, the null must be there. If you don't see the null, the row is invalid.
  2. Deterministic Processing: You don't ask "does this object have an 'email' key?". You ask "is the value at index 2 a string?".
  3. Auditability: When your validator fails, it points to a specific index in a specific record. It tells you exactly where the integrity contract was broken, making debugging a trivial task rather than a 4-hour forensic nightmare.

If your code doesn't start with validate_104_structure (or a shell-native equivalent), you aren't writing a secure integration—you're writing a ticking time bomb. Use these libraries, enforce the schema, and quit relying on guesswork.


Chapter 5: Chapter 5: Path Traversal Defense & Security Guarding - `lib_bejson_Core_bejson_path_guard.py`

Look, if you’re still letting user-supplied input determine file paths without sanitization, you’re basically begging for a pwn. Path traversal is the oldest trick in the book, and if your "secure" application is writing or reading files based on a Relative_Path field from a BEJSON document, you need to lock that down.

The lib_bejson_Core_bejson_path_guard.py is the ecosystem’s standard for preventing an attacker from escaping your data directory using ../ sequences. If you aren't routing every filesystem write through this, don't come crying to me when your /etc/passwd file gets overwritten.

The Logic of the Guard

The core of this library is bejson_safe_join(). It doesn’t just blindly concatenate paths. It resolves the absolute path of your target directory and ensures that the final destination—after resolving all the "dot-dot-slash" junk—still resides inside that base directory.

If an attacker tries to inject ../../bin/evil_script, the guard realizes the resolved path is outside the safe boundary and raises a ValueError.

from pathlib import Path

def bejson_safe_join(base_dir: str, target_path: str) -> Path:
    """
    Prevents path traversal by ensuring the target path is 
    strictly contained within the base_dir.
    """
    base = Path(base_dir).resolve()
    # Resolve the path, but don't require it to exist yet
    target = (base / target_path).resolve()
    
    if not str(target).startswith(str(base)):
        raise ValueError(f"Security Alert: Path traversal attempt detected! "
                         f"Target {target} is outside of {base}")
    
    return target

Why This is Mandatory

Most developers think os.path.join is enough. It isn't. os.path.join will happily combine your safe folder with a malicious payload, resulting in a path that exits your sandbox. Our guard uses pathlib's .resolve() to normalize everything before checking the string prefix.

If you're handling Chunked-104 or MFDB files, these are often dynamic. When unchunking a package, you are trusting the Relative_Path field defined by the manifest or the chunking header. Never trust the document. Trust the guard.

Bash Implementation: bejson_safe_join.sh

For shell scripts, we can’t use Python's pathlib goodness, so we use realpath to achieve the same result. If you’re writing a loader script in Bash to unpack a BEJSON structure, use this function to vet your paths before you touch the disk.

# Security Guard for Bash
bejson_safe_join() {
    local base_dir="$1"
    local target_rel="$2"
    
    # Resolve absolute paths
    local base_abs=$(realpath -m "$base_dir")
    local target_abs=$(realpath -m "$base_dir/$target_rel")
    
    # Check if target starts with base
    if [[ "$target_abs" != "$base_abs"* ]]; then
        echo "[SECURITY CRITICAL] Attempted path traversal: $target_rel" >&2
        return 1
    fi
    
    echo "$target_abs"
}

Practical Security Workflow

Every function in the ecosystem that performs disk I/O must adhere to this protocol:

  1. Input: Retrieve Relative_Path from the BEJSON record.
  2. Sanitize: Pass base_directory and Relative_Path to bejson_safe_join.
  3. Verify: If it throws a ValueError (or returns non-zero in Bash), immediately abort the process, log the attempt, and wipe the temporary buffer.
  4. Execute: Only proceed to open() or write() if the guard returns the validated path.

Stop thinking you’re "clever" by manually filtering strings. Regex filters for ../ are bypassable (ever heard of URL encoding or null-byte injections?). bejson_safe_join relies on OS-level path normalization. It’s not just best practice; it's the only way to keep your environment from getting pwned by a script-kiddie with a text editor. Use the library, or accept the risk.


Chapter 6: Chapter 6: Chunking Mechanics - `lib_bejson_Core_bejson_chunking.py` & Base64 Binary Encoding

If you think you can handle multi-file relational data by just dumping raw files into a folder, you're a noob. In the BEJSON ecosystem, we "chunk" data. Chunking is the process of flattening a directory structure or an MFDB into a single, valid BEJSON 104a document. This is how we move entire project states or databases as a single, immutable artifact without breaking positional integrity.

The core engine is lib_bejson_Core_bejson_chunking.py. It handles the heavy lifting of recursive file traversal, hash verification, and—most importantly—binary preservation.

The Binary Preservation Problem

Earlier versions of our chunking tools were trash because they dropped binary files. If you had a .png or a compiled binary in your directory, the chunker just ignored it. We fixed that. Now, the Is_Binary field in the schema acts as a switch:

  1. Is_Binary = False: The file is treated as UTF-8 text and shoved into File_Content.
  2. Is_Binary = True: The content is raw bytes, so we Base64-encode it before storage.

If you try to read an unchunked binary file as plain text, you’re going to get a UnicodeDecodeError and your script will crash. If you don't know why, go learn basic I/O before touching this library.

import base64

def bejson_core_chunking_is_binary(file_path) -> bool:
    """
    Tries to read 1024 bytes as UTF-8. If it fails, it's binary.
    """
    try:
        with open(file_path, 'tr', encoding='utf-8') as f:
            f.read(1024)
            return False
    except (UnicodeDecodeError, PermissionError):
        return True

Encoding/Decoding Workflow

When you run create_chunked_104, the library checks the file type. If it's binary, it performs the Base64 dance. When you run unchunk, the library looks at Is_Binary and reverses the process. If you’re writing custom tools to handle these artifacts, you MUST check this flag.

# During Chunking (Writing the BEJSON 104a)
if bejson_core_chunking_is_binary(path):
    with open(path, 'rb') as f:
        content = base64.b64encode(f.read()).decode('utf-8')
    is_binary = True
else:
    with open(path, 'r', encoding='utf-8') as f:
        content = f.read()
    is_binary = False

# During Unchunking (Restoring from BEJSON)
if record[is_binary_index]:
    binary_data = base64.b64decode(record[file_content_index])
    with open(target_path, 'wb') as f:
        f.write(binary_data)

Bash Implementation: The Dirty Way

Bash isn't great at handling Base64 natively without piping, but since everything in the ecosystem needs to be language-agnostic, our Bash utilities leverage base64 system binaries. If you're building a shell-based unchunker, don't try to parse the JSON manually—use jq to extract the File_Content and pipe it to base64 -d.

# Bash snippet for unchunking binary data from a 104a row
# Assumes 'row' is a JSON string of the record
content=$(echo "$row" | jq -r '.[2]') # File_Content index
is_binary=$(echo "$row" | jq -r '.[6]') # Is_Binary index

if [ "$is_binary" == "true" ]; then
    echo "$content" | base64 -d > "$output_path"
else
    echo "$content" > "$output_path"
fi

Structural Integrity: The Package Version

One thing you need to stop ignoring: Package_Version. We follow an "Always Bump" rule. Every time you re-chunk a target, the Package_Version must increment. If your chunker outputs a file with the same version after modifying content, your entire audit trail is useless.

The bejson_core_chunking_bump_package_version function is your best friend. Pass in the prior document dictionary, and it handles the arithmetic. Don't try to manually track this in your own app logic; let the library handle the increment so you don't end up with version collision hell.

Summary Checklist for Implementers

  • Don't ignore the SHA-256 hash: Use bejson_core_chunking_hash_file_bytes on your bytes before writing the record. If your read-back doesn't match the File_Hash in the record, your file is corrupted. Delete it and re-download.
  • Path Guarding: Always route your paths through bejson_safe_join before writing to disk. The chunking library does this, but if you're writing a custom parser, you’re on the hook for security.
  • Binary Bloat: Remember that Base64 encoding increases file size by roughly 33%. Don't chunk 2GB ISOs into a single BEJSON file unless you want your memory usage to explode. That's what MFDB splitting is for.

If you mess this up, you aren't just breaking your own code; you're breaking the compatibility of the entire BEJSON ecosystem. Keep your implementation byte-identical to the Python core, or get out.


Chapter 7: Chapter 7: MFDB 1.31 vs 1.32 Orchestration - Manifest Resolution & Package Unchunking

If you're still treating MFDBs like a folder full of random files, stop. The orchestration layer exists to prevent the exact kind of "orphan file" corruption you noobs keep causing. MFDB 1.31 is the classic multi-file standard, but MFDB 1.32 introduces the "Package Unchunking" protocol—a way to collapse an entire multi-file database into a single, addressable BEJSON 104a document.

The 1.31 Manifest Resolution Protocol

In MFDB 1.31, you rely on 104a.mfdb.bejson to act as the single source of truth. Every entity file has a Parent_Hierarchy pointing back to this root. If the paths don't resolve, the validator screams, and your code dies.

When resolving an MFDB 1.31 structure in Python, you must use the bejson_safe_join library. Never manually join paths with + "/" +. If you do, I’m personally going to find your repository and open an issue.

# The correct way to resolve a manifest entry path
from lib_bejson_Core_bejson_path_guard import bejson_safe_join

def resolve_entity(manifest_dir: str, file_path: str) -> str:
    # Always use safe_join to prevent path traversal attacks
    return bejson_safe_join(manifest_dir, file_path)

MFDB 1.32: The Single-Artifact Package

MFDB 1.32 is for when you need to ship a database that fits into a single transport stream (like an API response or a single text file) while keeping the relational logic intact. It doesn't replace the 1.31 file-system layout; it packages it.

The lib_bejson_Core_bejson_chunking.py library implements bejson_core_chunking_create_mfdb132_package. This function ingests an existing MFDB 1.31 directory and transforms it into a serialized Chunked-104a document. It preserves the manifest as the first record, followed by every entity file.

Packaging vs. Unchunking Mechanics

When you "unchunk" a 1.32 package, the library detects the schema via mfdb_validator_detect_mfdb_in_chunk. If the validator finds a 104a.mfdb.bejson manifest inside the chunked records, it triggers the automatic expansion of the database back into the standard 1.31 directory structure.

# Unchunking a 1.32 package to a local directory
from lib_bejson_Core_bejson_chunking import bejson_core_chunking_mfdb_unchunk

def restore_database(package_path: str, target_dir: str):
    # This automatically dispatches to the correct handler 
    # based on the detected MFDB schema
    return bejson_core_chunking_mfdb_unchunk(
        chunk_doc_path=package_path,
        out_root=target_dir
    )

The Bash "Unholy" Path to 1.32 Resolution

Doing this in Bash is painful because of the JSON parsing overhead, but sometimes you’re stuck in a restricted container. Use jq to isolate the File_Content and check the header of the extracted file to ensure it's a valid 104 manifest.

# Extracting a single entity from an MFDB 1.32 package
# 1. Get the row where File_Name is the entity
# 2. Extract the content and save it to the data/ directory
ROW=$(cat package.bejson | jq -r '.Values[] | select(.[0] == "user")')
CONTENT=$(echo "$ROW" | jq -r '.[2]')

# Always validate the path before writing
echo "$CONTENT" > "data/user.bejson"

Validation Requirements for 1.32

Don't get cocky just because it's a "package." You still need to run mfdb_validator_validate_mfdb132_package before you start parsing data. If you skip this, you’re reading garbage data that hasn't been verified for structural integrity.

  • Bidirectional Path Check: If the package claims an entity is at data/order.bejson but the internal Parent_Hierarchy points to the wrong root, the unchunker will throw an error. This is a safety feature, not a bug.
  • The MFDB_Version Header: Ensure it's set to "1.31" or higher. Anything lower is an antique and shouldn't be handled by the 1.32 chunking engine.
  • Atomic Swaps: When the unchunker restores the database, it writes to a temp directory first and then performs an os.rename. This is to prevent you from having a half-written, corrupted database if your process gets killed mid-operation.

If you attempt to bypass the validator, you deserve the resulting OSError. Use the provided library functions. They exist because you aren't smart enough to handle the edge cases of file system I/O and JSON state management on your own.


Chapter 8: Chapter 8: End-to-End Execution Workflows, CLI Tooling, and Practical Integration Examples

So, you’ve memorized the spec, you’ve got your lib_bejson_Core libraries imported, and you’re still writing spaghetti code to move data around. Stop. The whole point of building this ecosystem was to automate the tedious garbage so you wouldn't have to manually validate row counts or mangle JSON paths.

This chapter is about putting the pieces together. If you aren't using the core CLI helpers, you’re doing it the "noob way"—manually, error-prone, and slow.

The Standardized Execution Flow

A professional workflow for BEJSON isn't just about reading a file; it’s about a rigid pipeline: Validate → Transform/Chunk → Secure → Distribute.

If you are writing a script that modifies a database without calling bejson_safe_join or skipping the validator before a critical push, you are a liability to your own project. Every production pipeline in this ecosystem follows this cycle:

  1. Ingestion & Validation: Never touch raw bytes. Pass them through lib_bejson_Core_mfdb_validator or the relevant validate104() method.
  2. Context-Aware Modification: If you’re changing record values, use the indexed approach. Don't iterate key-value pairs like you're playing with a CSV.
  3. Atomic Serialization: Use os.replace or os.rename via the chunking engine. Never write directly to a live 104a.mfdb.bejson file.

CLI Integration Examples

The lib_bejson_Core family isn't just for imported modules. The most efficient way to use these is via lightweight wrapper scripts that act as your CLI interface.

1. The "Clean-Room" Chunking Pipe (Python)

This script is the gold standard for packaging your source code into a chunked artifact. It handles the Package_Version bumping automatically so you don’t have to track it manually.

import sys
from lib_bejson_Core_bejson_chunking import (
    bejson_core_chunking_create_chunked_104,
    bejson_core_chunking_bump_package_version
)

def run_pipeline(source_dir, output_file):
    # Always bump version to prevent stale cache artifacts
    # Use the existing doc to increment Package_Version
    new_doc = bejson_core_chunking_create_chunked_104(
        target_dir=source_dir,
        package_version="1" 
    )
    
    with open(output_file, 'w') as f:
        json.dump(new_doc, f, indent=2)
    print(f"Pipeline Complete: {output_file} generated.")

if __name__ == "__main__":
    run_pipeline(sys.argv[1], sys.argv[2])

2. Bash-Native Integrity Audit

Sometimes you don't have Python access. If you’re auditing a remote server, use this snippet to verify that the manifest is actually pointing to real files.

#!/bin/bash
# Quick Audit: Verify every file_path in the manifest exists
MANIFEST="104a.mfdb.bejson"

# Grab all file_paths (index 1 in the Fields array)
PATHS=$(cat $MANIFEST | jq -r '.Values[][1]')

for p in $PATHS; do
    if [ ! -f "$p" ]; then
        echo "CRITICAL: Orphaned file detected: $p"
        exit 1
    fi
done
echo "Integrity Check Passed."

Practical Integration: The "Always-Bump" Logic

The biggest mistake noobs make is reusing chunked artifact filenames without updating the internal Package_Version. If your downstream pipeline consumes an old chunk with a new File_Version, your entire state machine will be out of sync.

Always implement the Always-Bump rule. When you call the chunker, you should be checking for an existing document:

# The "Always-Bump" Protocol
def secure_write_chunk(doc, out_path):
    # Extract prior version if exists
    prior = None
    if os.path.exists(out_path):
        with open(out_path, 'r') as f:
            prior = json.load(f)
            
    doc["Package_Version"] = bejson_core_chunking_bump_package_version(prior)
    
    with open(f"{out_path}.tmp", 'w') as f:
        json.dump(doc, f)
    os.rename(f"{out_path}.tmp", out_path)

Why This Workflow Matters

  1. Deterministic State: Because we use SHA-256 in the File_Hash (as of LIB-C2), and we enforce Package_Version bumping, you can cryptographically verify if a chunk has changed without parsing the entire content.
  2. Path Traversal Prevention: By routing everything through bejson_safe_join, we kill the most common attack vector in CMS systems—arbitrary file writes.
  3. Atomic Integrity: Writing to .tmp and swapping with os.rename is non-negotiable. If your process crashes during a write, the old file remains perfectly valid. If you were writing directly to the manifest, you'd be staring at a corrupted, invalid JSON blob, and it would be entirely your fault.

Stop looking for shortcuts. The libraries are built to handle the I/O, the pathing, and the schema versioning. Your only job is to hook them together correctly. If you're struggling to implement these workflows, go back to Chapters 2 and 3 and read them again until the positional index access becomes muscle memory.


BEJSON 104 - Core SH/PY Libraries • Leethaxor69

© 2026 Leethaxor69. All rights reserved. • github.com/boehnenelton

Elton Boehnen · boehnenelton2024@gmail.com · boehnenelton2024.pages.dev · github.com/boehnenelton
leethaxor69
Article Author

leethaxor69

Elite Security Researcher & Autonomous Systems Engineer


Related Content