Skip to content

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

Closed

Description

@momothefox

Arabic SSIDs (e.g., محمد, محمدين وحسنين) configured via OpenWISP 2 and netjsonconfig on OpenWrt are truncated or corrupted, missing characters (e.g., محمد appears as م ح م). Adding spaces or multiple apostrophes (') after the SSID partially resolves the issue, but requires an excessive number of apostrophes (e.g., 10 for محمدين وحسنين) to display correctly, indicating a bug in encoding, parsing, or rendering.

Environment:
OpenWISP 2 web interface, using netjsonconfig backend for OpenWrt.
OpenWrt device (e.g., with Qualcomm Atheros QCA9880 802.11ac and QCA9550 802.11bgn, as shown in the wireless scan screenshot).
Configuration applied successfully (status: "applied," as shown in the OpenWISP Configuration screenshot).
Configuration:
Navigate to the OpenWISP Configuration page for a device.
Set the wireless SSID in the netjsonconfig JSON or UCI format to an Arabic string, such as:
option ssid 'محمد'
option ssid 'محمدين وحسنين'
Observations:
Initially, option ssid 'محمد' results in truncation, displaying only م ح م (missing د) in the UCI configuration and wireless scan (see Hex Dump and Wireless Scan screenshots).
Adding a single space after محمد (e.g., option ssid 'محمد ') displays the full محمد in the OpenWISP interface but not consistently in the UCI or wireless output.
Adding a single apostrophe after محمد (e.g., option ssid 'محمد') does not work; it requires two apostrophes (e.g., option ssid 'محمد'') to parse correctly in the UCI and wireless scan.
For longer SSIDs like محمدين وحسنين (7 characters, 18 bytes in UTF-8), truncation worsens, requiring up to 10 apostrophes (e.g., option ssid 'محمدين وحسنين'''''''''') to display correctly.
Verification:
Check the UCI configuration file on the OpenWrt device (e.g., /etc/config/wireless) and wireless scan output (e.g., via iwinfo) to confirm truncation or corruption.
Use the OpenWISP interface to monitor the "Configuration status" (see Configuration screenshot, status: "applied").
Actual Behavior
Truncation:
The SSID محمد (UTF-8: D8 85 D8 AD D8 85 D8 AF, 10 bytes) is truncated to م ح م (UTF-8: D9 85 D8 AD D9 85, 8 bytes after the opening quote) in the UCI configuration, as shown in the Hex Dump screenshot.
Longer SSIDs like محمدين وحسنين (UTF-8: D9 85 D8 AD D9 85 D8 AF D9 8A D9 86 20 D9 88 D8 AD D8 B3 D9 86 D9 8A D9 86, 18 bytes) lose more characters, requiring multiple apostrophes to parse correctly.
Workaround:
Adding spaces or apostrophes after the Arabic SSID partially resolves the issue, but the number of apostrophes required grows with SSID length (e.g., 2 for محمد, 10 for محمدين وحسنين).
The wireless scan (see Wireless Scan screenshot) shows the SSID correctly only when the exact number of apostrophes is used, but this is impractical and error-prone.
Impact:
Arabic SSIDs are unusable or require manual, non-intuitive workarounds, affecting localization and usability on OpenWrt devices managed by OpenWISP 2.

Expected Behavior
The Arabic SSID should display correctly in both the OpenWISP interface, UCI configuration, and wireless scan without truncation or corruption.
For example:
option ssid 'محمد' should render as محمد (10 bytes) in the UCI and wireless output.
option ssid 'محمدين وحسنين' should render as محمدين وحسنين (18 bytes) without requiring additional spaces or apostrophes.
Image

Image
Image
Image
Image
Image

System Information:

Activity

  1. added theissue type on Feb 21, 2025
  2. momothefox commented on Jan 27, 2026

    @momothefox
    SponsorAuthor

    @nemesifier

    Description

    The _add_file method in netjsonconfig/backends/base/backend.py incorrectly uses len(contents) (character count) instead of the actual byte length when setting info.size for tarfile entries. This causes configuration files containing multi-byte UTF-8 characters (such as Arabic, Chinese, Japanese, etc.) to be truncated.

    Environment

    • netjsonconfig version: 1.2
    • Python version: 3.10
    • Operating System: Ubuntu 22.04 (OpenWISP Controller)
    • OpenWrt version: 23.05 (affected devices)

    Steps to Reproduce

    1. Create a device template with a WiFi SSID containing Arabic characters, e.g., واي فاي
    2. Assign the template to a device
    3. Download the configuration tarball from the controller
    4. Extract and examine the wireless config file

    Expected Behavior

    The wireless configuration file should contain the complete SSID:

    option ssid 'واي فاي'
    

    Actual Behavior

    The file is truncated mid-character:

    option ssid 'و�
    

    UCI parser fails with:

    uci: Parse error (EOF with unterminated ') at line 34, byte 17
    

    OpenWISP agent logs show:

    daemon.crit openwisp: ERROR: invalid UCI configuration file: wireless
    

    Root Cause Analysis

    The bug is in netjsonconfig/backends/base/backend.py, line 288:

    def _add_file(self, tar, name, contents, mode=DEFAULT_FILE_MODE):
        byte_contents = BytesIO(contents.encode("utf8"))
        info = tarfile.TarInfo(name=name)
        info.size = len(contents)  # BUG: This is CHARACTER count, not BYTE count!
        # ...
        tar.addfile(tarinfo=info, fileobj=byte_contents)

    For ASCII text, len(contents) equals len(contents.encode('utf8')), so the bug is not visible. However, for multi-byte UTF-8 characters:

    String len(string) len(string.encode('utf8'))
    "hello" 5 5
    "واي فاي" 7 13
    "中文" 2 6

    When the tarfile is extracted, Python's tarfile module reads only info.size bytes, causing the file to be truncated.

    Demonstration

    test_str = "واي فاي"
    print(f"Character length: {len(test_str)}")        # 7
    print(f"Byte length: {len(test_str.encode('utf8'))}")  # 13
    # Difference: 6 bytes per SSID × 2 radios = 12 bytes missing minimum

    In our case, the wireless config was 31 bytes short (940 expected, 909 actual), causing the file to end mid-UTF-8 character sequence.

    Proposed Fix

    --- a/netjsonconfig/backends/base/backend.py
    +++ b/netjsonconfig/backends/base/backend.py
    @@ -285,7 +285,7 @@ class BaseBackend:
             """
             byte_contents = BytesIO(contents.encode("utf8"))
             info = tarfile.TarInfo(name=name)
    -        info.size = len(contents)
    +        info.size = len(byte_contents.getvalue())
             # mtime must be 0 or any checksum operation
             # will return a different digest even when content is the same
             info.mtime = 0

    Or alternatively:

    info.size = byte_contents.getbuffer().nbytes

    Impact

    This bug affects any OpenWISP deployment using non-ASCII characters in configuration values, including:

    • WiFi SSIDs in Arabic, Chinese, Japanese, Korean, etc.
    • Location names with accented characters
    • Any template variable containing UTF-8 multi-byte characters

    The bug has likely existed since the codebase was created, as it only manifests with non-ASCII text.

    Workaround

    Until a fix is released, users can manually patch the installed package:

    sed -i 's/info.size = len(contents)/info.size = len(byte_contents.getvalue())/' \
      /path/to/env/lib/python3.x/site-packages/netjsonconfig/backends/base/backend.py

    Then restart the OpenWISP services.


  3. moved this from To do (general) to To do (Python & Django) in OpenWISP Contributor's Boardon Jan 28, 2026
  4. moved this from To do (Python & Django) to To do (documentation) in OpenWISP Contributor's Boardon Jan 28, 2026
  5. moved this from To do (documentation) to To do (Python & Django) in OpenWISP Contributor's Boardon Jan 28, 2026
  6. Ayushk-05 commented on Sep 10, 2026

    @Ayushk-05

    Hi, I'd like to work on this issue. Is it still available?

  7. amanbn8 commented on Oct 3, 2026

    @amanbn8
    Contributor

    Hi, I'd like to take this one. I reproduced it on current master and it is exactly what @momothefox described above: BaseBackend._add_file writes the UTF-8 encoded contents into the archive but sets the tar member size to len(contents), i.e. the character count, so anything with multi-byte characters gets cut short on extraction (an SSID like محمدين وحسنين ends up as محمدين و in /etc/config/wireless). I have the one-line fix and a regression test ready and will open a PR shortly.

  8. openwisp-companion commented on Oct 3, 2026

    @openwisp-companion

    This issue has been automatically assigned to @amanbn8 who opened PR #419 to address it. 🎯

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

Metadata

Metadata

Assignees

Labels

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions