about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/liolib.c21
-rw-r--r--src/teliva.c39
-rw-r--r--src/teliva.h2
-rw-r--r--test/attack.tlv414
-rw-r--r--test/attack_inner_function.tlv415
5 files changed, 9 insertions, 882 deletions
diff --git a/src/liolib.c b/src/liolib.c
index a53d8e6..b6cff86 100644
--- a/src/liolib.c
+++ b/src/liolib.c
@@ -123,11 +123,6 @@ static int io_tostring (lua_State *L) {
 }
 
 
-static int is_equal(const char *a, const char *b) {
-  return strcmp(a, b) == 0;
-}
-
-
 static char iolib_errbuf[1024] = {0};
 static int io_open (lua_State *L) {
   const char *filename = luaL_checkstring(L, 1);
@@ -137,22 +132,10 @@ static int io_open (lua_State *L) {
   snprintf(buffer, 1020, "io.open(\"%s\", \"%s\")", filename, mode);
   append_to_audit_log(L, buffer);
   FILE **pf = newfile(L);
-  const char *caller = get_caller(L);
-  if (file_operation_permitted(caller, filename, mode))
-    *pf = fopen(filename, mode);
-  else if (is_equal(caller, "temporary_filename_in_same_volume"))
+  if (file_operation_permitted(filename, mode))
     *pf = fopen(filename, mode);
-  else if (is_equal(caller, "start_writing") || is_equal(caller, "start_reading")) {
-    caller = get_caller_of_caller(L);
-    if (file_operation_permitted(caller, filename, mode))
-      *pf = fopen(filename, mode);
-    else {
-      snprintf(iolib_errbuf, 1024, "app tried to open file '%s' from caller '%s'; adjust its permissions (ctrl-p) if that is expected", filename, caller);
-      Previous_message = iolib_errbuf;
-    }
-  }
   else {
-    snprintf(iolib_errbuf, 1024, "app tried to open file '%s' from caller '%s'; adjust its permissions (ctrl-p) if that is expected", filename, caller);
+    snprintf(iolib_errbuf, 1024, "app tried to open file '%s'; adjust its permissions (ctrl-p) if that is expected", filename);
     Previous_message = iolib_errbuf;
   }
   return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
diff --git a/src/teliva.c b/src/teliva.c
index 2de3fb3..9ae7328 100644
--- a/src/teliva.c
+++ b/src/teliva.c
@@ -324,30 +324,6 @@ void save_caller(lua_State* L, const char* name, int call_graph_depth) {
   if (ar.name) save_caller_as(L, name, ar.name);
 }
 
-char* get_caller(lua_State* L) {
-  static char result[1024] = {0};
-  lua_Debug ar;
-  lua_getstack(L, 1, &ar);
-  lua_getinfo(L, "n", &ar);
-  memset(result, '\0', 1024);
-  if (ar.name)
-    strncpy(result, ar.name, 1020);
-  else  /* TODO: distinguish functions called from C */
-    strncpy(result, "main", 1020);
-  return result;
-}
-
-char* get_caller_of_caller(lua_State* L) {
-  static char result[1024] = {0};
-  lua_Debug ar;
-  lua_getstack(L, 2, &ar);
-  lua_getinfo(L, "n", &ar);
-  memset(result, '\0', 1024);
-  if (ar.name)
-    strncpy(result, ar.name, 1020);
-  return result;
-}
-
 void save_caller_as(lua_State* L, const char* name, const char* caller_name) {
   // push table of caller tables
   luaL_newmetatable(L, "__teliva_caller");
@@ -1315,13 +1291,12 @@ static const char* user_configuration_filename() {
   return config_filename;
 }
 
-int file_operation_permitted(const char* caller, const char* filename, const char* mode) {
+int file_operation_permitted(const char* filename, const char* mode) {
   int oldtop = lua_gettop(trustedL);
   lua_getglobal(trustedL, "file_operation_permitted");
-  lua_pushstring(trustedL, caller);
   lua_pushstring(trustedL, filename);
   lua_pushboolean(trustedL, strncmp(mode, "r", /*strlen("r") + 1 for NULL*/ 2) != 0);
-  if (lua_pcall(trustedL, 3 /*args*/, 1 /*result*/, /*errfunc*/0)) {
+  if (lua_pcall(trustedL, 2 /*args*/, 1 /*result*/, /*errfunc*/0)) {
     /* TODO: error handling. Or should we use errfunc above? */
   }
   if (!lua_isboolean(trustedL, -1)) {
@@ -1356,10 +1331,9 @@ void characterize_file_operations_predicate() {
   for (const char** test_filename = test_filenames; *test_filename; ++test_filename) {
     for (const char** test_mode = test_modes; *test_mode; ++test_mode) {
       lua_getglobal(trustedL, "file_operation_permitted");
-      lua_pushstring(trustedL, "___");
       lua_pushstring(trustedL, *test_filename);
       lua_pushstring(trustedL, *test_mode);
-      if (lua_pcall(trustedL, 3 /*args*/, 1 /*result*/, /*errfunc*/0)) {
+      if (lua_pcall(trustedL, 2 /*args*/, 1 /*result*/, /*errfunc*/0)) {
         /* TODO: error handling. Or should we use errfunc above? */
       }
       ++num_attempts;
@@ -1410,7 +1384,7 @@ static void render_permissions_screen() {
   attrset(A_NORMAL);
 
   mvaddstr(7, 5, "File operations");
-  mvaddstr(7, 30, "function file_operation_permitted(caller, filename, is_write)");
+  mvaddstr(7, 30, "function file_operation_permitted(filename, is_write)");
   int y = render_wrapped_text(8, 32, COLS-5, file_operations_predicate_body);
   mvaddstr(y, 30, "end");
   y++;
@@ -1479,10 +1453,9 @@ static void render_permissions_screen() {
  * on the stack and return non-zero */
 int validate_file_operations_predicate() {
   lua_getglobal(trustedL, "file_operation_permitted");
-  lua_pushstring(trustedL, "caller");
   lua_pushstring(trustedL, "filename");
   lua_pushstring(trustedL, "r");  /* open mode */
-  if (lua_pcall(trustedL, 3 /*args*/, 1 /*result*/, /*errfunc*/0)) {
+  if (lua_pcall(trustedL, 2 /*args*/, 1 /*result*/, /*errfunc*/0)) {
     /* TODO: error handling. Or should we use errfunc above? */
   }
   int status = 1;
@@ -1495,7 +1468,7 @@ int validate_file_operations_predicate() {
 
 static int load_file_operations_predicate(const char* body) {
   char buffer[1024] = {0};
-  strcpy(buffer, "function file_operation_permitted(caller, filename, is_write)\n");
+  strcpy(buffer, "function file_operation_permitted(filename, is_write)\n");
   strncat(buffer, body, 1020);
   if (buffer[strlen(buffer)-1] != '\n')
     strncat(buffer, "\n", 1020);
diff --git a/src/teliva.h b/src/teliva.h
index 34a6724..f07b761 100644
--- a/src/teliva.h
+++ b/src/teliva.h
@@ -155,7 +155,7 @@ extern char* Previous_message;
 extern int load_image(lua_State* L, char** argv, int n);
 extern void developer_mode(lua_State* L);
 extern void permissions_mode(lua_State* L);
-extern int file_operation_permitted(const char* caller, const char* filename, const char* mode);
+extern int file_operation_permitted(const char* filename, const char* mode);
 extern int net_operations_permitted;
 
 extern void load_editor_buffer_to_current_definition_in_image(lua_State* L);
diff --git a/test/attack.tlv b/test/attack.tlv
deleted file mode 100644
index 195d6bc..0000000
--- a/test/attack.tlv
+++ /dev/null
@@ -1,414 +0,0 @@
-# .tlv file generated by https://github.com/akkartik/teliva
-# You may edit it if you are careful; however, you may see cryptic errors if you
-# violate Teliva's assumptions.
-#
-# .tlv files are representations of Teliva programs. Teliva programs consist of
-# sequences of definitions. Each definition is a table of key/value pairs. Keys
-# and values are both strings.
-#
-# Lines in .tlv files always follow exactly one of the following forms:
-# - comment lines at the top of the file starting with '#' at column 0
-# - beginnings of definitions starting with '- ' at column 0, followed by a
-#   key/value pair
-# - key/value pairs consisting of '  ' at column 0, containing either a
-#   spaceless value on the same line, or a multi-line value
-# - multiline values indented by more than 2 spaces, starting with a '>'
-#
-# If these constraints are violated, Teliva may unceremoniously crash. Please
-# report bugs at http://akkartik.name/contact
-- __teliva_timestamp: original
-  str_helpers:
-    >-- some string helpers from http://lua-users.org/wiki/StringIndexing
-    >
-    >-- index characters using []
-    >getmetatable('').__index = function(str,i)
-    >  if type(i) == 'number' then
-    >    return str:sub(i,i)
-    >  else
-    >    return string[i]
-    >  end
-    >end
-    >
-    >-- ranges using (), selected bytes using {}
-    >getmetatable('').__call = function(str,i,j)
-    >  if type(i)~='table' then
-    >    return str:sub(i,j)
-    >  else
-    >    local t={}
-    >    for k,v in ipairs(i) do
-    >      t[k]=str:sub(v,v)
-    >    end
-    >    return table.concat(t)
-    >  end
-    >end
-    >
-    >-- iterate over an ordered sequence
-    >function q(x)
-    >  if type(x) == 'string' then
-    >    return x:gmatch('.')
-    >  else
-    >    return ipairs(x)
-    >  end
-    >end
-    >
-    >-- insert within string
-    >function string.insert(str1, str2, pos)
-    >  return str1:sub(1,pos)..str2..str1:sub(pos+1)
-    >end
-    >
-    >function string.remove(s, pos)
-    >  return s:sub(1,pos-1)..s:sub(pos+1)
-    >end
-    >
-    >-- TODO: backport utf-8 support from Lua 5.3
-- __teliva_timestamp: original
-  debugy:
-    >debugy = 5
-- __teliva_timestamp: original
-  dbg:
-    >-- helper for debug by print; overlay debug information towards the right
-    >-- reset debugy every time you refresh screen
-    >function dbg(window, s)
-    >  local oldy = 0
-    >  local oldx = 0
-    >  oldy, oldx = window:getyx()
-    >  window:mvaddstr(debugy, 60, s)
-    >  debugy = debugy+1
-    >  window:mvaddstr(oldy, oldx, '')
-    >end
-- __teliva_timestamp: original
-  check_eq:
-    >function check_eq(x, expected, msg)
-    >  if eq(x, expected) then
-    >    Window:addch('.')
-    >  else
-    >    print('F - '..msg)
-    >    print('  expected '..str(expected)..' but got '..str(x))
-    >    teliva_num_test_failures = teliva_num_test_failures + 1
-    >    -- overlay first test failure on editors
-    >    if teliva_first_failure == nil then
-    >      teliva_first_failure = msg
-    >    end
-    >  end
-    >end
-- __teliva_timestamp: original
-  eq:
-    >function eq(a, b)
-    >  if type(a) ~= type(b) then return false end
-    >  if type(a) == 'table' then
-    >    if #a ~= #b then return false end
-    >    for k, v in pairs(a) do
-    >      if b[k] ~= v then
-    >        return false
-    >      end
-    >      return true
-    >    end
-    >  end
-    >  return a == b
-    >end
-- __teliva_timestamp: original
-  str:
-    >-- smarter tostring
-    >-- slow; used only for debugging
-    >function str(x)
-    >  if type(x) == 'table' then
-    >    local result = ''
-    >    result = result..#x..'{'
-    >    for k, v in pairs(x) do
-    >      result = result..str(k)..'='..str(v)..', '
-    >    end
-    >    result = result..'}'
-    >    return result
-    >  end
-    >  return tostring(x)
-    >end
-- __teliva_timestamp: original
-  map:
-    >-- only for arrays
-    >function map(l, f)
-    >  result = {}
-    >  for _, x in ipairs(l) do
-    >    table.insert(result, f(x))
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  reduce:
-    >-- only for arrays
-    >function reduce(l, f, init)
-    >  result = init
-    >  for _, x in ipairs(l) do
-    >    result = f(result, x)
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  filter:
-    >-- only for arrays
-    >function filter(l, f)
-    >  result = {}
-    >  for _, x in ipairs(l) do
-    >    if f(x) then
-    >      table.insert(result, x)
-    >    end
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  find_index:
-    >function find_index(arr, x)
-    >  for n, y in ipairs(arr) do
-    >    if x == y then
-    >      return n
-    >    end
-    >  end
-    >end
-- __teliva_timestamp: original
-  trim:
-    >function trim(s)
-    >  return s:gsub('^%s*', ''):gsub('%s*$', '')
-    >end
-- __teliva_timestamp: original
-  split:
-    >function split(s, d)
-    >  result = {}
-    >  for match in (s..d):gmatch("(.-)"..d) do
-    >    table.insert(result, match);
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  sort_letters:
-    >function sort_letters(s)
-    >  tmp = {}
-    >  for i=1,#s do
-    >    table.insert(tmp, s[i])
-    >  end
-    >  table.sort(tmp)
-    >  local result = ''
-    >  for _, c in pairs(tmp) do
-    >    result = result..c
-    >  end
-    >  return result
-    >end
-    >
-    >function test_sort_letters(s)
-    >  check_eq(sort_letters(''), '', 'test_sort_letters: empty')
-    >  check_eq(sort_letters('ba'), 'ab', 'test_sort_letters: non-empty')
-    >  check_eq(sort_letters('abba'), 'aabb', 'test_sort_letters: duplicates')
-    >end
-- __teliva_timestamp: original
-  count_letters:
-    >function count_letters(s)
-    >  local result = {}
-    >  for i=1,s:len() do
-    >    local c = s[i]
-    >    if result[c] == nil then
-    >      result[c] = 1
-    >    else
-    >      result[c] = result[c] + 1
-    >    end
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  append:
-    >-- concatenate list 'elems' into 'l', modifying 'l' in the process
-    >function append(l, elems)
-    >  for i=1,#elems do
-    >    l[#l+1] = elems[i]
-    >  end
-    >end
-- __teliva_timestamp: original
-  menu:
-    >-- To show app-specific hotkeys in the menu bar, add hotkey/command
-    >-- arrays of strings to the menu array.
-    >menu = {}
-- __teliva_timestamp: original
-  Window:
-    >Window = curses.stdscr()
-- __teliva_timestamp: original
-  window:
-    >-- constructor for fake screen and window
-    >-- call it like this:
-    >--   local w = window{
-    >--     kbd=kbd('abc'),
-    >--     scr=scr{h=5, w=4},
-    >--   }
-    >-- eventually it'll do everything a real ncurses window can
-    >function window(h)
-    >  h.__index = h
-    >  setmetatable(h, h)
-    >  h.__index = function(table, key)
-    >    return rawget(h, key)
-    >  end
-    >  h.getch = function(self)
-    >    return table.remove(h.kbd, 1)
-    >  end
-    >  h.addch = function(self, c)
-    >    local scr = self.scr
-    >    if scr.cursy <= scr.h then
-    >      scr[scr.cursy][scr.cursx] = c
-    >      scr.cursx = scr.cursx+1
-    >      if scr.cursx > scr.w then
-    >        scr.cursy = scr.cursy+1
-    >        scr.cursx = 1
-    >      end
-    >    end
-    >  end
-    >  h.addstr = function(self, s)
-    >    for i=1,s:len() do
-    >      self:addch(s[i])
-    >    end
-    >  end
-    >  h.mvaddch = function(self, y, x, c)
-    >    self.scr.cursy = y
-    >    self.scr.cursx = x
-    >    self.addch(c)
-    >  end
-    >  h.mvaddstr = function(self, y, x, s)
-    >    self.scr.cursy = y
-    >    self.scr.cursx = x
-    >    self:addstr(s)
-    >  end
-    >  return h
-    >end
-- __teliva_timestamp: original
-  kbd:
-    >function kbd(keys)
-    >  local result = {}
-    >  for i=1,keys:len() do
-    >    table.insert(result, keys[i])
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  scr:
-    >function scr(props)
-    >  props.cursx = 1
-    >  props.cursy = 1
-    >  for y=1,props.h do
-    >    props[y] = {}
-    >    for x=1,props.w do
-    >      props[y][x] = ' '
-    >    end
-    >  end
-    >  return props
-    >end
-- __teliva_timestamp: original
-  check_screen:
-    >function check_screen(window, contents, message)
-    >  local x, y = 1, 1
-    >  for i=1,contents:len() do
-    >    check_eq(contents[i], window.scr[y][x], message..'/'..y..','..x)
-    >    x = x+1
-    >    if x > window.scr.w then
-    >      y = y+1
-    >      x = 1
-    >    end
-    >  end
-    >end
-    >
-    >-- putting it all together, an example test of both keyboard and screen
-    >function test_check_screen()
-    >  local lines = {
-    >    c='123',
-    >    d='234',
-    >    a='345',
-    >    b='456',
-    >  }
-    >  local w = window{
-    >    kbd=kbd('abc'),
-    >    scr=scr{h=3, w=5},
-    >  }
-    >  local y = 1
-    >  while true do
-    >    local c = w:getch()
-    >    if c == nil then break end
-    >    w:mvaddstr(y, 1, lines[c])
-    >    y = y+1
-    >  end
-    >  check_screen(w, '345  '..
-    >                  '456  '..
-    >                  '123  ',
-    >              'test_check_screen')
-    >end
-- __teliva_timestamp: original
-  render:
-    >function render(window)
-    >  window:clear()
-    >  -- draw stuff to screen here
-    >  window:attron(curses.A_BOLD)
-    >  window:mvaddstr(1, 5, "example app")
-    >  window:attrset(curses.A_NORMAL)
-    >  for i=0,15 do
-    >    window:attrset(curses.color_pair(i))
-    >    window:mvaddstr(3+i, 5, "========================")
-    >  end
-    >  window:refresh()
-    >end
-- __teliva_timestamp: original
-  update:
-    >function update(window)
-    >  local key = window:getch()
-    >  -- process key here
-    >end
-- __teliva_timestamp: original
-  init_colors:
-    >function init_colors()
-    >  for i=0,7 do
-    >    curses.init_pair(i, i, -1)
-    >  end
-    >  curses.init_pair(8, 7, 0)
-    >  curses.init_pair(9, 7, 1)
-    >  curses.init_pair(10, 7, 2)
-    >  curses.init_pair(11, 7, 3)
-    >  curses.init_pair(12, 7, 4)
-    >  curses.init_pair(13, 7, 5)
-    >  curses.init_pair(14, 7, 6)
-    >  curses.init_pair(15, -1, 15)
-    >end
-- __teliva_timestamp: original
-  main:
-    >function main()
-    >  local fout = start_writing(nil, "x")
-    >  if fout then
-    >    fout:send('abc')
-    >    fout:close()
-    >  end
-    >  Window:getch()
-    >end
-- __teliva_timestamp: original
-  doc:blurb:
-    >A manual test for an arcane sandboxing scenario.
-    >
-    >Steps to reproduce:
-    >  - browse to 'main' and see that it's just trying to write
-    >    to file 'x'
-    >  - hit ctrl-x to return to the app until it asks to open a
-    >    file
-    >  - open permissions screen with ctrl-p, hit ctrl-p again
-    >    to confirm
-    >  - edit the file_operations_permitted by hitting ctrl-f
-    >  - modify the body of file_operations_permitted to allow
-    >    'main'
-    >    ```
-    >    return caller == 'main'
-    >    ```
-    >  - hit ctrl-x twice to return to the app
-    >
-    >A failure to sandbox this app is indicated by the error,
-    >"wrote to malicious file!!"
-- __teliva_timestamp: original
-  foo:
-    >-- maliciously write to a primitive Teliva's permission system cares about
-    >-- it's important that this definition is camouflaged as a definition of
-    >-- 'foo' (something different from 'start_writing').
-    >function start_writing(fs, filename)
-    >  local outfile = io.open('malicious_file', 'w')
-    >  if outfile then
-    >    outfile:write('abc')
-    >    outfile:close()
-    >    error('wrote to malicious_file!!')
-    >  end
-    >end
diff --git a/test/attack_inner_function.tlv b/test/attack_inner_function.tlv
deleted file mode 100644
index f42fbb9..0000000
--- a/test/attack_inner_function.tlv
+++ /dev/null
@@ -1,415 +0,0 @@
-# .tlv file generated by https://github.com/akkartik/teliva
-# You may edit it if you are careful; however, you may see cryptic errors if you
-# violate Teliva's assumptions.
-#
-# .tlv files are representations of Teliva programs. Teliva programs consist of
-# sequences of definitions. Each definition is a table of key/value pairs. Keys
-# and values are both strings.
-#
-# Lines in .tlv files always follow exactly one of the following forms:
-# - comment lines at the top of the file starting with '#' at column 0
-# - beginnings of definitions starting with '- ' at column 0, followed by a
-#   key/value pair
-# - key/value pairs consisting of '  ' at column 0, containing either a
-#   spaceless value on the same line, or a multi-line value
-# - multiline values indented by more than 2 spaces, starting with a '>'
-#
-# If these constraints are violated, Teliva may unceremoniously crash. Please
-# report bugs at http://akkartik.name/contact
-- __teliva_timestamp: original
-  str_helpers:
-    >-- some string helpers from http://lua-users.org/wiki/StringIndexing
-    >
-    >-- index characters using []
-    >getmetatable('').__index = function(str,i)
-    >  if type(i) == 'number' then
-    >    return str:sub(i,i)
-    >  else
-    >    return string[i]
-    >  end
-    >end
-    >
-    >-- ranges using (), selected bytes using {}
-    >getmetatable('').__call = function(str,i,j)
-    >  if type(i)~='table' then
-    >    return str:sub(i,j)
-    >  else
-    >    local t={}
-    >    for k,v in ipairs(i) do
-    >      t[k]=str:sub(v,v)
-    >    end
-    >    return table.concat(t)
-    >  end
-    >end
-    >
-    >-- iterate over an ordered sequence
-    >function q(x)
-    >  if type(x) == 'string' then
-    >    return x:gmatch('.')
-    >  else
-    >    return ipairs(x)
-    >  end
-    >end
-    >
-    >-- insert within string
-    >function string.insert(str1, str2, pos)
-    >  return str1:sub(1,pos)..str2..str1:sub(pos+1)
-    >end
-    >
-    >function string.remove(s, pos)
-    >  return s:sub(1,pos-1)..s:sub(pos+1)
-    >end
-    >
-    >-- TODO: backport utf-8 support from Lua 5.3
-- __teliva_timestamp: original
-  debugy:
-    >debugy = 5
-- __teliva_timestamp: original
-  dbg:
-    >-- helper for debug by print; overlay debug information towards the right
-    >-- reset debugy every time you refresh screen
-    >function dbg(window, s)
-    >  local oldy = 0
-    >  local oldx = 0
-    >  oldy, oldx = window:getyx()
-    >  window:mvaddstr(debugy, 60, s)
-    >  debugy = debugy+1
-    >  window:mvaddstr(oldy, oldx, '')
-    >end
-- __teliva_timestamp: original
-  check_eq:
-    >function check_eq(x, expected, msg)
-    >  if eq(x, expected) then
-    >    Window:addch('.')
-    >  else
-    >    print('F - '..msg)
-    >    print('  expected '..str(expected)..' but got '..str(x))
-    >    teliva_num_test_failures = teliva_num_test_failures + 1
-    >    -- overlay first test failure on editors
-    >    if teliva_first_failure == nil then
-    >      teliva_first_failure = msg
-    >    end
-    >  end
-    >end
-- __teliva_timestamp: original
-  eq:
-    >function eq(a, b)
-    >  if type(a) ~= type(b) then return false end
-    >  if type(a) == 'table' then
-    >    if #a ~= #b then return false end
-    >    for k, v in pairs(a) do
-    >      if b[k] ~= v then
-    >        return false
-    >      end
-    >      return true
-    >    end
-    >  end
-    >  return a == b
-    >end
-- __teliva_timestamp: original
-  str:
-    >-- smarter tostring
-    >-- slow; used only for debugging
-    >function str(x)
-    >  if type(x) == 'table' then
-    >    local result = ''
-    >    result = result..#x..'{'
-    >    for k, v in pairs(x) do
-    >      result = result..str(k)..'='..str(v)..', '
-    >    end
-    >    result = result..'}'
-    >    return result
-    >  end
-    >  return tostring(x)
-    >end
-- __teliva_timestamp: original
-  map:
-    >-- only for arrays
-    >function map(l, f)
-    >  result = {}
-    >  for _, x in ipairs(l) do
-    >    table.insert(result, f(x))
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  reduce:
-    >-- only for arrays
-    >function reduce(l, f, init)
-    >  result = init
-    >  for _, x in ipairs(l) do
-    >    result = f(result, x)
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  filter:
-    >-- only for arrays
-    >function filter(l, f)
-    >  result = {}
-    >  for _, x in ipairs(l) do
-    >    if f(x) then
-    >      table.insert(result, x)
-    >    end
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  find_index:
-    >function find_index(arr, x)
-    >  for n, y in ipairs(arr) do
-    >    if x == y then
-    >      return n
-    >    end
-    >  end
-    >end
-- __teliva_timestamp: original
-  trim:
-    >function trim(s)
-    >  return s:gsub('^%s*', ''):gsub('%s*$', '')
-    >end
-- __teliva_timestamp: original
-  split:
-    >function split(s, d)
-    >  result = {}
-    >  for match in (s..d):gmatch("(.-)"..d) do
-    >    table.insert(result, match);
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  sort_letters:
-    >function sort_letters(s)
-    >  tmp = {}
-    >  for i=1,#s do
-    >    table.insert(tmp, s[i])
-    >  end
-    >  table.sort(tmp)
-    >  local result = ''
-    >  for _, c in pairs(tmp) do
-    >    result = result..c
-    >  end
-    >  return result
-    >end
-    >
-    >function test_sort_letters(s)
-    >  check_eq(sort_letters(''), '', 'test_sort_letters: empty')
-    >  check_eq(sort_letters('ba'), 'ab', 'test_sort_letters: non-empty')
-    >  check_eq(sort_letters('abba'), 'aabb', 'test_sort_letters: duplicates')
-    >end
-- __teliva_timestamp: original
-  count_letters:
-    >function count_letters(s)
-    >  local result = {}
-    >  for i=1,s:len() do
-    >    local c = s[i]
-    >    if result[c] == nil then
-    >      result[c] = 1
-    >    else
-    >      result[c] = result[c] + 1
-    >    end
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  append:
-    >-- concatenate list 'elems' into 'l', modifying 'l' in the process
-    >function append(l, elems)
-    >  for i=1,#elems do
-    >    l[#l+1] = elems[i]
-    >  end
-    >end
-- __teliva_timestamp: original
-  menu:
-    >-- To show app-specific hotkeys in the menu bar, add hotkey/command
-    >-- arrays of strings to the menu array.
-    >menu = {}
-- __teliva_timestamp: original
-  Window:
-    >Window = curses.stdscr()
-- __teliva_timestamp: original
-  window:
-    >-- constructor for fake screen and window
-    >-- call it like this:
-    >--   local w = window{
-    >--     kbd=kbd('abc'),
-    >--     scr=scr{h=5, w=4},
-    >--   }
-    >-- eventually it'll do everything a real ncurses window can
-    >function window(h)
-    >  h.__index = h
-    >  setmetatable(h, h)
-    >  h.__index = function(table, key)
-    >    return rawget(h, key)
-    >  end
-    >  h.getch = function(self)
-    >    return table.remove(h.kbd, 1)
-    >  end
-    >  h.addch = function(self, c)
-    >    local scr = self.scr
-    >    if scr.cursy <= scr.h then
-    >      scr[scr.cursy][scr.cursx] = c
-    >      scr.cursx = scr.cursx+1
-    >      if scr.cursx > scr.w then
-    >        scr.cursy = scr.cursy+1
-    >        scr.cursx = 1
-    >      end
-    >    end
-    >  end
-    >  h.addstr = function(self, s)
-    >    for i=1,s:len() do
-    >      self:addch(s[i])
-    >    end
-    >  end
-    >  h.mvaddch = function(self, y, x, c)
-    >    self.scr.cursy = y
-    >    self.scr.cursx = x
-    >    self.addch(c)
-    >  end
-    >  h.mvaddstr = function(self, y, x, s)
-    >    self.scr.cursy = y
-    >    self.scr.cursx = x
-    >    self:addstr(s)
-    >  end
-    >  return h
-    >end
-- __teliva_timestamp: original
-  kbd:
-    >function kbd(keys)
-    >  local result = {}
-    >  for i=1,keys:len() do
-    >    table.insert(result, keys[i])
-    >  end
-    >  return result
-    >end
-- __teliva_timestamp: original
-  scr:
-    >function scr(props)
-    >  props.cursx = 1
-    >  props.cursy = 1
-    >  for y=1,props.h do
-    >    props[y] = {}
-    >    for x=1,props.w do
-    >      props[y][x] = ' '
-    >    end
-    >  end
-    >  return props
-    >end
-- __teliva_timestamp: original
-  check_screen:
-    >function check_screen(window, contents, message)
-    >  local x, y = 1, 1
-    >  for i=1,contents:len() do
-    >    check_eq(contents[i], window.scr[y][x], message..'/'..y..','..x)
-    >    x = x+1
-    >    if x > window.scr.w then
-    >      y = y+1
-    >      x = 1
-    >    end
-    >  end
-    >end
-    >
-    >-- putting it all together, an example test of both keyboard and screen
-    >function test_check_screen()
-    >  local lines = {
-    >    c='123',
-    >    d='234',
-    >    a='345',
-    >    b='456',
-    >  }
-    >  local w = window{
-    >    kbd=kbd('abc'),
-    >    scr=scr{h=3, w=5},
-    >  }
-    >  local y = 1
-    >  while true do
-    >    local c = w:getch()
-    >    if c == nil then break end
-    >    w:mvaddstr(y, 1, lines[c])
-    >    y = y+1
-    >  end
-    >  check_screen(w, '345  '..
-    >                  '456  '..
-    >                  '123  ',
-    >              'test_check_screen')
-    >end
-- __teliva_timestamp: original
-  render:
-    >function render(window)
-    >  window:clear()
-    >  -- draw stuff to screen here
-    >  window:attron(curses.A_BOLD)
-    >  window:mvaddstr(1, 5, "example app")
-    >  window:attrset(curses.A_NORMAL)
-    >  for i=0,15 do
-    >    window:attrset(curses.color_pair(i))
-    >    window:mvaddstr(3+i, 5, "========================")
-    >  end
-    >  window:refresh()
-    >end
-- __teliva_timestamp: original
-  update:
-    >function update(window)
-    >  local key = window:getch()
-    >  -- process key here
-    >end
-- __teliva_timestamp: original
-  init_colors:
-    >function init_colors()
-    >  for i=0,7 do
-    >    curses.init_pair(i, i, -1)
-    >  end
-    >  curses.init_pair(8, 7, 0)
-    >  curses.init_pair(9, 7, 1)
-    >  curses.init_pair(10, 7, 2)
-    >  curses.init_pair(11, 7, 3)
-    >  curses.init_pair(12, 7, 4)
-    >  curses.init_pair(13, 7, 5)
-    >  curses.init_pair(14, 7, 6)
-    >  curses.init_pair(15, -1, 15)
-    >end
-- __teliva_timestamp: original
-  main:
-    >function main()
-    >  init_colors()
-    >
-    >  while true do
-    >    render(Window)
-    >    update(Window)
-    >  end
-    >end
-- __teliva_timestamp: original
-  doc:blurb:
-    >To show a brief description of the app on the 'big picture' screen, put the text in a special buffer called 'doc:blurb'.
-    >
-    >You can also override the default big picture screen entirely by creating a buffer called 'doc:main'.
-- __teliva_timestamp:
-    >Mon Mar  7 19:19:35 2022
-  main:
-    >function main()
-    >  local foo = start_writing(nil, 'abc')
-    >  foo:send('abc')
-    >  foo:close()
-    >  bar()
-    >end
-- __teliva_timestamp:
-    >Mon Mar  7 19:20:16 2022
-  bar:
-    >function bar()
-    >  function main()
-    >    local foo = start_writing(nil, 'malicious_file')
-    >    foo:send('abc')
-    >    foo:close()
-    >  end
-    >  main()
-    >end
-- __teliva_timestamp:
-    >Mon Mar  7 19:20:23 2022
-  main:
-    >function main()
-    >  local foo = start_writing(nil, 'abc')
-    >  foo:send('abc')
-    >  foo:close()
-    >  bar()
-    >  Window:getch()
-    >end