File size: 4,397 Bytes
4525d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Argument-fidelity checker: does the Bash solution actually reference the same
concrete nouns (filenames, extensions, users, groups, hosts, services, procs,
ports, logfiles) named in the natural-language request?

`arg_mismatch(nl, bash)` returns a short reason string if the request names a
concrete argument the command does NOT use, else None. High precision: it only
flags token classes that must be shared, and only in the NL->Bash direction
(the request specifies X; the solution must honor X).

Used both as a standalone auditor over the dataset and as a generation gate.
"""
import re

FILE_EXTS = {"txt","log","csv","json","md","py","sh","tmp","bak","conf","yaml","yml",
             "xml","html","css","js","jpg","jpeg","png","gif","pdf","gz","old","sql",
             "zip","cache","tsv","ini","bz2","xz","core","img","iso","mp4"}

# distinctive vocab (kept in sync with generate.py pools; only tokens unlikely to
# appear incidentally as ordinary English are checked)
USERS = {"deploy","www-data","alice","bob","postgres","nginx","jenkins","ubuntu",
         "carol","git","backup","dave","erin","mysql","redis","ec2-user","admin",
         "svc-app","operator","tomcat"}
GROUPS = {"www-data","developers","staff","docker","sudo","admin","deploy","users",
          "wheel","operators","mysql","backup"}
SERVICES = {"nginx","docker","ssh","sshd","postgresql","mysql","mariadb","redis",
            "cron","apache2","firewalld","fail2ban","prometheus","grafana-server",
            "elasticsearch","rabbitmq","memcached","haproxy","containerd","chronyd",
            "systemd-networkd","mongod"}
PROCS = {"nginx","node","python","python3","java","mysqld","postgres","redis-server",
         "sshd","apache2","dockerd","gunicorn","celery","php-fpm","containerd","mongod",
         "rabbitmq","elasticsearch","memcached","haproxy"}

FILE_RE = re.compile(r'\b[a-zA-Z0-9_][a-zA-Z0-9_-]*\.[a-zA-Z0-9.]+\b')
def _files(s):
    out=set()
    for t in FILE_RE.findall(s):
        parts=t.lower().split(".")
        if t.lower().endswith("tar.gz") or t.lower().endswith("tar.bz2"):
            out.add(t)
        elif parts[-1] in FILE_EXTS:
            out.add(t)
    return out

EXT_NL_RE = re.compile(r'\.([a-z0-9]{1,5})\s+(?:files?|extension)\b')
def _ext_nl(s): return set(EXT_NL_RE.findall(s.lower()))
def _ext_bash(s): return set(re.findall(r'\.([a-z0-9]{1,5})', s.lower()))

def _words(s):
    return set(re.findall(r'[a-zA-Z0-9][a-zA-Z0-9._-]*', s.lower()))

PORT_NL_RE = re.compile(r'\bport\s+(\d{1,5})\b')

def arg_mismatch(nl, bash):
    nll, bashl = nl.lower(), bash.lower()
    # 1. filenames named in the request must appear in the command
    nlf = _files(nl)
    if nlf:
        bf = _files(bash) | _words(bash)
        missing = {f for f in nlf if f not in bashl}
        # allow a filename to be "used" if its stem appears (e.g. renamed target)
        if missing:
            return f"file(s) in request not in command: {sorted(missing)}"
    # 2. extension named as "<.ext> files" must appear in the command
    nle = _ext_nl(nl)
    if nle:
        be = _ext_bash(bash)
        miss = nle - be
        if miss:
            return f"extension(s) in request not in command: {sorted(miss)}"
    # 3. distinctive pool tokens (user/group/service/proc) named in request,
    #    but only when the request has a matching role cue (avoids flagging the
    #    ordinary English word "backup", "admin", ... ). Bash is bracket-stripped
    #    so grep's [s]shd exclusion trick still counts as containing "sshd".
    words_nl = _words(nl)
    words_bash = _words(bash.replace("[","").replace("]",""))
    ug_cue = re.search(r'\b(owner|own|owned|user|group|chown|chgrp|uid|gid|permission)\b', nll)
    sp_cue = re.search(r'\b(service|process|processes|daemon|running|restart|status|pid|kill|reload|stop|start)\b', nll)
    checks=[]
    if ug_cue: checks += [("user",USERS),("group",GROUPS)]
    if sp_cue: checks += [("service",SERVICES),("process",PROCS)]
    for label, vocab in checks:
        named = words_nl & vocab
        miss = {t for t in named if t not in words_bash}
        if miss:
            return f"{label}(s) in request not in command: {sorted(miss)}"
    # 4. explicit port number named in request must appear in command
    for p in PORT_NL_RE.findall(nll):
        if p not in bashl:
            return f"port {p} in request not in command"
    return None