Skip to content

[fix] Fixed truncation of non-ASCII file contents in generated tar #341 - #419

Merged
nemesifier merged 2 commits into
openwisp:masterfrom
amanbn8:issues/341-utf8-tar-size
Oct 3, 2026
Merged

nemesifier merged 2 commits into
openwisp:masterfrom
amanbn8:issues/341-utf8-tar-size

Conversation

@amanbn8

@amanbn8 amanbn8 commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Checklist

Reference to Existing Issue

Closes #341.

Description of Changes

Any non-ASCII text in a generated config file comes out truncated in the tar.gz produced by generate(). The reporter hit this with Arabic SSIDs on OpenWrt: /etc/config/wireless ends up cut off in the middle of the SSID and uci then refuses to parse the file.

The cause is in BaseBackend._add_file, as momothefox already worked out in the issue thread. It writes contents.encode('utf8') into the archive but sets the tar member size to len(contents), which is the number of characters, not the number of bytes. For plain ASCII the two are equal, so nothing looked wrong until someone used multi-byte characters. Everything past the declared size is silently dropped when the archive is extracted.

The fix encodes the contents once and uses the length of the encoded bytes for info.size.

I added test_generate_multibyte_contents to the OpenWrt backend tests. It generates an archive with an Arabic SSID and a files entry containing CJK and accented characters, then checks that the extracted wireless config contains the whole SSID and that the extra file's declared size and contents match its UTF-8 bytes. The test fails on master and passes with this change.

Tested locally on Python 3.13 with the full suite (coverage run runtests.py, 629 tests) and ./run-qa-checks. I also checked it by hand with a small script that generates the archive and reads the members back: before the fix the wireless member was declared as 257 bytes and ended at option ssid 'محمدين و, after the fix it is 269 bytes and contains the full SSID.

Screenshot

N/A

…enwisp#341

The size of each tar member was set to the number of characters in the
file contents instead of the length of its UTF-8 encoding, so any file
containing multi-byte characters (for example an Arabic SSID in
/etc/config/wireless) was cut short when the archive was extracted.

Closes openwisp#341
@coderabbitai

coderabbitai Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

🧰 Additional context used
📚 Code guidelines (1)
AGENTS.md — auto-discovered

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: c783e67e-c14b-4009-a2b0-ac00ca2fe16a
📥 Commits

Reviewing files that changed from the base of the PR and between a35174c and 5887b0a.

📒 Files selected for processing (1)
  • tests/openwrt/test_backend.py

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

📜 Recent review details
🧰 Additional context used
📓 Path-based instructions (3)
Ensure tests cover relevant success, error, boundary, and unusual input scenarios.

⚙️ CodeRabbit configuration file

Files:

  • tests/openwrt/test_backend.py
Flag potential security vulnerabilities Flag obvious performance regressions, such as heavy loops, repeated I/O, or unoptimized queries Flag unused or redundant code Flag outdated or incorrect comments/docstrings Ensure new code handles err...

⚙️ CodeRabbit configuration file

Files:

  • tests/openwrt/test_backend.py
Source excerpt: Keep tests quiet on success.

📄 CodeRabbit inference engine (AGENTS.md)

Files:

  • tests/openwrt/test_backend.py
🔇 Additional comments (1)
tests/openwrt/test_backend.py (1)

188-188: LGTM!

Also applies to: 190-194


📝 Walkthrough

Walkthrough

BaseBackend._add_file now sets the tar entry size to the byte length of its UTF-8-encoded contents. A new OpenWrt test generates an archive with a non-ASCII SSID and checks that the decoded wireless configuration contains it.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: nemesifier

Merge Risk: ⚪ Minimal · up to 5887b

The archive now declares the byte length of its UTF-8 content, and the OpenWrt regression test checks that a non-ASCII SSID survives generation. No merge-blocking issue is evident.


Caution

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

  • Ignore

❌ Failed checks (1 error)

Check name Status Explanation Resolution
Ui Changes, Regression Test, Docs ❌ Error The PR fixes generated archive content, a behavior of the documented OpenWrt generate and additional-files features. The existing OpenWrt documentation describes archive generation and plain-text fi… Update the OpenWrt backend documentation to state how UTF-8 text is preserved in generated archive members. Include the relevant generated configuration or additional-file behavior, then include that documentation change in the PR.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title uses the required [fix] prefix and clearly describes the non-ASCII file-content truncation fix. It also references issue #341.
Description check ✅ Passed The description includes the checklist, issue reference, detailed change summary, test coverage and results, and a screenshot section marked N/A. It provides the required information and aligns with t…
Linked Issues check ✅ Passed Issue #341 requires Arabic SSIDs to remain intact in the generated OpenWrt configuration. BaseBackend._add_file now sets each tar member size to the length of its UTF-8 encoded contents. `test_gener…
Out of Scope Changes check ✅ Passed The PR changes tar member sizing in BaseBackend._add_file and adds a regression test for Arabic SSID preservation. Both changes support issue #341. No unrelated changes appear in the whole-PR diff.
Full details: Ui Changes, Regression Test, Docs

Explanation

The PR fixes generated archive content, a behavior of the documented OpenWrt generate and additional-files features. The existing OpenWrt documentation describes archive generation and plain-text file contents, but the PR changes only backend.py and the regression test; it does not update the documentation to describe UTF-8 content handling. The new test_generate_utf8 does provide a regression test. The change does not alter an end-user UI, so screenshots are not required.

  • Fix all pre-merge checks with AI
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot]
coderabbitai Bot previously approved these changes Oct 3, 2026
@coveralls

coveralls commented Oct 3, 2026 •

Copy link
Copy Markdown

Coverage Status

Coverage is 99.158% — amanbn8:issues/341-utf8-tar-size into openwisp:master. No base build found for openwisp:master.

@coderabbitai coderabbitai 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.

Pre-merge checks failed. Please resolve the failing checks before merging.

@openwisp-companion

Copy link
Copy Markdown

Proposed change log entry:

[fix] Fixed truncation of non-ASCII file contents in generated tar #341

Corrected tar member size calculation by using the length of UTF-8
encoded bytes instead of character length when adding files to the
generated archive. This prevents multi-byte characters like Arabic SSIDs
from being truncated during extraction.

Closes #341

@nemesifier
nemesifier merged commit 1feff92 into openwisp:master Oct 3, 2026
10 checks passed
@nemesifier

Copy link
Copy Markdown
Member

/backport 1.3

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

Development

Successfully merging this pull request may close these issues.

[bug] Arabic SSID Truncation in OpenWISP 2 with netjsonconfig on OpenWrt

3 participants