44ccfacbe3a3d4b300f80d48651c77837a4b571e galt Tue Apr 26 11:12:02 2022 -0700 SQL INJECTION Prevention Version 2 - this improves our methods by making subclauses of SQL that get passed around be both easy and correct to use. The way that was achieved was by getting rid of the obscure and not well used functions sqlSafefFrag and sqlDyStringPrintfFrag and replacing them with the plain versions of those functions, since these are not needed anymore. The new version checks for NOSQLINJ in unquoted %-s which is used to include SQL clauses, and will give an error the NOSQLINJ clause is not present, and this will automatically require the correct behavior by developers. sqlDyStringPrint is a very useful function, however because it was not enforced, users could use various other dyString functions and they operated without any awareness or checking for SQL correct use. Now those dyString functions are prohibited and it will produce an error if you try to use a dyString function on a SQL string, which is simply detected by the presence of the NOSQLINJ prefix. diff --git src/hg/oneShot/cacheMergedRanges/cacheMergedRanges.c src/hg/oneShot/cacheMergedRanges/cacheMergedRanges.c index 39fb906..ff2982e 100644 --- src/hg/oneShot/cacheMergedRanges/cacheMergedRanges.c +++ src/hg/oneShot/cacheMergedRanges/cacheMergedRanges.c @@ -273,31 +273,33 @@ freeMem(prevChrom); } return chromHash; } struct hash *sqlSorting(struct sqlConnection *conn, char *table) /* Grab contents of bed table into chrom-hashed range lists, * using SQL to sort by chromStart; then merge ranges and * convert to chrom-hashed rbTrees with normal rbTreeAdd. */ { char *query = makeQuery(table, TRUE); struct sqlResult *sr = NULL; struct hash *chromTrees = NULL; int startMs = 0, endMs = 0, totalMs = 0; -sqlUpdate(conn, NOSQLINJ "reset query cache"); +char query2[1024]; +sqlSafef(query, sizeof query2, "reset query cache"); +sqlUpdate(conn, query2); startMs = clock1000(); sr = sqlGetResult(conn, query); endMs = clock1000(); verbose(1, "sqlSorting: Took %dms to do sorting query.\n", endMs - startMs); printf("%d\t", endMs - startMs); totalMs += (endMs - startMs); startMs = clock1000(); chromTrees = resultsToTreesMergeInline(sr); endMs = clock1000(); verbose(1, "sqlSorting: Took %dms to build, merge and convert.\n", endMs - startMs); printf("%d\t", endMs - startMs); totalMs += (endMs - startMs); @@ -307,31 +309,33 @@ sqlFreeResult(&sr); return chromTrees; } struct hash *jkSorting(struct sqlConnection *conn, char *table) /* Grab contents of bed table into chrom-hashed range lists, * using slSort() to sort by start; then merge ranges and * convert to chrom-hashed rbTrees with normal rbTreeAdd. */ { char *query = makeQuery(table, FALSE); struct sqlResult *sr = NULL; struct hash *chromTrees = NULL; int startMs = 0, endMs = 0, totalMs = 0; -sqlUpdate(conn, NOSQLINJ "reset query cache"); +char query2[1024]; +sqlSafef(query2, sizeof query2, "reset query cache"); +sqlUpdate(conn, query2); startMs = clock1000(); sr = sqlGetResult(conn, query); endMs = clock1000(); verbose(1, "jkSorting: Took %dms to do plain query.\n", endMs - startMs); printf("%d\t", endMs - startMs); totalMs += (endMs - startMs); startMs = clock1000(); chromTrees = resultsToTrees(sr, TRUE); endMs = clock1000(); verbose(1, "jkSorting: Took %dms to build, sort, merge, and convert.\n", endMs - startMs); printf("%d\t", endMs - startMs); totalMs += (endMs - startMs); @@ -386,31 +390,33 @@ } return chromHash; } struct hash *rbMerging(struct sqlConnection *conn, char *table) /* Grab contents of bed table ordered by chrom but not sorted, * then convert to chrom-hashed rbmTrees to handle merging of * overlapping ranges. */ { char *query = makeQuery(table, FALSE); struct sqlResult *sr = NULL; struct hash *chromTrees = NULL; int startMs = 0, endMs = 0, totalMs = 0; -sqlUpdate(conn, NOSQLINJ "reset query cache"); +char query2[1024]; +sqlSafef(query2, sizeof query2, "reset query cache"); +sqlUpdate(conn, query2); startMs = clock1000(); sr = sqlGetResult(conn, query); endMs = clock1000(); verbose(1, "rbMerging: Took %dms to do plain query.\n", endMs - startMs); printf("%d\t", endMs - startMs); totalMs += (endMs - startMs); startMs = clock1000(); chromTrees = resultsToMergedTrees(sr); endMs = clock1000(); verbose(1, "rbMerging: Took %dms to make merged trees.\n", endMs - startMs); printf("%d\t", endMs - startMs); totalMs += (endMs - startMs); verbose(1, "rbMerging: Took %dms total.\n\n", totalMs);