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/cirm/cdw/cdwRunOnIds/cdwRunOnIds.c src/hg/cirm/cdw/cdwRunOnIds/cdwRunOnIds.c index 2462793..ae73f17 100644 --- src/hg/cirm/cdw/cdwRunOnIds/cdwRunOnIds.c +++ src/hg/cirm/cdw/cdwRunOnIds/cdwRunOnIds.c @@ -1,81 +1,81 @@ /* cdwRunOnIds - Run a cdw command line program (one that takes startId endId as it's two parameters) for a range of ids, putting it on cdwJob queue.. */ /* Copyright (C) 2013 The Regents of the University of California * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */ #include "common.h" #include "linefile.h" #include "hash.h" #include "options.h" #include "jksql.h" #include "cdw.h" #include "cdwLib.h" char *runTable = "cdwTempJob"; boolean dry = FALSE; boolean one = FALSE; void usage() /* Explain usage and exit. */ { errAbort( "cdwRunOnIds - Run a cdw command line program (one that takes startId endId as it's two parameters) for a range of ids, putting it on cdwJob queue.\n" "usage:\n" " cdwRunOnIds program 'queryString'\n" "Where queryString is a SQL command that should return a list of fileIds.\n" "Example\n" " cdwRunOnIds cdwFixQualScore 'select fileId from cdwFastqFile where qualMean < 0'" "options:\n" " -runTable=%s -job table to use\n" " -dry - just print out what we would do\n" " -one - program takes just a single parameter\n" , runTable ); } /* Command line validation table. */ static struct optionSpec options[] = { {"runTable", OPTION_STRING}, {"dry", OPTION_BOOLEAN}, {"one", OPTION_BOOLEAN}, {NULL, 0}, }; void cdwRunOnIds(char *program, char *queryString) /* cdwRunOnIds - Run a cdw command line program (one that takes startId endId as it's two parameters) for a range of ids, * putting it on cdwJob queue. */ { -struct dyString *wrappedQuery = dyStringNew(0); -sqlDyStringPrintf(wrappedQuery, "%-s", queryString); // trust +char wrappedQuery[4096]; +sqlSafef(wrappedQuery, sizeof wrappedQuery, queryString, NULL); // trust query from the commandline struct sqlConnection *conn = cdwConnectReadWrite(); -struct slName *id, *idList = sqlQuickList(conn, wrappedQuery->string); +struct slName *id, *idList = sqlQuickList(conn, wrappedQuery); for (id = idList; id != NULL; id = id->next) { char query[512]; if (one) sqlSafef(query, sizeof(query), "insert into %s (commandLine) values ('%s %s')", runTable, program, id->name); else sqlSafef(query, sizeof(query), "insert into %s (commandLine) values ('%s %s %s')", runTable, program, id->name, id->name); if (dry) printf("%s\n", query); else sqlUpdate(conn, query); } } int main(int argc, char *argv[]) /* Process command line. */ { optionInit(&argc, argv, options); if (argc != 3) usage(); runTable = optionVal("runTable", runTable); dry = optionExists("dry"); one = optionExists("one"); cdwRunOnIds(argv[1], argv[2]); return 0; }