Skip to content

Stega decoding fails when original content ends with one or more zero-width space characters #1202

@felthy

Description

@felthy

Describe the bug

If content pasted into the Sanity studio contains trailing characters such as U+200B ZERO WIDTH SPACE, that field will fail to be editable in the Presentation Tool and an error will be shown in the console such as:

Failed to decode stega for string: (...) with the original error:  Error: Encoded data has invalid length

or

Failed to decode stega for string: (...) with the original error:  SyntaxError: Unexpected token '(...)', "(...)"... is not valid JSON

We've experienced this problem organically with content that our authors pasted into the studio from other sources.

To Reproduce

Steps to reproduce the behavior:

  1. Paste the text between the quotes here - "foo​" - into a field in the Sanity Studio. (Note: there is a single zero-width space character after the letter o. The string could also be represented in javascript as 'foo.\u200b'.)
  2. Open the document in the Presentation Tool
  3. Notice that the field is not outlined for editing
  4. Notice the Failed to decode stega error in the console.

Alternatively, the following test can reproduce the error inside this repo:

import {expect, test} from 'vitest'

import {stegaCombine,stegaDecode} from '../../src/stega/stega'

test('it handles trailing zero-width space characters', () => {
  const payload = 'foo.\u200b'
  const editInfo = JSON.stringify({origin: 'sanity.io', href: '/studio'})
  const encoded = stegaCombine(payload, editInfo)
  expect(encoded).not.toEqual(payload)
  const decoded = stegaDecode(encoded)
  expect(decoded.origin).toEqual('sanity.io')
  expect(decoded.href).toEqual('/studio')
})

Expected behavior

The original text should survive stega encoding and decoding without errors, and visual editing should work as normal.

Which versions of Sanity are you using?

@sanity/cli (global)               6.1.5 (latest: 6.1.7)
@sanity/cross-dataset-duplicator   1.5.1 (up to date)
@sanity/icons                      3.7.4 (up to date)
@sanity/orderable-document-list    1.4.2 (latest: 1.5.1)
@sanity/ui                        3.1.13 (latest: 3.1.14)
@sanity/vision                    5.16.0 (up to date)
sanity                            5.16.0 (up to date)
@sanity/eslint-config-studio       5.0.2 (latest: 6.0.0)

What operating system are you using?

macOS 26.3.1

Which versions of Node.js / npm are you running?

11.3.0
v24.1.0

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions