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/lib/wigDataStream.c src/hg/lib/wigDataStream.c index ee7a664..66f45b8 100644 --- src/hg/lib/wigDataStream.c +++ src/hg/lib/wigDataStream.c @@ -154,39 +154,38 @@ { if (wds->db) fprintf(fh, "#\t Database: %s, Table: %s\n", wds->db, wds->tblName); if (wds->isFile) fprintf(fh, "#\t from file, Table: %s\n", wds->tblName); wigStatsTableHeading(fh, htmlOut); } } /* void wigStatsHeader() */ /* strictly object methods following ************************/ /* PRIVATE METHODS ************************************************/ static void addConstraint(struct wiggleDataStream *wds, char *left, char *op, char *right) { -struct dyString *constrain = dyStringNew(256); +struct dyString *constraint = dyStringNew(256); if (wds->sqlConstraint) - dyStringPrintf(constrain, "%s AND ", wds->sqlConstraint); + sqlDyStringPrintf(constraint, "%-s AND ", wds->sqlConstraint); -sqlDyStringPrintfFrag(constrain, "%s %-s \"%s\"", left, op, right); +sqlDyStringPrintf(constraint, "%s %-s '%s'", left, op, right); freeMem(wds->sqlConstraint); /* potentially previously existing */ -wds->sqlConstraint = cloneString(constrain->string); -dyStringFree(&constrain); +wds->sqlConstraint = dyStringCannibalize(&constraint); } /* *row[] is artifically one too big to allow for a potential bin * column when reading files that may have it. */ static boolean nextRow(struct wiggleDataStream *wds, char *row[], int maxRow) /* read next wig row from sql query or lineFile * FALSE return on no more data */ { int numCols; if (wds->isFile) { /* the file may have a bin column, detect automatically and eliminate */ numCols = lineFileChopNextTab(wds->lf, row, maxRow+1); @@ -365,63 +364,70 @@ struct dyString *fileName = dyStringNew(256); lineFileClose(&wds->lf); /* possibly a previous file */ /* don't add .wig if it is already there, or use whatever filename * was given */ if (fileExists(wds->tblName)) dyStringPrintf(fileName, "%s", wds->tblName); else dyStringPrintf(fileName, "%s.wig", wds->tblName); wds->lf = lineFileOpen(fileName->string, TRUE); dyStringFree(&fileName); } else { struct dyString *query = dyStringNew(256); sqlDyStringPrintf(query, "select * from %s", wds->tblName); + char op[1+NOSQLINJ_SIZE+1]; if (wds->chrName) - addConstraint(wds, "chrom", "=", wds->chrName); + { + sqlSafef(op, sizeof op, "="); + addConstraint(wds, "chrom", op, wds->chrName); + } if (wds->winEnd) { char limits[256]; safef(limits, ArraySize(limits), "%d", wds->winEnd ); - addConstraint(wds, "chromStart", "<", limits); + sqlSafef(op, sizeof op, "<"); + addConstraint(wds, "chromStart", op, limits); safef(limits, ArraySize(limits), "%d", wds->winStart ); - addConstraint(wds, "chromEnd", ">", limits); + sqlSafef(op, sizeof op, ">"); + addConstraint(wds, "chromEnd", op, limits); } if (wds->spanLimit) { struct dyString *dyTmp = dyStringNew(256); dyStringPrintf(dyTmp, "%u", wds->spanLimit); - addConstraint(wds, "span", "=", dyTmp->string); + sqlSafef(op, sizeof op, "="); + addConstraint(wds, "span", op, dyTmp->string); dyStringFree(&dyTmp); } if (wds->sqlConstraint) { - dyStringPrintf(query, " where "); + sqlDyStringPrintf(query, " where "); if (wds->winEnd) { hAddBinToQuery(wds->winStart, wds->winEnd, query); } - dyStringPrintf(query, " (%s)", + sqlDyStringPrintf(query, " (%-s)", wds->sqlConstraint); } - dyStringPrintf(query, " order by "); + sqlDyStringPrintf(query, " order by "); if (!wds->chrName) - dyStringPrintf(query, " chrom ASC,"); + sqlDyStringPrintf(query, " chrom ASC,"); if (!wds->spanLimit) - dyStringPrintf(query, " span ASC,"); - dyStringPrintf(query, " chromStart ASC"); + sqlDyStringPrintf(query, " span ASC,"); + sqlDyStringPrintf(query, " chromStart ASC"); verbose(VERBOSE_SQL_ROW_LEVEL, "#\t%s\n", query->string); if (!wds->conn) wds->conn = hAllocConn(wds->db); wds->sr = sqlGetResult(wds->conn,query->string); } } static void setDbTable(struct wiggleDataStream *wds, char * db, char *table) /* sets the DB and table name, determines if table is a file or not */ { struct dyString *fileName = dyStringNew(256); if (!table)