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/hgTracks/wigMafTrack.c src/hg/hgTracks/wigMafTrack.c index f426b04..23eb470 100644 --- src/hg/hgTracks/wigMafTrack.c +++ src/hg/hgTracks/wigMafTrack.c @@ -1240,41 +1240,41 @@ { bigBedIntervalToRow(bb, chromName, startBuf, endBuf, bedRow, ArraySize(bedRow)); ms = mafSummaryLoad(bedRow); if ((hel = hashLookup(componentHash, ms->src)) == NULL) hashAdd(componentHash, ms->src, ms); else slAddHead(&(hel->val), ms); } bbiFileClose(&bbi); } else { /* Create SQL where clause that will load up just the * summaries for the species that we are including. */ conn = hAllocConn(database); - dyStringAppend(where, "src in ("); + sqlDyStringPrintf(where, "src in ("); for (mi = miList; mi != NULL; mi = mi->next) { if (!isPairwiseItem(mi)) /* exclude non-species items (e.g. conservation wiggle */ continue; - dyStringPrintf(where, "'%s'", mi->db); + sqlDyStringPrintf(where, "'%s'", mi->db); if (mi->next != NULL) - dyStringAppend(where, ","); + sqlDyStringPrintf(where, ","); } - dyStringAppend(where, ")"); + sqlDyStringPrintf(where, ")"); /* check for empty where clause */ if (!sameString(where->string,"src in ()")) whereClause = where->string; sr = hOrderedRangeQuery(conn, summary, chromName, seqStart, seqEnd, whereClause, &rowOffset); boolean hasFieldLeftStatus = hHasField(database, summary, "leftStatus"); /* Loop through result creating a hash of lists of maf summary blocks. * The hash is keyed by species. */ while ((row = sqlNextRow(sr)) != NULL) { if (hasFieldLeftStatus) ms = mafSummaryLoad(row + rowOffset); else @@ -2353,31 +2353,31 @@ } } mafAliFree(&sub); } /* draw inserts line */ mi = miList; for(offset=startSub2*2; (offset < alignLineLength) && (offset < winBaseCount + startSub2 * 2); offset++) { int x1, x2; x1 = (offset - startSub2 * 2) * width/winBaseCount; x2 = ((offset - startSub2 * 2)+1) * width/winBaseCount - 1; if (insertCounts[offset] != 0) { - struct dyString *label = newDyString(20); + struct dyString *label = dyStringNew(20); int haveRoomFor = (width/winBaseCount)/tl.mWidth; /* calculate number of AAs instead of bases if it is wigMafProt */ if (strstr(track->tdb->type, "wigMafProt")) { dyStringPrintf(label, "%d",insertCounts[offset]/3); } else { dyStringPrintf(label, "%d",insertCounts[offset]); } if (label->stringSize > haveRoomFor) { dyStringClear(label); @@ -2433,43 +2433,43 @@ * for the same answer over and over. For other codon modes, it * will still need to query the Frames table repeatedly. * This was found to speed up dramatically by adding an index to * the table: * hgsql staAur2 -e 'CREATE INDEX src on multiz369wayFrames (src, bin);' */ static char * extraPrevious = NULL; static struct mafFrames *mfList = NULL, *mf; static boolean found = FALSE; if (framesTable != NULL) { char extra[512]; if (sameString("codonDefault", codonTransMode)) { - safef(extra, sizeof(extra), "src='%s'",defaultCodonSpecies); + sqlSafef(extra, sizeof(extra), "src='%s'",defaultCodonSpecies); found = TRUE; } else if (sameString("codonFrameDef", codonTransMode)) { - safef(extra, sizeof(extra), "src='%s'",mi->db); + sqlSafef(extra, sizeof(extra), "src='%s'",mi->db); found = FALSE; } else if (sameString("codonFrameNone", codonTransMode)) { - safef(extra, sizeof(extra), "src='%s'",mi->db); + sqlSafef(extra, sizeof(extra), "src='%s'",mi->db); found = TRUE; } else errAbort("unknown codon translation mode %s",codonTransMode); tryagain: if (differentStringNullOk(extraPrevious, extra)) { if (track->isBigBed) { char *species = mi->db; if (sameString("codonDefault", codonTransMode)) species = defaultCodonSpecies; mfList = getFramesFromBb( framesTable, chromName, seqStart, seqEnd, species); } @@ -2520,31 +2520,31 @@ func = mafLoadFromSql; closure = &sqlClosure; sqlClosure.conn2 = conn2; sqlClosure.conn3 = conn3; sqlClosure.mafFile = mafFile; sqlClosure.tableName = tableName; } translateCodons(func, closure, mi->db, line, start, w, frame, mf->strand[0],mf->prevFramePos,mf->nextFramePos, complementBases, x, y, width, mi->height, hvg); } if (!found) { /* try the default species */ - safef(extra, sizeof(extra), "src='%s'",defaultCodonSpecies); + sqlSafef(extra, sizeof(extra), "src='%s'",defaultCodonSpecies); found = TRUE; /* don't try again */ goto tryagain; } } if (startSub2) { if (strstr(track->tdb->type, "wigMafProt")) { spreadAlignStringProt(hvg, x, y, width, mi->height-1, color, font, &line[2], &selfLine[2], winBaseCount, dots, FALSE, seqStart, mafOrigOffset); } else { @@ -2743,31 +2743,31 @@ or if none there, use first entry in trackDb setting */ boolean first = TRUE; for (consWig = consWigList; consWig != NULL; consWig = consWig->next) { if (differentString(consWig->leftLabel, DEFAULT_CONS_LABEL)) { char *wigVarSuffix = NULL; (void)wigMafWiggleVar(tdb->track, consWig,&wigVarSuffix); if (!cartUsualBooleanClosestToHome(cart, tdb, FALSE, wigVarSuffix, first)) continue; } first = FALSE; // Manufacture and initialize wiggle subtrack, both tdb and track struct trackDb *wigTdb = CloneVar(tdb); - wigType = newDyString(64); + wigType = dyStringNew(64); dyStringPrintf(wigType, "type wig "); for (i = 1; i < wordCount; i++) dyStringPrintf(wigType, "%s ", words[i]); wigTdb->type = cloneString(wigType->string); wigTdb->track = consWig->table; wigTdb->table= consWig->table; /* Tweak wiggle left labels: replace underscore with space and * append 'Cons' */ struct dyString *ds = dyStringNew(0); dyStringAppend(ds, consWig->leftLabel); if (differentString(consWig->leftLabel, DEFAULT_CONS_LABEL)) dyStringAppend(ds, " Cons"); wigTdb->shortLabel = dyStringCannibalize(&ds); subChar(wigTdb->shortLabel, '_', ' ');