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/ratStuff/mafSplitPos/mafSplitPos.c src/hg/ratStuff/mafSplitPos/mafSplitPos.c index 07c579c..f120b35 100644 --- src/hg/ratStuff/mafSplitPos/mafSplitPos.c +++ src/hg/ratStuff/mafSplitPos/mafSplitPos.c @@ -38,81 +38,81 @@ }; static char *chrom = NULL; static int minGap = 100; static int minRepeat = 100; static char *db = NULL; int nextGapPos(char *chrom, int desiredPos, struct sqlConnection *conn) { /* Find next gap on the chrom and return midpoint */ struct sqlResult *sr; char **row; int pos = -1; int start, end; struct hTableInfo *hti = hFindTableInfo(db, chrom, "gap"); -struct dyString *query = newDyString(1024); +struct dyString *query = dyStringNew(1024); if (hti == NULL) errAbort("table %s.gap doesn't exist", db); sqlDyStringPrintf(query, "select chromStart,chromEnd from "); if (hti->isSplit) - dyStringPrintf(query, "%s_gap where ", chrom); + sqlDyStringPrintf(query, "%s_gap where ", chrom); else - dyStringPrintf(query, "gap where %s='%s' AND ", hti->chromField, chrom); + sqlDyStringPrintf(query, "gap where %s='%s' AND ", hti->chromField, chrom); -dyStringPrintf(query, "(chromStart >= %d and chromEnd-chromStart > %d)\ +sqlDyStringPrintf(query, "(chromStart >= %d and chromEnd-chromStart > %d)\ order by chromStart limit 1", desiredPos, minGap); sr = sqlGetResult(conn, query->string); -freeDyString(&query); +dyStringFree(&query); if ((row = sqlNextRow(sr)) != NULL) { start = sqlSigned(row[0]); end = sqlSigned(row[1]); pos = start + (end - start)/2; } sqlFreeResult(&sr); return pos; } int nextRepeatPos(char *chrom, int desiredPos, struct sqlConnection *conn) /* Find next 0% diverged repeat on the chrom and return midpoint */ { struct sqlResult *sr; char **row; int pos = -1; int start, end; struct hTableInfo *hti = hFindTableInfo(db, chrom, "rmsk"); -struct dyString *query = newDyString(1024); +struct dyString *query = dyStringNew(1024); if (hti == NULL) errAbort("table %s.rmsk doesn't exist", db); sqlDyStringPrintf(query, "select genoStart,genoEnd from "); if (hti->isSplit) - dyStringPrintf(query, "%s_rmsk where ", chrom); + sqlDyStringPrintf(query, "%s_rmsk where ", chrom); else - dyStringPrintf(query, "rmsk where %s='%s' AND ", hti->chromField, chrom); -dyStringPrintf(query, + sqlDyStringPrintf(query, "rmsk where %s='%s' AND ", hti->chromField, chrom); +sqlDyStringPrintf(query, "(genoStart >= %d AND \ milliDiv=0 AND \ repClass<>'Simple_repeat' AND repClass<>'Low_complexity' AND \ genoEnd-genoStart>%d) order by genoStart limit 1", desiredPos, minRepeat); sr = sqlGetResult(conn, query->string); -freeDyString(&query); +dyStringFree(&query); if ((row = sqlNextRow(sr)) != NULL) { start = sqlSigned(row[0]); end = sqlSigned(row[1]); pos = start + (end - start)/2; } sqlFreeResult(&sr); return pos; } void chromSplits(char *chrom, int chromSize, int splitSize, struct sqlConnection *conn, FILE *f) /* determine positions on a single chromosome */ {