Skip to content

Automatically compute content_hash for uploads#538

Open
AndreyVMarkelov wants to merge 1 commit into
mainfrom
auto-content-hash
Open

Automatically compute content_hash for uploads#538
AndreyVMarkelov wants to merge 1 commit into
mainfrom
auto-content-hash

Conversation

@AndreyVMarkelov

Copy link
Copy Markdown
Contributor

Summary

Computes the Dropbox content hash of upload payloads and sends it with the request so the server verifies upload integrity, matching the behavior of dropbox-sdk-go #140.

  • dropbox/content_hash.py (new) — DropboxContentHasher / StreamHasher from dropbox-api-content-hasher, adapted for Python 3 (no six), plus a content_hash() helper.
  • auto_content_hash constructor flag (default True) to opt out. Preserved across clone(), as_user()/as_admin(), and with_path_root().
  • Transport fills the content_hash field on upload routes when the caller hasn't supplied one and the payload is bytes. Applies generically to all upload-style routes with a content_hash field (files_upload, files_alpha_upload, upload_session_start/append/append_v2/finish).

No public API change: content_hash was already an optional field on the upload methods; this just fills it in. The logic lives in the hand-written transport so it survives client regeneration, and the caller's argument object is never mutated (a copy is made).

Tests

  • test/unit/test_content_hash.py — hasher algorithm, chunked updates, copy() independence, reuse guard, non-bytes rejection, StreamHasher passthrough.
  • test/unit/test_dropbox_unit.py — auto on/off, caller-provided hash respected, flag propagation across clone/team/path-root.
  • test/integration/test_dropbox.py::test_upload_auto_content_hash — asserts the server-returned content_hash matches the locally computed one (end-to-end).

Unit suite: 56 passed; flake8 clean.

Note

The end-to-end behavior (server accepting and verifying the hash) is only exercised by the integration test, which requires live credentials and runs in CI's Integration job.

@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 58.58586% with 41 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.13%. Comparing base (620b8be) to head (36dcc3c).

Files with missing lines Patch % Lines
dropbox/content_hash.py 53.01% 35 Missing and 4 partials ⚠️
dropbox/dropbox_client.py 87.50% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #538      +/-   ##
==========================================
- Coverage   65.14%   65.13%   -0.01%     
==========================================
  Files          31       32       +1     
  Lines       54902    55001      +99     
  Branches     3901     3911      +10     
==========================================
+ Hits        35767    35827      +60     
- Misses      18981    19016      +35     
- Partials      154      158       +4     
Flag Coverage Δ
integration 65.00% <54.54%> (-0.02%) ⬇️
unit 64.70% <56.56%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Compute the Dropbox content hash of upload payloads and send it so the
server verifies integrity, matching dropbox-sdk-go (#140).

- Add dropbox/content_hash.py (DropboxContentHasher / StreamHasher from
  dropbox-api-content-hasher, adapted for Python 3) with a content_hash()
  helper.
- Add an auto_content_hash constructor flag (default True) to opt out;
  preserved across clone(), as_user()/as_admin(), and with_path_root().
- In the transport, fill the content_hash field on upload routes when the
  caller hasn't and the payload is bytes. content_hash was already an
  optional field, so there is no public API change; the logic lives in the
  hand-written transport so it survives client regeneration.
- Add unit tests for the hasher and the auto/opt-out behavior, and an
  integration test asserting the server-returned hash matches.
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