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/hubApi/hubApi.c src/hg/hubApi/hubApi.c index fa615d5..9388bcc 100644 --- src/hg/hubApi/hubApi.c +++ src/hg/hubApi/hubApi.c @@ -237,31 +237,31 @@ /* print out a sample getData URL */ { char errorPrint[2048]; errorPrint[0] = 0; if (isNotEmpty(errorString)) { safef(errorPrint, sizeof(errorPrint), " <font color='red'>ERROR: %s</font>", errorString); } boolean superChild = tdbIsSuperTrackChild(tdb); char *genome = NULL; if (hub) genome = hub->genomeList->name; -struct dyString *extraDyFlags = newDyString(128); +struct dyString *extraDyFlags = dyStringNew(128); if (debug) dyStringAppend(extraDyFlags, ";debug=1"); if (jsonOutputArrays) dyStringAppend(extraDyFlags, ";jsonOutputArrays=1"); char *extraFlags = dyStringCannibalize(&extraDyFlags); if (protectedTrack(tdb, tdb->track)) hPrintf("<li>%s : %s <protected data></li>\n", tdb->track, tdb->type); else if (db) { if (hub) { char urlReference[2048]; safef(urlReference, sizeof(urlReference), " <a href='%s/getData/track?hubUrl=%s;genome=%s;track=%s;maxItemsOutput=5%s' target=_blank>(sample data)%s</a>\n", urlPrefix, hub->url, genome, tdb->track, extraFlags, errorPrint); @@ -288,31 +288,31 @@ char urlReference[2048]; safef(urlReference, sizeof(urlReference), " <a href='%s/getData/track?hubUrl=%s;genome=%s;track=%s;maxItemsOutput=5%s' target=_blank>(sample data)%s</a>\n", urlPrefix, hub->url, genome, tdb->track, extraFlags, errorPrint); if (tdb->parent) hPrintf("<li><b>%s</b>: %s subtrack of parent: %s%s</li>\n", tdb->track, tdb->type, tdb->parent->track, urlReference); else hPrintf("<li><b>%s</b>: %s%s</li>\n", tdb->track, tdb->type, urlReference); } else hPrintf("<li>%s : %s not db hub track ?</li>\n", tdb->track, tdb->type); } static void hubSampleUrl(struct trackHub *hub, struct trackDb *tdb, long chromCount, long itemCount, char *genome, char *errorString) { -struct dyString *extraDyFlags = newDyString(128); +struct dyString *extraDyFlags = dyStringNew(128); if (debug) dyStringAppend(extraDyFlags, ";debug=1"); if (jsonOutputArrays) dyStringAppend(extraDyFlags, ";jsonOutputArrays=1"); char *extraFlags = dyStringCannibalize(&extraDyFlags); char errorPrint[2048]; errorPrint[0] = 0; if (isNotEmpty(errorString)) { safef(errorPrint, sizeof(errorPrint), " : <font color='red'>ERROR: %s</font>", errorString); } char countsMessage[512]; @@ -492,31 +492,31 @@ { struct trackDb *tdbEl = NULL; for (tdbEl = tdb->subtracks; tdbEl; tdbEl = tdbEl->next) { boolean compositeContainer = tdbIsComposite(tdbEl); boolean compositeView = tdbIsCompositeView(tdbEl); if (! (compositeContainer || compositeView) ) { char *bigDataIndex = NULL; char *relIdxUrl = trackDbSetting(tdbEl, "bigDataIndex"); if (relIdxUrl != NULL) bigDataIndex = trackHubRelativeUrl(hub->genomeList->trackDbFile, relIdxUrl); char *bigDataUrl = trackDbSetting(tdbEl, "bigDataUrl"); char *longName = NULL; unsigned longSize = 0; - struct dyString *errors = newDyString(1024); + struct dyString *errors = dyStringNew(1024); (void) bbiBriefMeasure(tdbEl->type, bigDataUrl, bigDataIndex, &chromCount, &itemCount, errors, &longName, &longSize); chromSize = longSize; chromName = longName; } if (tdbIsCompositeView(tdbEl)) hPrintf("<li><b>%s</b>: %s : composite view of parent: %s</li>\n", tdbEl->track, tdbEl->type, tdbEl->parent->track); else { if (isSupportedType(tdbEl->type)) hubSampleUrl(hub, tdbEl, chromCount, itemCount, genome, errorString); else hPrintf("<li><b>%s</b>: %s : subtrack of parent: %s</li>\n", tdbEl->track, tdbEl->type, tdbEl->parent->track); } hashCountTrack(tdbEl, countTracks); if (tdbEl->subtracks) @@ -593,31 +593,31 @@ static void hubCountOneTdb(struct trackHub *hub, char *db, struct trackDb *tdb, char *bigDataIndex, struct hash *countTracks, char *chromName, unsigned chromSize, char *genome) { char *bigDataUrl = trackDbSetting(tdb, "bigDataUrl"); boolean compositeContainer = tdbIsComposite(tdb); boolean compositeView = tdbIsCompositeView(tdb); boolean superChild = tdbIsSuperTrackChild(tdb); boolean depthSearch = cartUsualBoolean(cart, "depthSearch", FALSE); hashCountTrack(tdb, countTracks); long chromCount = 0; long itemCount = 0; -struct dyString *errors = newDyString(1024); +struct dyString *errors = dyStringNew(1024); /* if given a chromSize, it belongs to a UCSC db and this is *not* an * assembly hub, otherwise, look up a chrom and size in the bbi file */ if (! (compositeContainer || compositeView) ) { if (chromSize < 1 || depthSearch) { char *longName = NULL; unsigned longSize = 0; (void) bbiBriefMeasure(tdb->type, bigDataUrl, bigDataIndex, &chromCount, &itemCount, errors, &longName, &longSize); chromSize = longSize; chromName = longName; } } @@ -1232,31 +1232,31 @@ /* how does debug carry forward ? */ // if (debug) // cgiMakeHiddenVar("debug", "1"); } static void apiRequest(char *pathInfo) { hPrintDisable(); /*expect no more than MAX_PATH_INFO number of words*/ char *words[MAX_PATH_INFO]; /* can immediately verify valid parameters right here right now */ char *start = cgiOptionalString("start"); char *end = cgiOptionalString("end"); char *db = cgiOptionalString("genome"); char *hubUrl = cgiOptionalString("hubUrl"); -struct dyString *errorMsg = newDyString(128); +struct dyString *errorMsg = dyStringNew(128); if (isEmpty(hubUrl) && isNotEmpty(db)) { struct sqlConnection *conn = hAllocConnMaybe(db); if (NULL == conn) dyStringPrintf(errorMsg, "can not find genome genome='%s' for endpoint '%s'", db, pathInfo); else hFreeConn(&conn); } if (isNotEmpty(start) || isNotEmpty(end)) { long long llStart = -1; long long llEnd = -1; struct errCatch *errCatch = errCatchNew(); if (errCatchStart(errCatch))