Here is a little work in progress snippet of a thi...
# share-your-work
e
Here is a little work in progress snippet of a thing. It applies a heat map kinda color scheme to git diffs, the idea being that its rules can be tuned to call out places in the code that need closer attention when reviewing the diff. The rules are very much still a work in progress, and I'm super duper open to suggestions for ways to approach creating these rules. Rambling video, be warned. I apologies for having the crunchiest, noisiest mouse wheel ever in the history of the recorded universe. Its like an ASMR halloween nightmare.
❤️ 6
tiny example of some of my rules
Copy code
// Security: Secrets and credentials (Score: 0.9, Red highlight)
  secrets: {
    score: 0.9,
    highlightClass: 'secret',
    patterns: [
      { regex: /(['"][a-zA-Z0-9]{32,}['"])/g, desc: 'Long alphanumeric strings' },
      { regex: /\b(api[_-]?key|secret|password|token|auth)\s*[:=]\s*['"][^'"]+['"]/gi, desc: 'Credential assignments' },
      { regex: /\b(sk-[a-zA-Z0-9]{32,}|pk-[a-zA-Z0-9]{32,})\b/g, desc: 'API keys (OpenAI, Stripe, etc)' },
      { regex: /\b([0-9a-f]{40,64})\b/g, desc: 'Hex keys (SHA hashes, tokens)' },
      { regex: /-----BEGIN (PRIVATE|RSA|OPENSSH) KEY-----/g, desc: 'Private keys' },
      { regex: /\b(ghp_|gho_|ghu_|ghs_|ghr_)[a-zA-Z0-9]{36,}\b/g, desc: 'GitHub tokens' },
      { regex: /\b(AKIA[0-9A-Z]{16})\b/g, desc: 'AWS access keys' }
    ]
  },
  
  // Security: Dangerous functions (Score: 0.85, Orange highlight)
  dangerous_functions: {
    score: 0.85,
    highlightClass: 'danger',
    patterns: [
      { regex: /\b(eval|exec|execFile|spawn)\s*\(/g, desc: 'Code execution', languages: ['javascript', 'typescript', 'python', 'ruby'] },
      { regex: /\b(innerHTML|outerHTML|document\.write|execScript)\b/g, desc: 'DOM injection', languages: ['javascript', 'typescript'] },
      { regex: /\b(system|shell_exec|passthru|proc_open)\s*\(/g, desc: 'Shell execution', languages: ['php'] },
      { regex: /\b(__import__|compile|globals|locals)\s*\(/g, desc: 'Dynamic imports', languages: ['python'] },
      { regex: /\b(eval|instance_eval|class_eval|module_eval)\b/g, desc: 'Dynamic evaluation', languages: ['ruby'] },
      { regex: /\b(Runtime\.getRuntime|ProcessBuilder)\b/g, desc: 'Process execution', languages: ['java'] },
      { regex: /\bsystem\s*\(/g, desc: 'System calls', languages: ['c', 'cpp', 'rust'] }
    ]
  },
  
  // Security: Weak cryptography (Score: 0.75, Yellow highlight)
  weak_crypto: {
    score: 0.75,
    highlightClass: 'warning',
    patterns: [
      { regex: /\b(md5|sha1|des|rc4)\b/gi, desc: 'Weak hash algorithms' },
      { regex: /Math\.random\(\)/g, desc: 'Insecure random', languages: ['javascript', 'typescript'] },
      { regex: /\brandom\(\)/g, desc: 'Insecure random', languages: ['php', 'python'] },
      { regex: /\bnew\s+Random\(\)/g, desc: 'Insecure random', languages: ['java', 'csharp'] }
    ]
  },
also, I swear that I'm not a monster who keeps their browser window smooshed up against the top while leaving gutter to the bottom and sides...my OBS is wonkily aligned since upgrading macOS...it is Apple's fault.
😂 1
❤️ 1
another approach to the rules that I've considered but haven't implemented yet, is to use AST parsing or something like tree sitter to help to build or apply the rules. So far, haven't been super convinced that'll be deeply better than what I'm doing beyond that having knowledge of the AST could allow me to approach whole logical blocks more easily than I can with regex rules.
m
This is awesome! Neat project. How are you approaching the definition of what makes for a good rule, especially without "intelligence" backing your ruleset, or is that outside of your concerns? ("Gnarly logic" seems like a tough one to do with pure regex 🙃)
I got curious about what the inspiring project uses as its rules and funnily enough, from what it looks like, they leave that _almost_ entirely up to gpt to decide :)
e
@Matt Curtis yeah, I found it kinda unsettling that the other system doesn't really have rules, it is rather trusting the model to do it all. Right now, rules are mostly me trying to think through what I know to be "fragile" or potentially hairy changes that require more attention. My goal isn't for this to be a linter or code quality tool, but instead a way to draw attention to places that could be borked up, and that need human eyes to linger on them.
🔥 1
g
If you do happen to look at other parsing technologies, do look at ohmjs. OhmJS is better than PEG, PEG is better than LR. REGEX is unsuitable for anything with structure beyond line-at-a-time. In addition, I like Prolog for defining and exhaustively searching rules. The easiest-to-understand Prolog that I’ve encountered is Nils Holm’s Prolog control in 6 slides. I ported it to javascript, then didn’t bother using it when I found out how simple and fast it was to shell out to SWIPL. (I also ported it to common lisp). At one point, I used OhmJS to give myself a quikie DSL that allowed me to express rules in Prolog and to format results in JS, and, produced a bash script to choreograph the whole thing. It ran in the blink of an eye - I wouldn’t use it in production code, but, I did use it my development workflow. (Aside: we used backtracking TXL to create date-finding rules and to process bank code before Y2K).
e
oh! Thanks for the call out of ohmjs! This looks interesting, and maybe, might break me of my unwillingness to use other people's code in a thing -- Prolog was actually my first thought, or a constraints systems, but I couldn't quickly come up with a way for how to write anything like reusable logic for various languages in something shaped like prolog.
g
FYI - there’s a discord for OhmJS (Ohmland) and several experts are probably reachable in this slack (@Patrick Dubroy @Mariano Guerra). Further FYI - I consider that my programming language is “UNIX pipes”. This lets me use multiple languages to solve any problem in multiple paradigms. I find this to be more productive than simply trying to force-fit just one language onto my problem(s). OhmJS makes it even easier to glue stuff like this together (and, I’ve gone way down that rabbit hole creating my own little DSLs and using OhmJS to parse and transmogrify diagrams saved out as XML, graphML, SVG, …)
k
@Eli Mellen A nice case of Moldable Development!
💯 1
e
Added rules for accessibility changes to markup.
🧠 1
❤️ 1