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/protein/lib/doTracks.c src/hg/protein/lib/doTracks.c index 9a02242..7abd3b1 100644 --- src/hg/protein/lib/doTracks.c +++ src/hg/protein/lib/doTracks.c @@ -95,31 +95,31 @@ char *chp; int aaResCnt[20]; double aaResFreqDouble[20]; int abnormal; int ia = -1; double pctLow[20], pctHi[20]; /* count frequency for each residue for current protein */ chp = aa; for (j=0; j<20; j++) { aaResCnt[j] = 0; /* get cutoff threshold value pairs */ - sqlSafefFrag(cond_str, sizeof(cond_str), "AA='%c'", aaAlphabet[j]); + sqlSafef(cond_str, sizeof(cond_str), "AA='%c'", aaAlphabet[j]); answer = sqlGetField(database, "pbAnomLimit", "pctLow", cond_str); pctLow[j] = (double)(atof(answer)); answer = sqlGetField(database, "pbAnomLimit", "pctHi", cond_str); pctHi[j] = (double)(atof(answer)); } for (i=0; i<len; i++) { for (j=0; j<20; j++) { if (*chp == aaChar[j]) { aaResCnt[j] ++; break; } @@ -909,31 +909,31 @@ sqlSafef(query, sizeof(query), "select region from %s.sfAssign where sfID='%s' and seqID='%s' and evalue <=0.02", protDbName, sfID, seqID); sr = sqlMustGetResult(conn, query); row = sqlNextRow(sr); while (row != NULL) { region = row[0]; for (j=0; j<sfCnt; j++) { if (sfId[j] == atoi(sfID)) goto skip; } - sqlSafefFrag(cond_str, sizeof(cond_str), "id=%s;", sfID); + sqlSafef(cond_str, sizeof(cond_str), "id=%s;", sfID); sfDesc = sqlGetField(protDbName, "sfDes", "description", cond_str); /* !!! refine logic here later to be defensive against illegal syntax */ chp = region; done = 0; while (!done) { chp2 = strstr(chp, "-"); *chp2 = '\0'; chp2++; sscanf(chp, "%d", &int_start); chp = chp2; @@ -990,82 +990,82 @@ char *transcriptName; char *chp, *chp2; int ii = 0; int int_start, int_end; if (!hTableExists(database, "sfAssign")) return(0); conn = hAllocConn(database); conn2 = hAllocConn(database); if (hTableExists(database, "ensemblXref3")) { /* use ensemblXref3 for Ensembl data release after ensembl34d */ - sqlSafefFrag(cond_str, sizeof(cond_str), "tremblAcc='%s'", proteinID); + sqlSafef(cond_str, sizeof(cond_str), "tremblAcc='%s'", proteinID); ensPep = sqlGetField(database, "ensemblXref3", "protein", cond_str); if (ensPep == NULL) { - sqlSafefFrag(cond_str, sizeof(cond_str), "swissAcc='%s'", proteinID); + sqlSafef(cond_str, sizeof(cond_str), "swissAcc='%s'", proteinID); ensPep = sqlGetField(database, "ensemblXref3", "protein", cond_str); if (ensPep == NULL) return(0); } } else { if (! (hTableExists(database, "ensemblXref") || hTableExists(database, "ensTranscript") ) ) return(0); /* two steps query needed because the recent Ensembl gene_xref 11/2003 table does not have valid translation_name */ - sqlSafefFrag(cond_str, sizeof(cond_str), "external_name='%s'", protDisplayID); + sqlSafef(cond_str, sizeof(cond_str), "external_name='%s'", protDisplayID); transcriptName = sqlGetField(database, "ensGeneXref", "transcript_name", cond_str); if (transcriptName == NULL) { return(0); } else { - sqlSafefFrag(cond_str, sizeof(cond_str), "transcript_name='%s';", transcriptName); + sqlSafef(cond_str, sizeof(cond_str), "transcript_name='%s';", transcriptName); ensPep = sqlGetField(database, "ensTranscript", "translation_name", cond_str); if (ensPep == NULL) { hFreeConn(&conn); return(0); } } } ensPepName = ensPep; sqlSafef(query, sizeof(query), "select * from %s.sfAssign where seqID='%s' and evalue <= 0.02;", database, ensPep); sr = sqlMustGetResult(conn, query); row = sqlNextRow(sr); if (row == NULL) return(0); while (row != NULL) { genomeID = row[0]; seqID = row[1]; modelID = row[2]; region = row[3]; eValue = row[4]; sfID = row[5]; /* sfDesc = row[6]; */ /* !!! the recent Suprefamily sfAssign table does not have valid sf description */ - sqlSafefFrag(cond_str, sizeof(cond_str), "id=%s;", sfID); + sqlSafef(cond_str, sizeof(cond_str), "id=%s;", sfID); sfDesc = sqlGetField(database, "sfDes", "description", cond_str); /* !!! refine logic here later to be defensive against illegal syntax */ chp = region; done = 0; while (!done) { chp2 = strstr(chp, "-"); *chp2 = '\0'; chp2++; sscanf(chp, "%d", &int_start); chp = chp2; chp2 = strstr(chp, ","); @@ -1438,53 +1438,53 @@ l=strlen(aa); /* initialize AA properties */ aaPropertyInit(&hasResFreq); sfCount = getSuperfamilies2(proteinID); if (sfCount == 0) { sfCount = getSuperfamilies(proteinID); } if (mrnaID != NULL) { if (kgVersion == KG_III) { doExonTrack = FALSE; - sqlSafefFrag(cond_str, sizeof(cond_str), "spId='%s'", proteinID); + sqlSafef(cond_str, sizeof(cond_str), "spId='%s'", proteinID); kgId = sqlGetField(database, "kgXref", "kgId", cond_str); if (kgId != NULL) { - sqlSafefFrag(cond_str, sizeof(cond_str), "name='%s'", kgId); + sqlSafef(cond_str, sizeof(cond_str), "name='%s'", kgId); kgPep = sqlGetField(database, "knownGenePep", "seq", cond_str); //printf("<pre><br>%s", kgPep);fflush(stdout); if (kgPep != NULL) { if (strstr(protDbName, "proteins") != NULL) { protDbDate = strstr(protDbName, "proteins") + strlen("proteins"); safef(uniProtDbName, sizeof(uniProtDbName),"sp%s", protDbDate); - sqlSafefFrag(cond_str, sizeof(cond_str), "acc='%s'", proteinID); + sqlSafef(cond_str, sizeof(cond_str), "acc='%s'", proteinID); protPep = sqlGetField(uniProtDbName, "protein", "val", cond_str); //printf("<br>%s\n", protPep);fflush(stdout); if (protPep != NULL) { if (sameWord(kgPep, protPep)) { //printf("<br>MATCH!\n");fflush(stdout); - sqlSafefFrag(cond_str, sizeof(cond_str), "qName='%s'", kgId); + sqlSafef(cond_str, sizeof(cond_str), "qName='%s'", kgId); answer = sqlGetField(database, kgProtMapTableName, "qName", cond_str); if (answer != NULL) { /* NOTE: passing in kgId instead of proteinID because kgProtMap2's qName uses kgId instead of protein display ID */ getExonInfo(kgId, &exCount, &chrom, &strand); assert(exCount > 0); doExonTrack = TRUE; } } /* else {