#include "JetLint.h" #include #include #include #include #include #include // 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 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); } // "' 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]); } // "" 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, "') ? 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 whose attributes never become statement text: // 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 &wrappers, std::vector &findings, LintStats &stats) { std::vector 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; // 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("", 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 's opening tag, not its body, // so a nested is still counted and checked. } } namespace { bool fileMentions(const std::string &text, const std::vector &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 &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(" 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(" &paths, bool strict, std::ostream &out, std::ostream &err) { namespace fs = std::filesystem; std::vector files; bool error = false; for (const auto &p : paths) { std::error_code ec; if (fs::is_directory(p, ec)) { std::vector 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 // takes its statement from the caller's // content, so each use of it is checked like an . std::vector texts(files.size()); std::vector readOk(files.size(), false); std::vector 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 names{"sql"}; names.insert(names.end(), wrappers.begin(), wrappers.end()); std::vector 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 byKind; std::map 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 << " blocks" << (wrappers.empty() ? "" : " (and uses of tags that wrap )") << " 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; } }