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/snp/details/dbSnp.c src/hg/snp/details/dbSnp.c index faa7142..36b9d73 100644 --- src/hg/snp/details/dbSnp.c +++ src/hg/snp/details/dbSnp.c @@ -76,35 +76,36 @@ smtNoMatch = 0, /* No match */ smtObserved = 1, /* Matches observed */ smtAlternate = 2, /* Matches alternate */ smtObservedRC = 3, /* Matches observed reverse complement */ smtAlternateRC = 4, /* Matches alternate reverse complement*/ }; struct slName *getChromList() /* Get list of all chromosomes. */ { struct sqlConnection *conn = hAllocConn(); struct sqlResult *sr; char **row = NULL; struct slName *list = NULL; struct slName *el = NULL; -char *queryString = NOSQLINJ "select chrom " - "from chromInfo " - "where chrom not like '%random' " - "order by size desc"; -sr = sqlGetResult(conn, queryString); +char query[1024]; +sqlSafef(query, sizeof query, + "select chrom from chromInfo " + "where chrom not like '%%random' " + "order by size desc"); +sr = sqlGetResult(conn, query); while ((row=sqlNextRow(sr))) { el = newSlName(row[0]); slAddHead(&list, el); } slReverse(&list); /* Restore order */ sqlFreeResult(&sr); hFreeConn(&conn); return list; } void convertToLowercase(char *ptr) /* convert and entire string to lowercase */ { for( ; *ptr !='\0'; ptr++) @@ -113,57 +114,57 @@ void convertToUppercase(char *ptr) /* convert and entire string to uppercase */ { for( ; *ptr !='\0'; ptr++) *ptr=toupper(*ptr); } long int addSnpsFromChrom(char *chromName, struct dnaSeq *seq, char *snpTable, struct hash *snpHash) /* Add all snps in table on chromosome to hash */ { struct sqlConnection *conn = hAllocConn(); struct sqlResult *sr; char **row = NULL; -struct dyString *query = newDyString(256); +struct dyString *query = dyStringNew(256); char rsId[20]; unsigned long int snpCount = 0; sqlDyStringPrintf(query, "select chromStart, " " name " "from %s " "where chrom = '%s' " " and source not like 'A%%' ", snpTable, chromName); sr = sqlGetResult(conn, query->string); while ((row=sqlNextRow(sr))) { struct dbInfo *db; strcpy(rsId, row[1]); AllocVar(db); hashAddSaveName(snpHash, rsId+2, db, &db->name+2); db->chromPos = atoi(row[0]); strncpy(db->baseInAsm, seq->dna+(db->chromPos),1); strncpy(db->region,seq->dna+(db->chromPos)-FLANK,REGION); convertToLowercase(db->region); snpCount++; } sqlFreeResult(&sr); hFreeConn(&conn); -freeDyString(&query); +dyStringFree(&query); return snpCount; } struct hash *makeSnpInfoHash(char *database) /* Make hash of info for all SNPs */ { struct slName *chromList = getChromList(); struct slName *chrom; struct hash *snpHash = newHash(24); /* Make hash 2^24 (16M) big */ unsigned long int snpMapCount = 0; unsigned long int snpMapCumul = 0; struct dnaSeq *seq = NULL; /* walk through list of chromosomes, get seq, get snps, save in hash */ for (chrom = chromList; chrom; chrom = chrom->next) {