Skip to content

fix: decrypt raises 'Ciphertext too short' on plaintext files shorter than 4 bytes#1163

Open
yc111233 wants to merge 1 commit intovolcengine:mainfrom
yc111233:fix/encryption-ciphertext-too-short
Open

fix: decrypt raises 'Ciphertext too short' on plaintext files shorter than 4 bytes#1163
yc111233 wants to merge 1 commit intovolcengine:mainfrom
yc111233:fix/encryption-ciphertext-too-short

Conversation

@yc111233
Copy link
Copy Markdown

@yc111233 yc111233 commented Apr 1, 2026

Problem

When encryption is enabled, FileEncryptor.decrypt() raises InvalidMagicError("Ciphertext too short") for plaintext files shorter than 4 bytes (including empty files).

This happens because the length check (len(ciphertext) < MAGIC_LENGTH) runs before the magic header check (ciphertext.startswith(MAGIC)). Empty or short plaintext files fail the length check before they can be recognized as unencrypted.

Root Cause

In append_file() (viking_fs.py), when a session's messages.jsonl is empty (0 bytes) — which happens after VK archives messages — the read-then-decrypt flow hits this bug:

  1. Reads 0-byte file → passes to _decrypt_content()
  2. decrypt() checks len(b'') < 4 → raises "Ciphertext too short"
  3. Never reaches the startswith(MAGIC) check that would return plaintext as-is

Fix

Swap the order of checks in decrypt():

# Before (bug):
if len(ciphertext) < MAGIC_LENGTH:
    raise InvalidMagicError("Ciphertext too short")
if not ciphertext.startswith(MAGIC):
    return ciphertext

# After (fix):
if not ciphertext.startswith(MAGIC):
    return ciphertext
if len(ciphertext) < MAGIC_LENGTH:
    raise InvalidMagicError("Ciphertext too short")

Testing

Verified that:

  • Empty bytes decrypt → returns empty bytes (no error)
  • Short plaintext (e.g. b'ab') → returns as-is
  • JSON plaintext → returns as-is
  • Encrypted round trip → works correctly
  • memory_store / memory_recall → work with encryption enabled

… than 4 bytes

The decrypt() method checked ciphertext length before checking the magic
header. This caused plaintext files shorter than 4 bytes (including empty
files) to raise InvalidMagicError('Ciphertext too short') before the
'is this plaintext?' check could return them as-is.

Fix: swap the order — check if content starts with OVE1 magic first,
then only check length for actual encrypted content.

This fixes failures when append_file() reads an empty messages.jsonl
session file and tries to decrypt it.
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 2026

Failed to generate code suggestions for PR

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


yc111233 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@baojun-zhang
Copy link
Copy Markdown
Collaborator

Thank you for your submission! We really appreciate it.
If possible, please supplement the unit tests for this scenario in https://github.com/volcengine/OpenViking/blob/main/tests/unit/crypto/test_encryptor.py .

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

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

3 participants