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/hgLinkIn/handlerList.c src/hg/hgLinkIn/handlerList.c index 3e6dcec..2e5901a 100644 --- src/hg/hgLinkIn/handlerList.c +++ src/hg/hgLinkIn/handlerList.c @@ -72,38 +72,38 @@ * a list of identifiers, search for any EMBL mRNA names that we have positions for. * Return a list of matching positions. */ { struct linkInResult *results = NULL; bool hasMRna = sqlTableExists(conn, "all_mrna"); if (!hasMRna) return NULL; struct dyString *query = dyStringNew(0); sqlDyStringPrintf(query, "select tName, tStart, tEnd from all_mrna where qName in ("); struct slName *thisName = emblNames; bool firstName = TRUE; while (thisName != NULL) { if (firstName) { - sqlDyStringPrintfFrag(query, "'%s'", thisName->name); + sqlDyStringPrintf(query, "'%s'", thisName->name); firstName = FALSE; } else - sqlDyStringPrintfFrag(query, ",'%s'", thisName->name); + sqlDyStringPrintf(query, ",'%s'", thisName->name); thisName = thisName->next; } -sqlDyStringPrintfFrag(query, ")"); +sqlDyStringPrintf(query, ")"); struct sqlResult *sr = sqlGetResult(conn, dyStringContents(query)); char **row = NULL; while ((row = sqlNextRow(sr)) != NULL) { struct linkInResult *newResult = NULL; AllocVar(newResult); newResult->db = cloneString(db); char position[4096]; safef(position, sizeof(position), "%s:%s-%s", row[0], row[1], row[2]); newResult->position = cloneString(position); newResult->trackToView = cloneString("all_mrna"); slAddHead(&results, newResult); } slReverse(&results); sqlFreeResult(&sr); @@ -118,38 +118,38 @@ { struct linkInResult *results = NULL; bool hasNcbiRefGene = sqlTableExists(conn, "ncbiRefGene"); bool hasRefGene = sqlTableExists(conn, "refGene"); if (!hasNcbiRefGene && !hasRefGene) return NULL; struct dyString *query = dyStringNew(0); sqlDyStringPrintf(query, "select chrom, txStart, txEnd from %s where name in (", hasNcbiRefGene?"ncbiRefGene":"refGene"); struct slName *thisName = refNames; bool firstName = TRUE; while (thisName != NULL) { if (firstName) { - sqlDyStringPrintfFrag(query, "'%s'", thisName->name); + sqlDyStringPrintf(query, "'%s'", thisName->name); firstName = FALSE; } else - sqlDyStringPrintfFrag(query, ",'%s'", thisName->name); + sqlDyStringPrintf(query, ",'%s'", thisName->name); thisName = thisName->next; } -sqlDyStringPrintfFrag(query, ")"); +sqlDyStringPrintf(query, ")"); struct sqlResult *sr = sqlGetResult(conn, dyStringContents(query)); char **row = NULL; while ((row = sqlNextRow(sr)) != NULL) { struct linkInResult *newResult = NULL; AllocVar(newResult); newResult->db = cloneString(db); char position[4096]; safef(position, sizeof(position), "%s:%s-%s", row[0], row[1], row[2]); newResult->position = cloneString(position); newResult->trackToView = cloneString(hasNcbiRefGene?"ncbiRefGene":"refGene"); slAddHead(&results, newResult); } slReverse(&results); sqlFreeResult(&sr);