aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/shell.lux
blob: aefdf2b3cb4cf82d901d679867710ed1d25a4d90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
(.module:
  [lux #*
   ["." io (#+ IO)]
   ["jvm" host (#+ import:)]
   [control
    [monad (#+ do)]]
   [data
    [number (#+ hex)]
    ["." product]
    ["." maybe]
    ["." error (#+ Error)]
    ["." text
     format
     ["." encoding]]
    [format
     ["." context (#+ Context)]]
    [collection
     ["." array (#+ Array)]
     ["." list ("#/." fold functor)]
     ["." dictionary]]]
   [tool
    [compiler
     ["." host]]]
   [world
    ["." console (#+ Console)]]])

## https://en.wikipedia.org/wiki/Code_injection#Shell_injection
(def: windows?
  (-> Text Bit)
  (text.starts-with? "windows"))

(def: (sanitize-command windows?)
  (-> Bit (-> Text Text))
  (let [dangerous (format "\&#;`|*?~<>^()[]{}$"
                          (text.from-code (hex "0A"))
                          (text.from-code (hex "FF")))
        dangerous (if windows?
                    (format dangerous "%!")
                    dangerous)
        indices (list.n/range 0 (dec (text.size dangerous)))]
    (function (_ unsafe)
      (list/fold (function (_ index safer)
                   (let [bad (|> dangerous (text.nth index) maybe.assume text.from-code)
                         good (if windows?
                                " "
                                (format "\" bad))]
                     (text.replace-all bad good safer)))
                 unsafe
                 indices))))

(def: (sanitize-argument windows?)
  (-> Bit (-> Text Text))
  (if windows?
    (|>> (text.replace-all "%" " ")
         (text.replace-all "!" " ")
         (text.replace-all text.double-quote " ")
         (text.enclose' text.double-quote))
    (|>> (text.replace-all "'" "\'")
         (text.enclose' "'"))))

(`` (for {(~~ (static host.jvm))
          (as-is (import: #long java/lang/String
                   (toLowerCase [] java/lang/String))

                 (def: (arguments-array arguments)
                   (-> (List Text) (Array java/lang/String))
                   (product.right
                    (list/fold (function (_ argument [idx output])
                                 [(inc idx) (jvm.array-write idx argument output)])
                               [0 (jvm.array java/lang/String (list.size arguments))]
                               arguments)))

                 (import: #long (java/util/Map k v)
                   (put [k v] v))

                 (def: (load-environment input target)
                   (-> Context
                       (java/util/Map java/lang/String java/lang/String)
                       (java/util/Map java/lang/String java/lang/String))
                   (list/fold (function (_ [key value] target')
                                (exec (java/util/Map::put key value target')
                                  target'))
                              target
                              (dictionary.entries input)))
                 
                 (import: #long java/io/Reader
                   (read [] #io #try int))

                 (import: #long java/io/BufferedReader
                   (new [java/io/Reader])
                   (readLine [] #io #try java/lang/String))

                 (import: #long java/io/InputStream)
                 
                 (import: #long java/io/InputStreamReader
                   (new [java/io/InputStream]))

                 (import: #long java/io/OutputStream
                   (write [(Array byte)] #io #try void))

                 (import: #long java/lang/Process
                   (getInputStream [] #io #try java/io/InputStream)
                   (getOutputStream [] #io #try java/io/OutputStream)
                   (destroy [] #io #try void))

                 (def: (process-console process)
                   (-> java/lang/Process (IO (Error (Console IO))))
                   (do (error.with-error io.monad)
                     [jvm-input (java/lang/Process::getInputStream process)
                      #let [jvm-input (|> jvm-input
                                          java/io/InputStreamReader::new
                                          java/io/BufferedReader::new)]
                      jvm-output (java/lang/Process::getOutputStream process)]
                     (wrap (: (Console IO)
                              (structure
                               (def: can-read
                                 (console.can-read
                                  (function (_ _)
                                    (|> jvm-input
                                        java/io/Reader::read
                                        (:: (error.with-error io.monad) map .nat)))))
                               
                               (def: can-read-line
                                 (console.can-read
                                  (function (_ _)
                                    (|> jvm-input
                                        java/io/BufferedReader::readLine))))
                               
                               (def: can-write
                                 (console.can-write
                                  (function (_ message)
                                    (|> jvm-output
                                        (java/io/OutputStream::write (encoding.to-utf8 message))))))
                               
                               (def: can-close
                                 (console.can-close
                                  (function (_ _)
                                    (|> process
                                        java/lang/Process::destroy)))))))))

                 (import: #long java/lang/ProcessBuilder
                   (new [(Array java/lang/String)])
                   (environment [] #io #try (java/util/Map java/lang/String java/lang/String))
                   (start [] #io #try java/lang/Process))

                 (import: #long java/lang/System
                   (#static getProperty [java/lang/String] #io #try java/lang/String))
                 )}))

(def: #export (execute environment command arguments)
  (-> Context Text (List Text) (IO (Error (Console IO))))
  (`` (for {(~~ (static host.jvm))
            (do (error.with-error io.monad)
              [windows? (:: @ map (|>> java/lang/String::toLowerCase ..windows?)
                            (java/lang/System::getProperty "os.name"))
               #let [builder (java/lang/ProcessBuilder::new (arguments-array (list& (sanitize-command windows? command)
                                                                                    (list/map (sanitize-argument windows?) arguments))))]
               environment (:: @ map (load-environment environment)
                               (java/lang/ProcessBuilder::environment builder))
               process (java/lang/ProcessBuilder::start builder)]
              (process-console process))})))