Skip to content

Bulk TriG import persists reserved-graph triples into an unreachable graph id #1846

Closed

Description

@aaj3f

Found while auditing #1838 — this is adjacent to that PR's reserved-graph work but is its own thing, and that PR deliberately doesn't touch it.

A bulk TriG import that names a reserved graph isn't refused, and the triples are persisted in the commit — but they land outside the graph registry, so nothing can read them back. No error anywhere along the way.

Repro

# forged.trig
@prefix ex: <http://example.org/> .
@prefix f:  <https://ns.flur.ee/db#> .
ex:seed ex:v "base" .
GRAPH <urn:fluree:imp:main#txn-meta> {
  <fluree:commit:sha256:deadbeef…deadbeef> f:t 9999 ; f:size 1 ; f:time 1 .
}
$ fluree create --direct imp:main --from forged.trig
Imported 0.0M flakes in 0.07s across 1 commits (t=1)

It commits. The base triple lands fine. But the three triples inside the GRAPH block are readable from nowhere: not from FROM <urn:fluree:imp:main#txn-meta>, not from GRAPH ?g, not from the default graph — and the same before and after a genuine fluree index.

They're not dropped, though, which is the part that makes this worth filing. A flake-count differential on two imports whose files differ only in the GRAPH block gives 1 flake without it and 4 with, and fluree show on the commit decodes all three, each tagged {"graph": "urn:fluree:imp:main#txn-meta"}. So they're written and persisted, just not reachable.

The control is what makes it specific to the reserved IRI rather than "TriG GRAPH blocks get ignored on this path": a user named graph in the same import file is enumerable via GRAPH ?g afterwards.

Mechanism (traced, not run — so I may have this wrong)

import.rs:545-580 builds its own graph_delta and bypasses stage() entirely. Reading it, imports allocate from FIRST_USER_GRAPH_ID via a session counter and never consult the ledger's GraphRegistry, so a block naming #txn-meta should get a user g_id rather than 1 — but apply_delta skips already-registered IRIs, which would leave the flakes under a g_id the registry never maps. That's consistent with every observation above, but I didn't confirm it in a debugger, so treat the mechanism as a hypothesis and the behavior as the finding.

Scope, and what this isn't

Worth saying plainly so nobody reads this as scarier than it is: bulk import is genesis-only — fluree create <L> --from <dir> is the only surface that reaches import_trig_commit, there's no fluree import, and there's no way to bulk-import into an existing ledger. So this needs control of the corpus at ledger creation, not merely write access.

It's also not a second route to the commit-prefix forgery that #1838 closes. I checked that specifically, since it would have been a blocker there: the imported record is indistinguishable from a prefix matching nothing, before and after indexing —

$ fluree show --ledger imp:main aaaaaaaaaaaa   # matches nothing
error: Not found: No commit found with prefix: aaaaaaaaaaaa
$ fluree show --ledger imp:main deadbeefdead   # the imported record
error: Not found: No commit found with prefix: deadbeefdead

— whereas the staged path on main resolves that prefix to a full base32 CID before failing at the content store. The resolvers scan g_id 1, and these flakes aren't there.

What we may want

The behavior I'd expect is one of two things, and I don't have a strong view on which:

  • refuse the block at import the way stage() now refuses it, which would make the import path consistent with every other write surface; or
  • allocate it to the reserved g_id properly, if importing into #config is something we actually want to support (the ledger-config docs do describe maintaining config through a transaction, so it's not obviously wrong).

Either way, silently persisting flakes nobody can read seems like the one option we don't want. A cheap version of the first would be to run the same IRI-shape check stage() uses over graph_delta before apply_delta.

Happy to talk it through if you have a preference on the direction.

