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/checkTableCoords/checkTableCoords.c src/hg/checkTableCoords/checkTableCoords.c index 65d117f..ad13eb3 100644 --- src/hg/checkTableCoords/checkTableCoords.c +++ src/hg/checkTableCoords/checkTableCoords.c @@ -99,31 +99,36 @@ * own so that we can use the test database for testing. */ { struct sqlConnection *conn = hAllocConn(db); char query[1024]; sqlSafef(query, sizeof(query), "select size from chromInfo where chrom = '%s'", chrom); int size = sqlQuickNum(conn, query); hFreeConn(&conn); return size; } struct slName *getTableNames(struct sqlConnection *conn) /* Return a list of names of tables that have not been excluded by * command line options. */ { -char *query = hoursOld ? NOSQLINJ "show table status" : "NOSQLINJ show tables"; +char query[1024]; +if (hoursOld) + sqlSafef(query, sizeof query, "show table status"); +else + sqlSafef(query, sizeof query, "show tables"); + struct sqlResult *sr = sqlGetResult(conn, query); struct slName *tableList = NULL; char **row = NULL; int startTime = clock1(); int ageThresh = hoursOld * 3600; while((row = sqlNextRow(sr)) != NULL) { struct slName *tableName = NULL; struct slName *pat = NULL; boolean gotMatch = FALSE; if (hoursOld) { if (row[11] != NULL) { @@ -462,86 +467,88 @@ if (splitAndNonSplitExist(conn, table, tableChrom)) continue; safef(tableChromPrefix, sizeof(tableChromPrefix), "%s_", tableChrom); if (hti->isSplit) chromList = newSlName(tableChrom); else chromList = allChroms; /* invariant: chrom must be described in chromInfo. */ /* items with bad chrom will be invisible to hGetBedRange(), so * catch them here by SQL query. */ /* The SQL query is too huge for scaffold-based db's, check count: */ if (hChromCount(db) <= MAX_SEQS_SUPPORTED) { if (isNotEmpty(hti->chromField)) { - struct dyString *bigQuery = newDyString(1024); + struct dyString *bigQuery = dyStringNew(1024); dyStringClear(bigQuery); sqlDyStringPrintf(bigQuery, "select count(*) from %s where ", table); for (chromPtr=chromList; chromPtr != NULL; chromPtr=chromPtr->next) { sqlDyStringPrintf(bigQuery, "%s != '%s' ", hti->chromField, chromPtr->name); if (chromPtr->next != NULL) - dyStringAppend(bigQuery, "AND "); + sqlDyStringPrintf(bigQuery, "AND "); } gotError |= reportErrors(BAD_CHROM, table, sqlQuickNum(conn, bigQuery->string)); dyStringFree(&bigQuery); } for (chromPtr=chromList; chromPtr != NULL; chromPtr=chromPtr->next) { char *chrom = chromPtr->name; struct bed *bedList = hGetBedRange(db, table, chrom, 0, 0, NULL); if (hti->isSplit && isNotEmpty(hti->chromField)) gotError |= checkSplitTableOnlyChrom(bedList, table, hti, tableChrom); gotError |= checkStartEnd(bedList, table, hti, testChromSize(chrom)); if (hti->hasCDS) gotError |= checkCDSStartEnd(bedList, table, hti); if (hti->hasBlocks && !ignoreBlocks) gotError |= checkBlocks(bedList, table, hti); bedFreeList(&bedList); } } else { // For scaffold-based databases, compare the number of rows with chrom found in // chromInfo to the number of rows -- if not the same, then some rows have bad chrom. - int rowCount = sqlRowCount(conn, table); + char queryTblSafe[1024]; + sqlSafef(queryTblSafe, sizeof queryTblSafe, "%s", table); + int rowCount = sqlRowCount(conn, queryTblSafe); char query[2048]; sqlSafef(query, sizeof(query), "select count(*) from %s, chromInfo " "where %s.%s = chromInfo.chrom", table, table, hti->chromField); int okChromRowCount = sqlQuickNum(conn, query); int badChromRowCount = rowCount - okChromRowCount; if (badChromRowCount) gotError |= reportErrors(BAD_CHROM, table, badChromRowCount); } } } return gotError; } void processExcludes(char *exclude) /* Combine alwaysExclude and command like -exclude arg (if given), and * process into a list. If it contains "genbank", add genbankExclude. */ { -struct dyString *allExcludes = newDyString(512); +struct dyString *allExcludes = dyStringNew(512); char *patterns[128]; int numPats = 0, i = 0; dyStringAppend(allExcludes, alwaysExclude); if (exclude != NULL) dyStringPrintf(allExcludes, ",%s", exclude); numPats = chopCommas(allExcludes->string, patterns); for (i=0; i < numPats; i++) { if (sameWord(patterns[i], "genbank")) { char *gbPatterns[128]; int gbNumPats = 0, j = 0; char *gbExclude = cloneString(genbankExclude); gbNumPats = chopCommas(gbExclude, gbPatterns);