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.rbstays as it is typed. %r{\A[\w.,/:@%+=-]+\z}
Class Method Summary collapse
-
.escape(char) ⇒ String
\x1bbelow U+0100,\u{202e}above. -
.escape_bytes(bytes) ⇒ String
Each one as
\xNN. -
.printable(value, lines: false) ⇒ 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. -
.shell(value) ⇒ String
Text.printable, then single-quoted unless BARE, so it pastes as one argument.
Class Method Details
.escape(char) ⇒ String
Returns \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.
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.
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
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 |