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/protein/kgAttachKegg/kgAttachKegg.c src/hg/protein/kgAttachKegg/kgAttachKegg.c index 8ed540f..e7ace03 100644 --- src/hg/protein/kgAttachKegg/kgAttachKegg.c +++ src/hg/protein/kgAttachKegg/kgAttachKegg.c @@ -24,47 +24,51 @@ static struct optionSpec options[] = { {NULL, 0}, }; void kgAttachKegg(char *database, char *locusLinkToPathway, char *knownToKegg) /* kgAttachKegg - Attach UCSC genes to KEGG pathways via locusLink IDs. */ { /* Build up hash keyed by locus link ID with KEGG pathway id's as value. */ struct hash *llToKegg = hashTwoColumnFile(locusLinkToPathway); verbose(1, "Got %d items in %s\n", llToKegg->elCount, locusLinkToPathway); /* Build up hash keyed by refSeq accession (without version) with UCSC known gene values. */ struct sqlConnection *conn = sqlConnect(database); struct hash *ucscToRef = hashNew(16); -struct sqlResult *sr = sqlGetResult(conn, NOSQLINJ "select * from knownToRefSeq"); +char query[1024]; +sqlSafef(query, sizeof query, "select * from knownToRefSeq"); +struct sqlResult *sr = sqlGetResult(conn, query); char **row; while ((row = sqlNextRow(sr)) != NULL) hashAdd(ucscToRef, row[0], cloneString(row[1])); verbose(1, "Got %d items in %s.knownToRefSeq\n", ucscToRef->elCount, database); sqlFreeResult(&sr); /* Build up hash keyed by refSeq accessions with locus link values. */ struct hash *refToLl = hashNew(16); -sr = sqlGetResult(conn, NOSQLINJ "select mrnaAcc,locusLinkId from refLink"); +sqlSafef(query, sizeof query, "select mrnaAcc,locusLinkId from refLink"); +sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) hashAdd(refToLl, row[0], cloneString(row[1])); sqlFreeResult(&sr); verbose(1, "Got %d items in %s.refLink\n", refToLl->elCount, database); /* Stream through kgTxInfo table getting ones that are _primarily_ refSeq. */ -sr = sqlGetResult(conn, NOSQLINJ "select name from kgTxInfo where isRefSeq=1"); +sqlSafef(query, sizeof query, "select name from kgTxInfo where isRefSeq=1"); +sr = sqlGetResult(conn, query); FILE *f = mustOpen(knownToKegg, "w"); while ((row = sqlNextRow(sr)) != NULL) { char *ucsc = row[0]; char *refSeq = hashFindVal(ucscToRef, ucsc); if (refSeq) { char *ll = hashFindVal(refToLl, refSeq); if (ll) { char *kegg = hashFindVal(llToKegg, ll); if (kegg) fprintf(f, "%s\t%s\t%s\n", ucsc, ll, kegg); } }