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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// generated by hxc 1.0.0-acgt-compiler from src/tripwire.hlx -- do not edit by hand
#[cfg(windows)]
#[allow(dead_code)]
const SYS_CURL: &str = "C:\\Windows\\System32\\curl.exe";
#[cfg(not(windows))]
#[allow(dead_code)]
const SYS_CURL: &str = "curl";
#[allow(dead_code)]
fn _helix_allowed(var: &str, what: &str) -> bool {
let ok = std::env::var(var).map(|v| v == "1").unwrap_or(false);
if !ok {
eprintln!("helix: BLOCKED {} -- permission denied (set {}=1 to allow; `hxc run` allows by default, a `hxc build`/`export-rust` binary does not)", what, var);
}
ok
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_exec(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_exec(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
c
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_net(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_net(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
c
}
// hlx:5
pub fn words(command: &str) -> Vec<String> {
// hlx:6
let mut s = String::with_capacity(command.len() + 8);
// hlx:7
let mut chars = command.chars().peekable();
// hlx:8
while let Some(c) = chars.next() {
// hlx:9
match c {
// hlx:10
'"' | '\'' | '`' => {}
// hlx:11
';' | '|' | '&' | '(' | ')' => s.push(' '),
// hlx:12
'>' => {
// hlx:13
if chars.peek() == Some(&'>') {
// hlx:14
chars.next();
// hlx:15
s.push_str(" >> ");
// hlx:16
} else {
// hlx:17
s.push_str(" > ");
// hlx:18
}
// hlx:19
}
// hlx:20
c => s.push(c),
// hlx:21
}
// hlx:22
}
// hlx:23
s.split_whitespace().map(str::to_owned).collect()
// hlx:24
}
// hlx:27
fn runner(w: &[String]) -> Option<String> {
// hlx:28
let mut i = 0;
// hlx:29
while i < w.len()
&& (matches!(
w[i].as_str(),
"sudo"
| "doas"
| "env"
| "time"
| "nohup"
| "nice"
| "command"
| "exec"
| "builtin"
| "xargs"
) || w[i].starts_with('-')
|| w[i].contains('='))
{
// hlx:30
if matches!(
w[i].as_str(),
"-u" | "-g" | "-I" | "-n" | "-P" | "-d" | "-E" | "-L"
) {
// hlx:31
i += 1;
// hlx:32
}
// hlx:33
i += 1;
// hlx:34
}
// hlx:35
let name = w.get(i)?.rsplit('/').next()?.to_lowercase();
// hlx:36
if name == "git" {
// hlx:37
let sub = w.iter().skip(i + 1).find(|x| !x.starts_with('-'))?;
// hlx:38
return Some(["git ", sub.as_str()].concat());
// hlx:39
}
// hlx:40
Some(name)
// hlx:41
}
// hlx:44
fn read_only(w: &[String]) -> bool {
// hlx:45
let Some(r) = runner(w) else { return false };
// hlx:46
matches!(
r.as_str(),
"grep"
| "egrep"
| "fgrep"
| "zgrep"
| "rg"
| "ag"
| "ack"
| "echo"
| "printf"
| "man"
| "cat"
| "bat"
| "less"
| "more"
| "head"
| "tail"
| "wc"
| "ls"
| "stat"
| "file"
| "which"
| "type"
| "whereis"
| "whatis"
| "apropos"
| "git log"
| "git grep"
| "git show"
| "git diff"
| "git blame"
| "git status"
)
// hlx:47
}
// hlx:50
fn runs_input(w: &[String]) -> bool {
// hlx:51
let Some(r) = runner(w) else { return false };
// hlx:52
matches!(
r.as_str(),
"sh" | "bash"
| "zsh"
| "dash"
| "ksh"
| "fish"
| "psql"
| "mysql"
| "mariadb"
| "sqlite3"
| "sqlcmd"
| "redis-cli"
| "mongosh"
| "pwsh"
| "powershell"
| "cmd"
| "python"
| "python3"
| "node"
| "perl"
| "ruby"
)
// hlx:53
}
// hlx:56
pub fn command_words(command: &str) -> Vec<String> {
// hlx:57
let mut segs: Vec<(String, String, bool)> = Vec::new();
// hlx:58
let mut raw = String::new();
// hlx:59
let mut bare = String::new();
// hlx:60
let mut quote = ' ';
// hlx:61
let mut prev = ' ';
// hlx:62
let mut it = command.chars();
// hlx:63
while let Some(c) = it.next() {
// hlx:64
if quote == ' ' && matches!(c, ';' | '|' | '&' | '(' | ')' | '\n') {
// hlx:66
if c == '&'
&& prev != '&'
&& prev != '>'
&& prev != '|'
&& !matches!(it.as_str().chars().next(), Some('&' | '>'))
{
// hlx:67
raw.push_str(" __bg__");
// hlx:68
bare.push_str(" __bg__");
// hlx:69
if it.as_str().trim_start().starts_with(')') {
// hlx:70
raw.push_str(" __subbg__");
// hlx:71
bare.push_str(" __subbg__");
// hlx:72
}
// hlx:73
}
// hlx:74
segs.push((
raw.clone(),
bare.clone(),
c == '|' && prev != '|' && it.as_str().chars().next() != Some('|'),
));
// hlx:75
raw.clear();
// hlx:76
bare.clear();
// hlx:77
prev = c;
// hlx:78
continue;
// hlx:79
}
// hlx:80
prev = c;
// hlx:81
if c == '\\' && quote != '\'' {
// hlx:82
raw.push(c);
// hlx:83
bare.push(c);
// hlx:84
if let Some(n) = it.next() {
// hlx:85
raw.push(n);
// hlx:86
bare.push(n);
// hlx:87
}
// hlx:88
continue;
// hlx:89
}
// hlx:90
if quote != ' ' {
// hlx:91
raw.push(c);
// hlx:92
bare.push(if c == '>' { '_' } else { c });
// hlx:93
if c == quote {
// hlx:94
quote = ' ';
// hlx:95
}
// hlx:96
continue;
// hlx:97
}
// hlx:98
if c == '"' || c == '\'' || c == '`' {
// hlx:99
quote = c;
// hlx:100
}
// hlx:101
raw.push(c);
// hlx:102
bare.push(c);
// hlx:103
}
// hlx:104
segs.push((raw, bare, false));
// hlx:105
let mut out: Vec<String> = Vec::new();
// hlx:106
for i in 0..segs.len() {
// hlx:107
let w = words(&segs[i].0);
// hlx:108
if w.is_empty() {
// hlx:109
continue;
// hlx:110
}
// hlx:111
if read_only(&w)
&& !w.iter().any(|x| x == "__bg__")
&& !(segs[i].2 && segs.get(i + 1).is_some_and(|s| runs_input(&words(&s.0))))
{
// hlx:112
let b = words(&segs[i].1);
// hlx:113
for j in 0..b.len() {
// hlx:114
if b[j] == ">" || b[j] == ">>" {
// hlx:115
out.push(b[j].clone());
// hlx:116
if let Some(t) = b.get(j + 1) {
// hlx:117
out.push(t.clone());
// hlx:118
}
// hlx:119
}
// hlx:120
}
// hlx:121
continue;
// hlx:122
}
// hlx:123
out.extend(w);
// hlx:124
}
// hlx:125
out
// hlx:126
}
// hlx:129
fn alt_hits(pat: &str, word: &str) -> bool {
// hlx:130
let w = word.trim_end_matches(['/', '\\']).to_lowercase();
// hlx:131
let p = pat.to_lowercase();
// hlx:132
if let Some(sub) = p.strip_prefix('*') {
// hlx:133
return !sub.is_empty() && word.to_lowercase().contains(sub);
// hlx:134
}
// hlx:135
if let Some(head) = p.strip_suffix('+') {
// hlx:136
if let Some(rest) = w.strip_prefix(head) {
// hlx:137
let seg = rest.split(['/', '\\']).next().unwrap_or("");
// hlx:138
return !rest.is_empty() && !seg.contains(['*', '?', '[']) && !rest.contains('$');
// hlx:139
}
// hlx:140
return false;
// hlx:141
}
// hlx:142
if let Some((head, rest)) = p.split_once('^') {
// hlx:143
return w.starts_with(head) && [head, rest].concat().starts_with(w.as_str());
// hlx:144
}
// hlx:145
if let Some((a, b)) = p.split_once('?') {
// hlx:146
if w.len() <= a.len() + b.len() || !w.starts_with(a) || !w.ends_with(b) {
// hlx:147
return false;
// hlx:148
}
// hlx:149
let mid = &w[a.len()..w.len() - b.len()];
// hlx:150
return !mid.contains(['/', '\\', '*']);
// hlx:151
}
// hlx:152
w == p
// hlx:153
}
// hlx:156
fn token_hits(tok: &str, word: &str) -> bool {
// hlx:157
if let Some(p) = tok.strip_prefix('=') {
// hlx:158
return p.split('|').all(|a| {
a.strip_prefix('!')
.is_none_or(|n| n.is_empty() || !alt_hits(n, word))
}) && p
.split('|')
.any(|a| !a.is_empty() && !a.starts_with('!') && alt_hits(a, word));
// hlx:159
}
// hlx:160
match tok.strip_prefix('*') {
// hlx:161
Some("") => word == "*",
// hlx:162
Some(sub) => {
word.contains(sub)
|| (!sub.starts_with('-') && word.to_lowercase().contains(&sub.to_lowercase()))
}
// hlx:163
None => {
word == tok
|| (tok.chars().all(|c| c.is_ascii_alphabetic()) && word.eq_ignore_ascii_case(tok))
} // hlx:164
}
// hlx:165
}
// hlx:168
pub fn spec_matches(spec: &str, words: &[String]) -> Option<usize> {
// hlx:169
let mut at = 0;
// hlx:170
let mut positive = 0;
// hlx:171
let mut gap: Vec<&str> = Vec::new();
// hlx:172
for tok in spec.split_whitespace() {
// hlx:173
if let Some(g) = tok.strip_prefix('<') {
// hlx:174
gap.push(g);
// hlx:175
continue;
// hlx:176
}
// hlx:177
if let Some(neg) = tok.strip_prefix('!') {
// hlx:178
if words.iter().any(|w| token_hits(neg, w)) {
// hlx:179
return None;
// hlx:180
}
// hlx:181
continue;
// hlx:182
}
// hlx:183
let found = words.iter().skip(at).position(|w| token_hits(tok, w))?;
// hlx:184
if words[at..at + found]
.iter()
.any(|w| gap.iter().any(|g| token_hits(g, w)))
{
// hlx:185
return None;
// hlx:186
}
// hlx:187
gap.clear();
// hlx:188
at += found + 1;
// hlx:189
positive += 1;
// hlx:190
}
// hlx:191
(positive > 0).then_some(positive)
// hlx:192
}
// hlx:195
fn tag_spec(tag: &str) -> Option<&str> {
// hlx:196
tag.get(..4)
.filter(|p| p.eq_ignore_ascii_case("cmd:"))
.map(|_| &tag[4..])
// hlx:197
}
// hlx:200
pub fn has_cmd(tags: &str) -> bool {
// hlx:201
tags.split(';')
.any(|t| t.trim().to_lowercase().starts_with("cmd:"))
// hlx:202
}
// hlx:205
pub fn cmd_specs(tags: &str) -> Vec<String> {
// hlx:206
let mut v: Vec<String> = tags
.split(';')
.map(str::trim)
.filter(|t| t.to_lowercase().starts_with("cmd:"))
.map(str::to_owned)
.collect();
// hlx:207
v.sort();
// hlx:208
v
// hlx:209
}
// hlx:212
pub fn best_match(tags: &str, words: &[String]) -> Option<usize> {
// hlx:213
let mut best: Option<usize> = None;
// hlx:214
for t in tags.split(';') {
// hlx:215
let Some(spec) = tag_spec(t.trim()) else {
continue;
};
// hlx:216
if let Some(n) = spec_matches(spec, words) {
// hlx:217
if best.is_none_or(|b| n > b) {
// hlx:218
best = Some(n);
// hlx:219
}
// hlx:220
}
// hlx:221
}
// hlx:222
best
// hlx:223
}