Module: Agent::Lock::Text

Defined in:
lib/agent/lock/text.rb

Overview

Text read from a store, made safe to print.

A lock record is whatever some host sharing the store wrote, and a shared Redis lets any process that can reach it write one. Printed raw, its fields reach the operator's terminal and every log a harness keeps: an escape sequence in an agent ID can clear the screen, rewrite lines already printed, or show a link that goes somewhere else, and a scope with ; curl ... | sh in it turns a suggested alock break line into a command somebody pastes. So every field goes through Text.printable before it is printed, and through Text.shell when it lands inside a command. The record itself is never rewritten: release and break still have to match it exactly.

Constant Summary collapse

UNSAFE =

C0 and C1 control characters, DEL, and the Unicode marks that reorder how a line displays (the "Trojan Source" bidi overrides), which make a line read differently from the bytes it holds.

/[[:cntrl:]‎‏‪-‮⁦-⁩]/
BARE =

What a shell takes literally, so a value made only of these needs no quotes: lib/billing.rb stays as it is typed.

%r{\A[\w.,/:@%+=-]+\z}

Class Method Summary collapse

Class Method Details

.escape(char) ⇒ String

Returns \x1b below U+0100, \u{202e} above.

Parameters:

  • char (String) —

    one character

Returns:

  • (String) —

    \x1b below U+0100, \u{202e} above



55
# File 'lib/agent/lock/text.rb', line 55

def escape(char) = char.ord < 0x100 ? format("\\x%02x", char.ord) : format("\\u{%x}", char.ord)

.escape_bytes(bytes) ⇒ String

Returns each one as \xNN.

Parameters:

  • bytes (String) —

    bytes that are not valid UTF-8

Returns:

  • (String) —

    each one as \xNN



59
# File 'lib/agent/lock/text.rb', line 59

def escape_bytes(bytes) = bytes.bytes.map { |byte| format("\\x%02x", byte) }.join

.printable(value, lines: false) ⇒ String

Returns valid UTF-8 in which every unsafe character, and every byte that is not UTF-8, shows as an escape (\x1b, \u{202e}), so a planted sequence is visible to the operator instead of acted on.

Parameters:

  • value (#to_s) —

    a field read from a store

  • lines (Boolean) (defaults to: false) —

    keep newlines, for a multi-line body such as an intent with notes; any other control character is still escaped

Returns:

  • (String) —

    valid UTF-8 in which every unsafe character, and every byte that is not UTF-8, shows as an escape (\x1b, \u{202e}), so a planted sequence is visible to the operator instead of acted on



33
34
35
36
# File 'lib/agent/lock/text.rb', line 33

def printable(value, lines: false)
  text = value.to_s.dup.force_encoding(Encoding::UTF_8).scrub { |bytes| escape_bytes(bytes) }
  text.gsub(UNSAFE) { |char| lines && char == "\n" ? char : escape(char) }
end

.shell(value) ⇒ String

Returns printable, then single-quoted unless BARE, so it pastes as one argument. Single quotes rather than Shellwords.escape, which is just as safe but prints docs/** as docs/\*\*; and some quoting is needed, since docs/** pasted bare is a glob the shell expands into file names.

Parameters:

  • value (#to_s) —

    a field that lands inside a suggested command

Returns:

  • (String) —

    printable, then single-quoted unless BARE, so it pastes as one argument. Single quotes rather than Shellwords.escape, which is just as safe but prints docs/** as docs/\*\*; and some quoting is needed, since docs/** pasted bare is a glob the shell expands into file names.



48
49
50
51
# File 'lib/agent/lock/text.rb', line 48

def shell(value)
  text = printable(value)
  text.match?(BARE) ? text : "'#{text.gsub("'") { "'\\''" }}'"
end