Add jetc --lint-sql to find <sql> that pastes values (phase 2)

Implements phase 2 of docs/design-sql-bound-parameters.md. jetc --lint-sql
[--strict] <file|dir>... lists each <sql> whose statement contains a $[...]
or #[...] reference instead of a <bind>, with file:line and where each value
comes from. Row values ($[.col], $[session.col], $[?N], $[#]) are only
reported with --strict. References in <bind>, <if>, <set> and the other
attribute-only tags, <comment> and |> <| are skipped. A custom tag defined
as <sql><container></sql> (barant.com's <showtable>) is checked at each use.
Exit status is 0 for none found, 1 for findings, 2 for an unreadable path.

- jetc/JetLint.*: the scanner; jetc.cpp: the option
- build scripts and runbindtests.sh: link JetLint.cpp
- tests/runlinttests.sh and tests/lintsql/: fixtures and expected output
- docs: worklist for the local copies of both sites, design doc status,
  manual note on the lint

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-20 20:29:28 -08:00
1 parent fd81bb877f
commit 2847ce5fa7
22 files changed
+667 -10

No files matched your search

+322
View File
@@ -0,0 +1,322 @@
#include "JetLint.h"
#include <algorithm>
#include <cctype>
#include <filesystem>
#include <fstream>
#include <map>
#include <sstream>
// See JetLint.h. This is a plain text scan of the page source rather than a
// use of jetc's Parser: the parser keeps no line numbers, compiles some
// content into fallback spans, and stops at the first error, while a lint
// run over a whole site has to report every <sql> in every file.
namespace jetc {
namespace {
bool startsWithNoCase(const std::string &s, size_t pos, const char *word) {
for (size_t i = 0; word[i]; ++i)
if (pos + i >= s.size() || std::tolower((unsigned char)s[pos + i]) != word[i]) return false;
return true;
}
bool isTagEndChar(char c) {
return c == '>' || c == '/' || std::isspace((unsigned char)c);
}
// "<name" at pos, followed by whitespace, '>' or '/'.
bool isOpenTag(const std::string &s, size_t pos, const std::string &name) {
if (s[pos] != '<' || !startsWithNoCase(s, pos + 1, name.c_str())) return false;
size_t after = pos + 1 + name.size();
return after < s.size() && isTagEndChar(s[after]);
}
// "</name" + optional whitespace + ">" at pos. Returns the position
// after the '>', or npos.
size_t closeTagEnd(const std::string &s, size_t pos, const std::string &name) {
if (s.compare(pos, 2, "</") != 0 || !startsWithNoCase(s, pos + 2, name.c_str())) return std::string::npos;
size_t i = pos + 2 + name.size();
while (i < s.size() && std::isspace((unsigned char)s[i])) ++i;
return (i < s.size() && s[i] == '>') ? i + 1 : std::string::npos;
}
// pos is at '<'. Returns the position of the '>' ending the opening
// tag, skipping quoted attribute values, or npos.
size_t openTagEnd(const std::string &s, size_t pos) {
char quote = 0;
for (size_t i = pos + 1; i < s.size(); ++i) {
char c = s[i];
if (quote) { if (c == quote) quote = 0; }
else if (c == '"' || c == '\'') quote = c;
else if (c == '>') return i;
}
return std::string::npos;
}
// pos is at "$[" or "#[". Returns the position after the matching
// ']' (nested references included), or npos.
size_t refEnd(const std::string &s, size_t pos) {
int depth = 0;
for (size_t i = pos; i < s.size(); ++i) {
if ((s[i] == '$' || s[i] == '#') && i + 1 < s.size() && s[i + 1] == '[') { ++depth; ++i; }
else if (s[i] == ']' && --depth == 0) return i + 1;
}
return std::string::npos;
}
// What a reference reads from, from its type designator (see
// Tag::getVariable). "row" is a column of an earlier query.
std::string refKind(const std::string &ref) {
if (ref[0] == '#') return "local";
char d = ref.size() > 2 ? ref[2] : 0;
switch (d) {
case ':': return "cgi";
case '@': return "session";
case '&': return "env";
case '%': return "keyword";
case '!': return "global";
case '.': case '?': case '#': return "row";
}
// $[session.column] - a dot outside any nested reference.
int depth = 0;
for (size_t i = 2; i + 1 < ref.size(); ++i) {
if (ref[i] == '[') ++depth;
else if (ref[i] == ']') --depth;
else if (ref[i] == '.' && depth == 0) return "row";
}
return "global";
}
int lineAt(const std::string &s, size_t pos) {
return 1 + (int)std::count(s.begin(), s.begin() + pos, '\n');
}
// Tags inside a <sql> whose attributes never become statement text:
// <bind> passes its value safely, and the rest only decide what the
// body renders.
const char *const attributeOnlyTags[] = {
"bind", "if", "else", "ifrow", "while", "whilerow", "until", "for", "set",
};
}
void lintSqlText(const std::string &file, const std::string &s, bool strict,
const std::vector<std::string> &wrappers,
std::vector<LintFinding> &findings, LintStats &stats) {
std::vector<std::string> names{"sql"};
names.insert(names.end(), wrappers.begin(), wrappers.end());
bool counted = false;
size_t pos = 0;
while ((pos = s.find('<', pos)) != std::string::npos) {
std::string name;
for (const auto &n : names)
if (isOpenTag(s, pos, n)) { name = n; break; }
if (name.empty()) { ++pos; continue; }
if (!counted) { ++stats.files; counted = true; }
++stats.sqlBlocks;
size_t tagStart = pos;
size_t gt = openTagEnd(s, pos);
if (gt == std::string::npos) break;
pos = gt + 1;
if (s[gt - 1] == '/') continue; // <sql ... /> has no statement
LintFinding finding{file, lineAt(s, tagStart), name, {}};
int depth = 1;
size_t i = pos;
while (i < s.size()) {
size_t end;
if (s[i] == '<') {
if ((end = closeTagEnd(s, i, name)) != std::string::npos) {
i = end;
if (--depth == 0) break;
continue;
}
if (isOpenTag(s, i, name)) {
// A nested block of the same tag is its own block; it is
// reported when the outer scan reaches it.
++depth;
size_t e = openTagEnd(s, i);
i = (e == std::string::npos) ? s.size() : e + 1;
continue;
}
if (isOpenTag(s, i, "comment")) {
size_t c = s.find("</comment>", i);
i = (c == std::string::npos) ? s.size() : c + 10;
continue;
}
bool skipped = false;
for (const char *tag : attributeOnlyTags)
if (isOpenTag(s, i, tag)) {
size_t e = openTagEnd(s, i);
i = (e == std::string::npos) ? s.size() : e + 1;
skipped = true;
break;
}
if (skipped) continue;
} else if (s.compare(i, 2, "|>") == 0) {
// |> ... <| is never parsed, so nothing in it is a reference.
size_t c = s.find("<|", i + 2);
i = (c == std::string::npos) ? s.size() : c + 2;
continue;
} else if ((s[i] == '$' || s[i] == '#') && i + 1 < s.size() && s[i + 1] == '[') {
end = refEnd(s, i);
if (end == std::string::npos) end = s.size();
LintRef ref{s.substr(i, end - i), ""};
ref.kind = refKind(ref.text);
i = end;
if (ref.kind == "row" && !strict) continue;
bool seen = false;
for (auto &r : finding.refs) seen = seen || r.text == ref.text;
if (!seen) finding.refs.push_back(ref);
continue;
}
++i;
}
if (!finding.refs.empty()) {
++stats.flaggedBlocks;
findings.push_back(finding);
}
// Carry on from the end of this <sql>'s opening tag, not its body,
// so a nested <sql> is still counted and checked.
}
}
namespace {
bool fileMentions(const std::string &text, const std::vector<std::string> &names) {
for (size_t p = 0; (p = text.find('<', p)) != std::string::npos; ++p)
for (const auto &n : names)
if (isOpenTag(text, p, n)) return true;
return false;
}
bool readFile(const std::string &path, std::string &text) {
std::ifstream in(path, std::ios::binary);
if (!in) return false;
std::ostringstream ss;
ss << in.rdbuf();
text = ss.str();
return true;
}
}
void findSqlWrappers(const std::string &s, std::vector<std::string> &wrappers) {
for (size_t pos = 0; (pos = s.find('<', pos)) != std::string::npos; ++pos) {
if (!isOpenTag(s, pos, "tag")) continue;
size_t gt = openTagEnd(s, pos);
if (gt == std::string::npos) return;
std::string open = s.substr(pos, gt - pos);
// name="X" (or name='X')
std::string lower = open;
for (auto &c : lower) c = (char)std::tolower((unsigned char)c);
size_t n = lower.find("name=");
if (n == std::string::npos || n + 5 >= open.size()) continue;
char q = open[n + 5];
if (q != '"' && q != '\'') continue;
size_t e = open.find(q, n + 6);
if (e == std::string::npos) continue;
std::string name = open.substr(n + 6, e - n - 6);
// Tag names match without regard to case (isOpenTag lowercases).
for (auto &c : name) c = (char)std::tolower((unsigned char)c);
size_t close = std::string::npos;
for (size_t c = gt; (c = s.find("</", c)) != std::string::npos; ++c)
if (closeTagEnd(s, c, "tag") != std::string::npos) { close = c; break; }
std::string body = s.substr(gt + 1, (close == std::string::npos ? s.size() : close) - gt - 1);
// An <sql> in the definition whose statement is the caller's content.
for (size_t p = 0; (p = body.find('<', p)) != std::string::npos; ++p) {
if (!isOpenTag(body, p, "sql")) continue;
size_t bgt = openTagEnd(body, p);
if (bgt == std::string::npos) break;
size_t end = std::string::npos;
for (size_t c = bgt; (c = body.find("</", c)) != std::string::npos; ++c)
if (closeTagEnd(body, c, "sql") != std::string::npos) { end = c; break; }
std::string stmt = body.substr(bgt + 1, (end == std::string::npos ? body.size() : end) - bgt - 1);
bool hasContainer = false;
for (size_t c = 0; (c = stmt.find('<', c)) != std::string::npos; ++c)
hasContainer = hasContainer || isOpenTag(stmt, c, "container");
if (hasContainer && !name.empty() &&
std::find(wrappers.begin(), wrappers.end(), name) == wrappers.end()) {
wrappers.push_back(name);
break;
}
}
}
}
int lintSql(const std::vector<std::string> &paths, bool strict, std::ostream &out, std::ostream &err) {
namespace fs = std::filesystem;
std::vector<std::string> files;
bool error = false;
for (const auto &p : paths) {
std::error_code ec;
if (fs::is_directory(p, ec)) {
std::vector<std::string> found;
for (auto it = fs::recursive_directory_iterator(p, fs::directory_options::skip_permission_denied, ec);
it != fs::recursive_directory_iterator(); it.increment(ec)) {
if (ec) break;
std::string name = it->path().filename().string();
if (it->is_directory(ec) && !name.empty() && name[0] == '.') { it.disable_recursion_pending(); continue; }
if (it->is_regular_file(ec)) found.push_back(it->path().string());
}
std::sort(found.begin(), found.end());
files.insert(files.end(), found.begin(), found.end());
} else if (fs::is_regular_file(p, ec)) {
files.push_back(p);
} else {
err << "jetc: cannot open " << p << std::endl;
error = true;
}
}
// Two passes: a custom tag defined anywhere in the scanned files as
// <sql ...><container></sql> takes its statement from the caller's
// content, so each use of it is checked like an <sql>.
std::vector<std::string> texts(files.size());
std::vector<bool> readOk(files.size(), false);
std::vector<std::string> wrappers;
for (size_t i = 0; i < files.size(); ++i) {
if (!readFile(files[i], texts[i])) { err << "jetc: cannot read " << files[i] << std::endl; error = true; continue; }
readOk[i] = true;
findSqlWrappers(texts[i], wrappers);
}
std::vector<std::string> names{"sql"};
names.insert(names.end(), wrappers.begin(), wrappers.end());
std::vector<LintFinding> findings;
LintStats stats;
for (size_t i = 0; i < files.size(); ++i) {
if (!readOk[i] || !fileMentions(texts[i], names)) continue;
lintSqlText(files[i], texts[i], strict, wrappers, findings, stats);
}
std::map<std::string, int> byKind;
std::map<std::string, bool> flaggedFiles;
for (const auto &f : findings) {
flaggedFiles[f.file] = true;
out << f.file << ":" << f.line << ": <" << f.tag << ">"
<< (f.tag == "sql" ? "" : " (wraps sql)") << " pastes ";
for (size_t i = 0; i < f.refs.size(); ++i) {
out << (i ? ", " : "") << f.refs[i].text << " (" << f.refs[i].kind << ")";
++byKind[f.refs[i].kind];
}
out << "\n";
}
out << "lint-sql: " << stats.flaggedBlocks << " of " << stats.sqlBlocks
<< " <sql> blocks" << (wrappers.empty() ? "" : " (and uses of tags that wrap <sql>)")
<< " paste values into the statement, in " << flaggedFiles.size()
<< " of " << stats.files << " files";
if (!byKind.empty()) {
out << " (";
bool first = true;
for (auto &[kind, n] : byKind) { out << (first ? "" : ", ") << kind << " " << n; first = false; }
out << ")";
}
out << std::endl;
if (error) return 2;
return findings.empty() ? 0 : 1;
}
}