| |
| """Best-effort compile/syntax checks per language for SecureCodePairs. |
| |
| Toolchains available on the build host are used for real checks: |
| Python (ast), C/C++ (gcc/g++ -fsyntax-only), Rust (rustc), Ruby (ruby -c), |
| JavaScript (node --check), Go (go vet) for standalone code, |
| Java (javac) for standalone code. |
| Framework-dependent snippets (Flask/Spring/Gin/Rails/Express/Next/ASP.NET/TS |
| modules) are detected via markers and fall back to a brace/paren heuristic so |
| the build stays reproducible without those packages installed. |
| Missing toolchains degrade gracefully (reported as warnings). |
| """ |
| import os |
| import shutil |
| import subprocess |
| import tempfile |
|
|
| |
| FRAMEWORK_MARKERS = { |
| "Java": ("org.springframework", "@RestController", "@Controller", "@Service", |
| "@SpringBootApplication", "@GetMapping", "@PostMapping", "@RequestMapping", |
| "@Repository", "import org."), |
| "Go": ("gin.", "github.com/gin-gonic", "google.golang.org/grpc", "func "), |
| "TypeScript": ("import ", "export ", "@"), |
| "C#": ("using Microsoft", "[ApiController]", "ControllerBase", "using "), |
| "PHP": ("Laravel", "Illuminate", "Artisan", "<?php", "namespace "), |
| "Kotlin": ("android.", "import androidx", "findViewById", "import "), |
| "Swift": ("import UIKit", "import SwiftUI", "@objc", "import "), |
| "Scala": ("akka", "play.api", "import scala", "object "), |
| "Rust": ("actix", "use actix", "use ", "rocket", "tokio", "impl "), |
| "Ruby": ("Rails", "ActiveRecord", "gem ", "class ", "def "), |
| "Python": ("from flask", "from django", "from fastapi", "import "), |
| "JavaScript": ("require(", "import ", "app.", "module.exports", "@Controller", "@Injectable", "@Get", "@Post"), |
| "C++": ("QProcess", "QString", "Qt", "QApplication", "#include <Q"), |
| "C": ("#include", "func "), |
| } |
|
|
| CHECKERS = {} |
|
|
|
|
| def _have(cmd): |
| return shutil.which(cmd) is not None |
|
|
|
|
| def _write_temp(ext, code): |
| fd, path = tempfile.mkstemp(suffix=ext) |
| with os.fdopen(fd, "w", encoding="utf-8") as f: |
| f.write(code) |
| return path |
|
|
|
|
| def _framework(language, code): |
| for m in FRAMEWORK_MARKERS.get(language, ()): |
| if m in code: |
| return True |
| return False |
|
|
|
|
| def _heuristic(code, lang): |
| if code.count("{") != code.count("}"): |
| return False, f"brace mismatch {code.count('{')}/{code.count('}')}" |
| if code.count("(") != code.count(")"): |
| return False, "paren mismatch" |
| if code.count("[") != code.count("]"): |
| return False, "bracket mismatch" |
| return True, f"{lang}: heuristic (no/limited compiler)" |
|
|
|
|
| def check_python(code): |
| try: |
| import ast |
| ast.parse(code) |
| return True, "" |
| except SyntaxError as e: |
| return False, str(e) |
|
|
|
|
| def check_java(code): |
| if _framework("Java", code): |
| return _heuristic(code, "java") |
| if not _have("javac"): |
| return None, "javac missing" |
| p = _write_temp(".java", code) |
| try: |
| r = subprocess.run(["javac", "-d", tempfile.gettempdir(), p], |
| capture_output=True, text=True, timeout=60) |
| return (r.returncode == 0, r.stderr[:300]) |
| except Exception as e: |
| return False, str(e) |
| finally: |
| os.remove(p) |
|
|
|
|
| def check_go(code): |
| if _framework("Go", code): |
| return _heuristic(code, "go") |
| if not _have("go"): |
| return None, "go missing" |
| p = _write_temp(".go", code) |
| try: |
| r = subprocess.run(["go", "vet", p], capture_output=True, text=True, timeout=120) |
| return (r.returncode == 0, r.stderr[:300]) |
| except Exception as e: |
| return False, str(e) |
| finally: |
| os.remove(p) |
|
|
|
|
| def check_rust(code): |
| if not _have("rustc"): |
| return None, "rustc missing" |
| p = _write_temp(".rs", code) |
| try: |
| r = subprocess.run(["rustc", "--edition", "2021", "-o", os.devnull, p], |
| capture_output=True, text=True, timeout=120) |
| return (r.returncode == 0, r.stderr[:300]) |
| except Exception as e: |
| return False, str(e) |
| finally: |
| os.remove(p) |
|
|
|
|
| def check_c(code): |
| if _framework("C", code): |
| return _heuristic(code, "c") |
| if not _have("gcc"): |
| return None, "gcc missing" |
| p = _write_temp(".c", code) |
| try: |
| r = subprocess.run(["gcc", "-fsyntax-only", p], capture_output=True, text=True, timeout=60) |
| return (r.returncode == 0, r.stderr[:300]) |
| except Exception as e: |
| return False, str(e) |
| finally: |
| os.remove(p) |
|
|
|
|
| def check_cpp(code): |
| if _framework("C++", code): |
| return _heuristic(code, "cpp") |
| if not _have("g++"): |
| return None, "g++ missing" |
| p = _write_temp(".cpp", code) |
| try: |
| r = subprocess.run(["g++", "-fsyntax-only", p], capture_output=True, text=True, timeout=60) |
| return (r.returncode == 0, r.stderr[:300]) |
| except Exception as e: |
| return False, str(e) |
| finally: |
| os.remove(p) |
|
|
|
|
| def check_ruby(code): |
| if not _have("ruby"): |
| return None, "ruby missing" |
| p = _write_temp(".rb", code) |
| try: |
| r = subprocess.run(["ruby", "-c", p], capture_output=True, text=True, timeout=30) |
| return (r.returncode == 0, r.stderr[:300]) |
| except Exception as e: |
| return False, str(e) |
| finally: |
| os.remove(p) |
|
|
|
|
| def check_js(code): |
| if not _have("node"): |
| return None, "node missing" |
| p = _write_temp(".js", code) |
| try: |
| r = subprocess.run(["node", "--check", p], capture_output=True, text=True, timeout=30) |
| return (r.returncode == 0, r.stderr[:300]) |
| except Exception as e: |
| return False, str(e) |
| finally: |
| os.remove(p) |
|
|
|
|
| def check_ts(code): |
| |
| return _heuristic(code, "ts") |
|
|
|
|
| def check_csharp(code): |
| return _heuristic(code, "csharp") |
|
|
|
|
| def check_scala(code): |
| return _heuristic(code, "scala") |
|
|
|
|
| def check_kotlin(code): |
| return _heuristic(code, "kotlin") |
|
|
|
|
| def check_swift(code): |
| return _heuristic(code, "swift") |
|
|
|
|
| def check_php(code): |
| return _heuristic(code, "php") |
|
|
|
|
| CHECKERS = { |
| "Python": check_python, |
| "Java": check_java, |
| "Go": check_go, |
| "Rust": check_rust, |
| "C": check_c, |
| "C++": check_cpp, |
| "Ruby": check_ruby, |
| "JavaScript": check_js, |
| "TypeScript": check_ts, |
| "C#": check_csharp, |
| "Scala": check_scala, |
| "Kotlin": check_kotlin, |
| "Swift": check_swift, |
| "PHP": check_php, |
| } |
|
|
|
|
| def check(language: str, code: str): |
| fn = CHECKERS.get(language) |
| if not fn: |
| return None, "no checker" |
| return fn(code) |
|
|