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/getHumanBrainRna/getHumanBrainRna.c src/hg/oneShot/getHumanBrainRna/getHumanBrainRna.c index 60451e8..0535374 100644 --- src/hg/oneShot/getHumanBrainRna/getHumanBrainRna.c +++ src/hg/oneShot/getHumanBrainRna/getHumanBrainRna.c @@ -18,51 +18,55 @@ " getHumanBrainRna db output.psl\n" "options:\n" " -xxx=XXX\n" ); } static struct optionSpec options[] = { {NULL, 0}, }; struct hash *getBadLibs(struct sqlConnection *conn) /* Get hash of IDs of libs that we know are bad * (Orestes, etc) */ { struct hash *hash = hashNew(0); -struct sqlResult *sr = sqlGetResult(conn, - NOSQLINJ "select id from library where " - "name like 'Athersys RAGE%' or name like '%ORESTES%'"); +char query[1024]; +sqlSafef(query, sizeof query, + "select id from library where " + "name like 'Athersys RAGE%%' or name like '%%ORESTES%%'"); +struct sqlResult *sr = sqlGetResult(conn, query); char **row; while ((row = sqlNextRow(sr)) != NULL) { hashAdd(hash, row[0], NULL); } sqlFreeResult(&sr); return hash; } struct hash *getGoodTissues(struct sqlConnection *conn) /* Get hash of IDs of good tissues. */ { struct hash *hash = hashNew(0); -struct sqlResult *sr = sqlGetResult(conn, - NOSQLINJ "select id from tissue where " - "name like '%brain%' or name like '%amygdala%' or " - "name like '%hippocampus%' or name like '%cortex%' or " - "name like '%straitum%'"); +char query[1024]; +sqlSafef(query, sizeof query, + "select id from tissue where " + "name like '%%brain%%' or name like '%%amygdala%%' or " + "name like '%%hippocampus%%' or name like '%%cortex%%' or " + "name like '%%straitum%%'"); +struct sqlResult *sr = sqlGetResult(conn, query); char **row; while ((row = sqlNextRow(sr)) != NULL) { hashAdd(hash, row[0], NULL); } sqlFreeResult(&sr); return hash; } struct hash *getGoodAccs(struct sqlConnection *conn, struct hash *goodTissueHash, struct hash *badLibHash, int orgId) /* Get accessions of mRNA and ESTs that are of given organism, * given tissues, and not of given libraries. */ { @@ -112,49 +116,53 @@ if (hashLookup(qHash, qName)) { saveRow(row+1, 21, f); ++count; } } sqlFreeResult(&sr); verbose(1, "wrote %d %s\n", count, table); } void savePsl(struct sqlConnection *conn, struct hash *accHash, char *fileName) /* Save EST and mRNAs aligments that are in hash table to file */ { FILE *f = mustOpen(fileName, "w"); struct slName *table, *splicedEstList; -splicedEstList = sqlQuickList(conn, NOSQLINJ "show tables like 'chr%_intronEst'"); +char query[1024]; +sqlSafef(query, sizeof query, "show tables like 'chr%%_intronEst'"); +splicedEstList = sqlQuickList(conn, query); writeMatchingPsl(conn, "all_mrna", accHash, f); for (table = splicedEstList; table != NULL; table = table->next) writeMatchingPsl(conn, table->name, accHash, f); // writeMatchingPsl(conn, "all_est", accHash, f); carefulClose(&f); } void getHumanBrainRna(char *db, char *outFile) /* getHumanBrainRna - Get list of human brain RNAs. */ { struct sqlConnection *conn = sqlConnect(db); struct hash *badLibHash = getBadLibs(conn); verbose(1, "Got %d bad libraries\n", badLibHash->elCount); struct hash *goodTissueHash = getGoodTissues(conn); verbose(1, "Got %d good tissues\n", goodTissueHash->elCount); -int humanId = sqlQuickNum(conn, - NOSQLINJ "select id from organism where name = 'Homo sapiens'"); +char query[1024]; +sqlSafef(query, sizeof query, + "select id from organism where name = 'Homo sapiens'"); +int humanId = sqlQuickNum(conn, query); struct hash *goodAccHash = getGoodAccs(conn, goodTissueHash, badLibHash, humanId); verbose(1, "Got %d good accessions\n", goodAccHash->elCount); savePsl(conn, goodAccHash, outFile); } int main(int argc, char *argv[]) /* Process command line. */ { optionInit(&argc, argv, options); if (argc != 3) usage(); getHumanBrainRna(argv[1], argv[2]); return 0; }