Skip to content

Commit

Permalink
fix(cgroups): improve escaping of systemd unit names
Browse files Browse the repository at this point in the history
WM_CLASS is loosely validated and can contain pretty much anything,
causing unit name validation failure on the systemd side. `app_id`
handler in sway can also be too permissive.
Escape everything that is not allowed in the systemd unit documentation.

Fixes #14
  • Loading branch information
alebastr committed Feb 21, 2022
1 parent 9ab24bd commit 083d613
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/assign-cgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import argparse
import asyncio
import logging
import re
import socket
import struct
from typing import Optional
Expand Down Expand Up @@ -46,10 +47,23 @@
# Launcher should only be listed here if it creates cgroup of its own.
LAUNCHER_APPS = ["nwgbar", "nwgdmenu", "nwggrid"]

SD_UNIT_ESCAPE_RE = re.compile(r"[^\w:.\\]", re.ASCII)


def escape_app_id(app_id: str) -> str:
"""Escape app_id for systemd APIs"""
return app_id.replace("-", "\\x2d")
"""Escape app_id for systemd APIs.
The "unit prefix" must consist of one or more valid characters (ASCII letters,
digits, ":", "-", "_", ".", and "\"). The total length of the unit name including
the suffix must not exceed 256 characters. [systemd.unit(5)]
We also want to escape "-" to avoid creating extra slices.
"""

def repl(match):
return "".join([f"\\x{x:02x}" for x in match.group().encode()])

return SD_UNIT_ESCAPE_RE.sub(repl, app_id)


LAUNCHER_APP_CGROUPS = [
Expand Down

0 comments on commit 083d613

Please sign in to comment.