Skip to content

feat(lua): add helpers to escape strings for the Lua config - #399

Open
Mirilindo wants to merge 1 commit into
hyprland-community:masterfrom
Mirilindo:lua-escaping
Open

Mirilindo wants to merge 1 commit into
hyprland-community:masterfrom
Mirilindo:lua-escaping

Conversation

@Mirilindo

Copy link
Copy Markdown

Hyprland's Lua config makes every dispatcher and config value a Lua snippet, and a consumer that
wants to work on 0.55 and later has to build those snippets itself today, since the crate still
emits the old syntax (that's #391). Every string interpolated into one — a workspace name, a
monitor description, a command to run — goes through a Lua string literal, and the crate has no way
to escape one. So consumers write their own:

  • hyprland-autoname-workspaces#138
    adds an escape_lua_string() of its own and passes hand-built Lua to DispatchType::Custom,
    because DispatchType::RenameWorkspace no longer has any effect.
  • A pending PR against H3rmt's lua branch (H3rmt/hyprland-rs#1,
    by @mitsaucepls) adds window.move and writes its quotes by hand as well, so the same escaping is
    now hand-rolled in two independent places.

This adds hyprland::lua, so the escaping exists once, in the library:

pub fn escape_string(value: &str) -> String  // escaped contents, no quotes
pub fn quote_string(value: &str) -> String   // the complete "..." literal

Both escape \, " and the ASCII control characters; everything else, non-ASCII text included, is
left untouched.

Why the control characters are zero padded

\n, \r and \t keep their mnemonic escape. The rest become \ddd, which is a byte, and Lua
reads up to three digits — so without the padding the escape swallows a digit of the string itself.
Measured on 0.56.2:

$ hyprctl eval 'local s = "a\11b"   error("len=" .. #s)'
error: [string "local s = "a\11b"   error("len=" .. #s)"]:1: len=3
$ hyprctl eval 'local s = "a\0011b" error("len=" .. #s)'
error: [string "local s = "a\0011b" error("len=" .. #s)"]:1: len=4

The caller passed four bytes in both cases.

How it was checked

I generated the strings from this code rather than typing Lua by hand, then round-tripped each one
through a running Hyprland 0.56.2, asking Lua for the bytes it actually parsed so that no binary
data has to survive the IPC round trip:

hyprctl eval 'local s = <literal> error(table.concat({string.byte(s, 1, #s)}, ","))'

403 strings, every one byte-identical to the input: each of the 128 ASCII codepoints alone,
surrounded (a<c>b) and followed by a digit (a<c>1b); the seven cases of #138; U+0085, U+2028,
U+2029, ZWJ and U+FEFF; trailing backslashes; an apostrophe; and injection payloads. The negative
control is the reason for all of this — interpolated naively, a name of a" error("PROOF") -- runs
inside the compositor; through quote_string the same name is inert data.

The unit tests reuse the cases of
#138 verbatim, so it
is easy to check this covers what the copy in that PR does — plus the remaining control characters,
which it leaves alone.

Scope

Additive: one module, two functions, no new dependency, no existing behaviour changed (three added
lines in lib.rs declare the module), and no feature gate — the escaping is needed with or without
dispatch. ./test.sh passes, and cargo clippy --all-targets and
cargo doc --no-deps --all-features report exactly what they report on master.

This deliberately does not touch dispatch itself. That is the larger question of #391, and this
helper is useful whichever way it is answered. If this one looks right to you, I'm happy to follow
up with the dispatchers themselves emitting Lua.

One note: the README's "This crate provides 6 modules" list is already out of date on master (it
omits error, instance, hyprpaper and others), so I left it alone rather than half-fix it.
Happy to update the whole list in a separate commit if you'd like.

🤖 Generated with Claude Code

Hyprland's Lua config makes every dispatcher and config value a Lua snippet, so
any string interpolated into one — a workspace name, a monitor description, a
command to run — goes through a Lua string literal. The crate has no way to
escape one, so its consumers write theirs: hyprland-autoname-workspaces#138
carries an `escape_lua_string()` of its own and hands the Lua it built by hand
to `DispatchType::Custom`. Without such escaping, a stray `"` in a name breaks
the snippet, and a crafted one runs arbitrary Lua inside the compositor.

Add `hyprland::lua` with `escape_string()` and `quote_string()`. Both escape
`\`, `"` and the control characters — those without a mnemonic escape as zero
padded `\ddd`, since an unpadded `\1` swallows a following digit of the string
itself and turns `"a\11b"` into three bytes where the caller passed four.
Everything else — non ASCII text included — is left untouched, `'` among it:
the output belongs in a `"..."` literal, which is what `quote_string()` builds.

The unit tests reuse the cases of autoname-workspaces#138. Those cases, and 403
strings generated from this code in total, were round-tripped through a running
Hyprland 0.56.2 by asking Lua for the bytes of the string it parsed
(`string.byte`): every one came back identical to the input.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Mirilindo

Copy link
Copy Markdown
Author

@yavko a gentle ping on this one. Hyprland 0.57.0 was released on October 28 with
hyprwm/Hyprland#15539: hyprland.conf is no longer parsed, the dispatch command only takes
Lua, and keyword is gone from the IPC. So on 0.57, every DispatchType the published crate
sends is rejected, except Custom when the caller writes the Lua by hand (as
hyprland-community/hyprland-autoname-workspaces#138 does), and Keyword::set has nothing left
to talk to.

Two things that may help:

  • The CI never ran on this PR: the run waited for a maintainer to approve it, and GitHub closed
    it without starting any job. Locally, against current master, the workflow's steps pass
    (cargo clippy --all-targets, cargo doc --no-deps --all-features, ./test.sh).
  • The follow-up is written and tested on my side: Dispatch::call sends Lua, with a Lua form
    for every variant called by the crates on crates.io that depend on hyprland,
    RenameWorkspace (dispatches do no work on hyprland 0.55 #391) included (Custom aside: it sends whatever the caller wrote). It uses quote_string from this PR, so I'll open it once
    this one is merged.

Thanks for keeping the door open.

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