Skip to content

feat(torrent): validate v2 documents and hybrid layouts - #173

Closed
linroid wants to merge 2 commits into
torrent-v2-04-infofrom
torrent-v2-04-document
Closed

linroid wants to merge 2 commits into
torrent-v2-04-infofrom
torrent-v2-04-document

Conversation

@linroid

@linroid linroid commented Sep 12, 2026

Copy link
Copy Markdown
Owner

Require document-level piece-layer validation for bounded v2 imports and validate the v1 layout when a document is hybrid. Imported metainfo must include its required authenticated layers; info-only BEP 9 responses cannot pass as complete documents.

Part of #162, stacked on #172 (torrent-v2-04-info). Hybrid validation requires identical raw filenames, order, lengths and piece alignment, checks v1 hash counts, and retains original v1 file indices. Padding becomes virtual zero spans; omitted padding paths and optional trailing padding are supported. Partial/conflicting layouts and symlinks are rejected.

Validation: all 342 local torrent tests passed with none skipped, both pinned-client conformance scenarios executed, and the native-runtime dependency guard passed. Coverage includes independent envelope/info/layer bounds, corrupt or missing layers, empty/small files, single/multi-file hybrids, and incorrect names/order/lengths/alignment/hash counts.

This is the bounded in-memory import path. Large streaming/spilled imports, safe output mapping, runtime routing, and dual payload verification still need integration before v2/hybrid download support is advertised. Metadata consistency does not prove that payload matches both hash schemes.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-12T14:04:51.837131Z 3ba4a64 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3ba4a64886

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +42 to +44
val nodes = requireNotNull(envelope["piece layers"]?.dictionary) {
"Imported metainfo requires a piece layers dictionary"
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Accept omitted piece-layer dictionaries for small torrents

When a valid v2 metainfo contains no file larger than pieceLength, BEP 52 permits piece layers to be omitted, and validatePieceLayers(emptyMap()) can already authenticate that case. This requireNotNull rejects such documents before validation, so empty, one-byte, and single-piece torrents cannot be imported unless a producer injects an unnecessary empty dictionary. Treat a missing field as an empty map while continuing to reject missing layers when the file tree requires them.

AGENTS.md reference: AGENTS.md:L242-L245

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current BEP 52 explicitly says a torrent is invalid when this field is absent (lines 93–106): https://github.com/bittorrent/bittorrent.org/blob/master/beps/bep_0052.rst#L93-L106 . I am retaining the required dictionary check, including for small torrents. An empty dictionary is accepted for files that need no external layers; info-only magnet metadata follows a separate path. Relaxing document import here would conflict with the specification.

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

  533 files  +10    533 suites  +10   3m 49s ⏱️ -2s
2 419 tests +24  2 419 ✅ +24  0 💤 ±0  0 ❌ ±0 
3 101 runs  +40  3 101 ✅ +40  0 💤 ±0  0 ❌ ±0 

Results for commit 3ba4a64. ± Comparison against base commit 6818eff.

@linroid

linroid commented Sep 20, 2026

Copy link
Copy Markdown
Owner Author

Superseded by #232 (V2 identity, metainfo and Merkle integrity) in the consolidated eight-PR stack for #162.

The replacement preserves this PR's code and subsequent fixes; its description links the original head and review discussions. The only additional consolidation fix is the CI-discovered listener cancellation regression, documented on the replacement. All eight replacements have passed their required CI checks.

Closing without merging. This branch and its discussions remain available for reference. Review index: #162 (comment)

@linroid linroid closed this Sep 20, 2026
linroid added a commit that referenced this pull request Sep 20, 2026
Consolidates #170–#173, preserving source 3ba4a64.
Includes the foundation listener cancellation regression fix discovered by consolidation CI.
@linroid
linroid deleted the torrent-v2-04-document branch September 20, 2026 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant