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/geneBounds/estLibStats/estLibStats.c src/hg/geneBounds/estLibStats/estLibStats.c index 397c72f..baae6bc 100644 --- src/hg/geneBounds/estLibStats/estLibStats.c +++ src/hg/geneBounds/estLibStats/estLibStats.c @@ -132,31 +132,33 @@ struct libInfo *addEsts(char *database, struct hash *eiHash, struct hash *libHash, struct hash *authorHash, struct hash *liHash) /* Read in all ESTs from mRNA table in database and add them to * library they belong to. */ { struct libInfo *liList = NULL, *li; struct estOrientInfo *ei; struct sqlConnection *conn = sqlConnect(database); struct sqlResult *sr = NULL; char liId[256]; char **row; struct nameId *library, *author; -sr = sqlGetResult(conn, NOSQLINJ "select acc,library,author,direction from mrna where type = 'EST'"); +char query[1024]; +sqlSafef(query, sizeof query, "select acc,library,author,direction from mrna where type = 'EST'"); +sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) { library = hashMustFindVal(libHash, row[1]); author = hashMustFindVal(authorHash, row[2]); sprintf(liId, "%s.%s", library->id, author->id); if ((li = hashFindVal(liHash, liId)) == NULL) { AllocVar(li); li->libName = library->name; li->author = author->name; hashAdd(liHash, liId, li); slAddHead(&liList, li); } li->estCount += 1; if ((ei = hashFindVal(eiHash, row[0])) != NULL) @@ -296,31 +298,33 @@ printGroupStats(f, li->unPrime); fprintf(f, "%s\t", li->libName); fprintf(f, "%s\n", li->author); } carefulClose(&f); } #ifdef NEEDSWORK void addSizes(char *database, struct hash *eiHash, struct hash *dateHash) /* Add and date info to ests in eiHash. Date strings are stored in dateHash */ { struct sqlConnection *conn = sqlConnect(database); struct sqlResult *sr = NULL; char **row; -sr = sqlGetResult(conn, NOSQLINJ "select acc,size,gb_date from seq"); +char query[1024]; +sqlSafef(query, sizeof query, "select acc,size,gb_date from seq"); +sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) { char *acc = row[0]; struct estInfo *ei = hashFindVal(eiHash, acc); if (ei != NULL) { ei->size = sqlUnsigned(row[1]); ei->date = hashStoreName(dateHash, row[2]); } } sqlFreeResult(&sr); sqlDisconnect(&conn); } #endif /* NEEDSWORK */