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/dnaGene/dnaGene.c src/hg/dnaGene/dnaGene.c index 5e0d991..ec7bf55 100644 --- src/hg/dnaGene/dnaGene.c +++ src/hg/dnaGene/dnaGene.c @@ -42,31 +42,31 @@ conn2 = hAllocConn(); result = FALSE; sqlSafef(query2, sizeof query2, "select gbAC from %s.locus2Acc0 where locusID=%s and seqType='m';", tempDbName, locusID); sr2 = sqlMustGetResult(conn2, query2); row2 = sqlNextRow(sr2); while (row2 != NULL) { gbAC = row2[0]; gbID = strdup(gbAC); chp = strstr(gbID, "."); if (chp != NULL) *chp = '\0'; - sqlSafefFrag(cond_str, sizeof cond_str, "name = '%s';", gbID); + sqlSafef(cond_str, sizeof cond_str, "name = '%s';", gbID); knownGeneID = sqlGetField(dbName, "knownGene", "name", cond_str); if (knownGeneID != NULL) { result=TRUE; break; } row2 = sqlNextRow(sr2); } hFreeConn(&conn); hFreeConn(&conn2); sqlFreeResult(&sr2); return(result); } @@ -134,79 +134,79 @@ giNCBI2 = row2[2]; revStatus = row2[3]; proteinAC2 = row2[4]; taxID2 = row2[5]; refSeq = strdup(refAC); chp = strstr(refAC, "."); if (chp != NULL) *chp = '\0'; proteinDisplayID = NULL; /* check if the locusID of this RefSeq points to a KG mRNA */ hasKGmRNA = checkMrna(locusID); /* check if this RefSeq has 'g' type sequence(s) referenced */ - sqlSafefFrag(cond_str, sizeof cond_str, "locusID=%s and seqType='g';", locusID); + sqlSafef(cond_str, sizeof cond_str, "locusID=%s and seqType='g';", locusID); gseq = sqlGetField(tempDbName, "locus2Acc0", "gbac", cond_str); /* process only 'g' type record which does not have corresponding KG entry */ if ((!hasKGmRNA) && (gseq != NULL)) { - sqlSafefFrag(cond_str, sizeof cond_str, "name='%s'", refAC); + sqlSafef(cond_str, sizeof cond_str, "name='%s'", refAC); hseq = sqlGetField(genomeReadOnly, "refGene", "name", cond_str); if (hseq != NULL) { - sqlSafefFrag(cond_str, sizeof cond_str, "refseq='%s';", refAC); + sqlSafef(cond_str, sizeof cond_str, "refseq='%s';", refAC); swissprot = sqlGetField(protDbName, "hugo", "swissprot", cond_str); if (swissprot != NULL) { if (strlen(swissprot) >0) { // HUGO has an entry with swissprot ID, get display ID - sqlSafefFrag(cond_str, sizeof cond_str, "accession='%s';", swissprot); + sqlSafef(cond_str, sizeof cond_str, "accession='%s';", swissprot); proteinDisplayID = sqlGetField(protDbName, "spXref2", "displayID", cond_str); if (proteinDisplayID == NULL) { fprintf(stderr, "%s: a HUGO.swissprot, ", swissprot); fprintf(stderr, "but not a SP Primary AC.\n"); fflush(stdout); } } else { //printf("HGNC has a non-NULL but empty swissprot field "); //printf("for %s\n", refAC);fflush(stdout); } } // not finding it in HUGO does not mean not a valid one for sure if (proteinDisplayID == NULL) { // get gbAC and check if spXref2 actually has it sqlSafef(query3, sizeof query3, "select gbAC from %s.locus2Acc0 where locusID=%s;", tempDbName, locusID); sr3 = sqlMustGetResult(conn3, query3); row3 = sqlNextRow(sr3); while (row3 != NULL) { gbAC = row3[0]; chp = strstr(gbAC, "."); if (chp != NULL) *chp = '\0'; - sqlSafefFrag(cond_str, sizeof cond_str, "extAC='%s'", gbAC); + sqlSafef(cond_str, sizeof cond_str, "extAC='%s'", gbAC); proteinDisplayID = sqlGetField(protDbName, "spXref2", "displayID", cond_str); if (proteinDisplayID == NULL) { //printf("%s %s is in refGene, but has no SWISS-PROT.\n", // locusID, refAC); //fflush(stdout); } else { //printf("%s %s got 2nd chance.\n", refAC, gbAC);fflush(stdout); break; } row3 = sqlNextRow(sr3); }