Skip to content

Commit 54dbca2

Browse files
committed
CA-337000 escape non-printable characters in log msgs
To ensure that non-printable characters are logged in a readable way, escape them using an efficient implementation: - Any \ (0x5C) escaped to the sequence \\ (0x5C,0x5C). - Any byte in the ranges 0x00..0x1F and 0x7F..0xFF escaped by an e hexadecimal \xHH escape with H a capital hexadecimal number. These bytes are the US-ASCII control characters and non US-ASCII bytes. - Any other byte is left unchanged. The escaping of \ could be considered unnecessary. The main motivation was using an existing efficient implementation. Signed-off-by: Christian Lindig <[email protected]>
1 parent 587940c commit 54dbca2

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/debug.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ let gettimestring () =
6969
tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec
7070
(int_of_float (1000.0 *. msec))
7171

72+
(** [escape str] efficiently escapes non-printable characters and in
73+
* addition the backslash character. The function is efficient in the
74+
* sense that it will allocate a new string only when necessary *)
75+
let escape = Astring.String.Ascii.escape
76+
7277
let format include_time brand priority message =
7378
let id = get_thread_id () in
7479
let name = match ThreadLocalTable.find names with Some x -> x | None -> "" in
@@ -110,7 +115,7 @@ let output_log brand level priority s =
110115
if !print_debug
111116
then Printf.printf "%s\n%!" (format true brand priority s);
112117

113-
Syslog.log (get_facility ()) level msg
118+
Syslog.log (get_facility ()) level (escape msg)
114119
end
115120

116121
let logs_reporter =
@@ -265,7 +270,7 @@ module Make = functor(Brand: BRAND) -> struct
265270
Printf.kprintf
266271
(fun s ->
267272
let msg = if raw then s else format true Brand.name "audit" s in
268-
Syslog.log Syslog.Local6 Syslog.Info msg;
273+
Syslog.log Syslog.Local6 Syslog.Info (escape msg);
269274
msg
270275
) fmt
271276

lib/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
(modules (:standard \ scheduler task_server updates))
77
(c_names syslog_stubs)
88
(libraries
9+
astring
910
cmdliner
1011
cohttp
1112
fd-send-recv

0 commit comments

Comments
 (0)