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/hgGene/dnaBindMotif.c src/hg/hgGene/dnaBindMotif.c index fb69c2c..467d8cd 100644 --- src/hg/hgGene/dnaBindMotif.c +++ src/hg/hgGene/dnaBindMotif.c @@ -1,102 +1,102 @@ /* This shows the section on the binding motif of a * transcription factor. */ /* 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 "hash.h" #include "linefile.h" #include "jksql.h" #include "dnaMotif.h" #include "dnaMotifSql.h" #include "hui.h" #include "portable.h" #include "hgGene.h" static char *orfToGene(struct sqlConnection *conn, char *orf) /* Return gene name of given orf, or NULL if it * doesn't exist. */ { char gene[256]; char query[256]; sqlSafef(query, sizeof(query), "select value from sgdToName where name = '%s'", orf); if (sqlQuickQuery(conn, query, gene, sizeof(gene)) == NULL) return NULL; return cloneString(gene); } static boolean transRegCodeMotifExists(struct section *section, struct sqlConnection *conn, char *geneId) /* Return TRUE if tables exists and have our gene. */ { char *gene; if (!sqlTableExists(conn, "transRegCodeMotif")) return FALSE; if (!sqlTableExists(conn, "sgdToName")) return FALSE; gene = orfToGene(conn, geneId); if (gene == NULL) return FALSE; return sqlRowExists(conn, "transRegCodeMotif", "name", gene); } struct dnaMotif *dnaMotifLoadNamed(struct sqlConnection *conn, char *table, char *name) /* Load motif of given name from table. Return NULL if no such * motif. */ { char where[256]; -sqlSafefFrag(where, sizeof(where), "name='%s'", name); +sqlSafef(where, sizeof(where), "name='%s'", name); return dnaMotifLoadWhere(conn, table, where); } struct dnaMotif *transRegMotif(struct sqlConnection *conn, char *geneId) /* Get motif for gene, NULL if none. */ { struct dnaMotif *motif = NULL; char *gene = orfToGene(conn, geneId); if (gene != NULL) motif = dnaMotifLoadNamed(conn, "transRegCodeMotif", gene); return motif; } static void transRegCodeMotifPrint(struct section *section, struct sqlConnection *conn, char *geneId) /* Print out transcription regulatory code info. */ { struct dnaMotif *motif = transRegMotif(conn, geneId); if (motif != NULL) { struct tempName pngTn; dnaMotifMakeProbabalistic(motif); makeTempName(&pngTn, "logo", ".png"); dnaMotifToLogoPng(motif, 47, 140, NULL, "../trash", pngTn.forCgi); hPrintf(" "); hPrintf("<IMG SRC=\"%s\" BORDER=1>", pngTn.forHtml); hPrintf("\n"); hPrintf("<PRE>"); dnaMotifPrintProb(motif, stdout); hPrintf("</PRE><BR>\n"); hPrintf("This data is from "); hPrintf("<A HREF=\"%s\" TARGET=_blank>", "http://www.nature.com/cgi-taf/DynaPage.taf?file=/nature/journal/v431/n7004/abs/nature02800_fs.html"); hPrintf("Transcriptional regulatory code of a eukaryotic genome</A> "); hPrintf("by Harbison, Gordon, et al."); } } struct section *transRegCodeMotifSection(struct sqlConnection *conn, struct hash *sectionRa) /* Create dnaBindMotif section. */ { struct section *section = sectionNew(sectionRa, "transRegCodeMotif"); if (section != NULL) { section->exists = transRegCodeMotifExists; section->print = transRegCodeMotifPrint; } return section; }