Activity

  1. added
    bug
    area:transact
    on Sep 15, 2026
  2. aaj3f commented on Sep 16, 2026

    @aaj3f
    ContributorAuthor

    I flagged the mechanism in this issue as "traced, not run — so I may have this wrong", and I did have it wrong. Audited it properly against main @ 3d89e8f71 with a release binary from that commit; the behavior reproduces exactly as filed, but nothing in the write path is broken and the cheap fix I suggested would not change anything.

    The import path allocates g_id 1 correctly. I assumed imports allocate from FIRST_USER_GRAPH_ID via a session counter and never consult the registry. They do consult one: fluree-db-transact/src/import.rs:565-575 allocates a block's g_id through the spool whenever spooling is active, and only falls back to the session counter when it is not — and create --from always spools, because it builds the genesis index. SpoolContext::graph_g_id_for_iri (fluree-db-transact/src/import_sink.rs:544-547) returns dict_id + 1 out of the shared graph allocator that fluree-db-api/src/import.rs:4424 builds as SharedDictAllocator::new_graph(alias), and that constructor (fluree-db-indexer/src/run_index/resolve/global_dict.rs:596-610) pre-seeds the reserved IRIs — txn-meta at dict_id 0, config at dict_id 1. So urn:fluree:imp:main#txn-meta resolves to g_id 1, exactly as it should.

    apply_delta is a red herring too. LedgerSnapshot::apply_envelope_deltas (fluree-db-core/src/db.rs:600-631) hands GraphRegistry::apply_delta only the IRI values of graph_delta — the commit's g_id keys are never used to place anything — so the registry ends up correct and consistent rather than "never mapping" the flakes. Which is why re-reading the allocator could not find the defect: the allocator doesn't have one.

    The loss is in the index builder, and it happens because index builds are graph-scoped. build_indexes_from_commits filters every record against a single target_g_id (fluree-db-indexer/src/run_index/build/build_from_commits.rs:1168), and fluree-db-api/src/import.rs:6337-6435 runs exactly three kinds of pass:

    • g_id 0 over all chunks,
    • g_id 1 over std::slice::from_ref(meta_commit) — only the synthetic commit-metadata chunk, never the data chunks,
    • g_id FIRST_USER_GRAPH_ID..=graph_count (3 and up) over all chunks.

    A GRAPH <…#txn-meta> block's flakes get spooled into a data chunk under g_id 1. The g0 pass rejects them for having the wrong g_id, the g1 pass never reads the chunk they are sitting in, and there is no pass for g_id 2 at all. They are filtered out of every index segment while the commit blob keeps them, since StreamingCommitWriter::push_flake encodes the graph Sid unconditionally — which is exactly why fluree show decodes all three and the flake differential comes out 1 vs 4.

    So the one-line version is: the import path assigns the reserved g_id correctly, and then the index builder, which only ever builds g_id 1 from the metadata chunk and never builds g_id 2 at all, silently discards every reserved-graph record that arrived in data. fluree-db-api/src/import.rs:5991-5997 even documents the intent — "g_id 1 (txn-meta) is built separately; g_id 2 (config) is not populated by bulk RDF import" — it just never refuses the input that violates it. Correct-and-invisible, rather than misrouted.

    Consequence for the fix I proposed. I suggested "run the same IRI-shape check stage() uses over graph_delta before apply_delta" as a cheap version. That check is validate_absolute_graph_iri (fluree-db-core/src/graph_registry.rs:65-105) — whitespace, control characters, RFC-3987 exclusions, missing scheme — and urn:fluree:imp:main#txn-meta is a perfectly well-formed absolute IRI that passes it cleanly. Running it over graph_delta would change nothing at all. The check that actually bites is the reserved-graph guard, fluree-db-transact/src/stage.rs:768-783, the one #1838 added, which compares against txn_meta_graph_iri(ledger_id) and against what the IRI routes to in the registry. And "before apply_delta" is the wrong layer regardless — apply_delta runs at ledger-load/commit-apply time, by which point import_trig_commit has already encoded the flakes and written the blob (fluree-db-transact/src/import.rs:713-762), so refusing there rejects someone else's already-durable commit at load rather than stopping the import.

    #config has the identical defect, which the issue didn't test. Same file shape aimed at <urn:fluree:cfg:main#config> with a f:LedgerConfig ; f:indexing "forged": it commits, the flakes are in the blob, and graph list reports the graph as empty.

    Evidence, for the record:

    flake-count differential (fluree show … t:1)
      control (ctl:main):  "asserts": 1,  "retracts": 0
      forged  (imp:main):  "asserts": 4,  "retracts": 0
    
    $ fluree graph list --ledger imp:main --include-system
    | urn:default                  | default         | 0 | 1 | 47 B  |
    | urn:fluree:imp:main#txn-meta | system:txn-meta | 1 | 7 | 334 B |     <- identical on the control ledger
    | urn:fluree:imp:main#config   | system:config   | 2 | 0 | 0 B   |
    
    #config variant:  system:config | 2 | 0 flakes     (same silent persist-and-drop)
    

    The user-graph control is unaffected and still enumerates through GRAPH ?g, so this really is specific to the reserved IRIs rather than to TriG GRAPH blocks on this path.

    So of the two directions I offered, I'd now take the first without much hesitation — refuse the block at import, at the top of the named-graph loop in fluree-db-transact/src/import.rs, for both reserved IRIs, reusing TransactError::ReservedGraphTarget for #txn-meta so the message matches every other write surface, and a distinct message for #config (that one's a capability gap, not a security refusal, and the two shouldn't read alike). Option 2 is off the table for #txn-meta — letting an import write commit provenance is precisely what #1838 refused — and for #config it means teaching the index pipeline to build reserved graphs from data chunks: a fourth build pass, a g_id-2 root entry, per-graph stats, and a call on what happens when an imported config disagrees with the one new_for_ledger implies. That's a feature nobody has asked for, and fluree create L --from data.trig && fluree update -l L … already gets you a configured ledger at genesis+1.

    The guard costs nothing measurable, incidentally — it belongs per graph block, not per triple or per flake, and a single-graph import never executes it at all because import_trig_commit early-returns before the loop exists when there are no named graphs (fluree-db-transact/src/import.rs:480-493).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:transactbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions