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/autoSql/autoSql.c src/hg/autoSql/autoSql.c index 95cd659..b2e727e 100644 --- src/hg/autoSql/autoSql.c +++ src/hg/autoSql/autoSql.c @@ -999,41 +999,41 @@ { struct asColumn *col; for(col = colList; col != NULL; col = col->next) { if(col->obType == NULL) return FALSE; } return TRUE; } void dynamicSaveToDb(struct asObject *table, FILE *f, FILE *hFile) /* create C code that will save a table structure to the database */ { char *tableName = table->name; struct asColumn *col; -struct dyString *colInsert = newDyString(1024); /* code to associate columns with printf format characters the insert statement */ -struct dyString *stringDeclarations = newDyString(1024); /* code to declare necessary strings */ -struct dyString *stringFrees = newDyString(1024); /* code to free necessary strings */ -struct dyString *update = newDyString(1024); /* code to do the update statement itself */ -struct dyString *stringArrays = newDyString(1024); /* code to convert arrays to strings */ +struct dyString *colInsert = dyStringNew(1024); /* code to associate columns with printf format characters the insert statement */ +struct dyString *stringDeclarations = dyStringNew(1024); /* code to declare necessary strings */ +struct dyString *stringFrees = dyStringNew(1024); /* code to free necessary strings */ +struct dyString *update = dyStringNew(1024); /* code to do the update statement itself */ +struct dyString *stringArrays = dyStringNew(1024); /* code to convert arrays to strings */ boolean hasArray = FALSE; dynamicSaveToDbPrintPrototype(tableName, hFile, TRUE); fprintf(hFile, "\n"); dynamicSaveToDbPrintPrototype(tableName, f, FALSE); fprintf(f, "{\n"); -fprintf(f, "struct dyString *update = newDyString(updateSize);\n"); +fprintf(f, "struct dyString *update = dyStringNew(updateSize);\n"); dyStringPrintf(update, "sqlDyStringPrintf(update, \"insert into %%s values ( "); dyStringPrintf(stringDeclarations, "char "); for (col = table->columnList; col != NULL; col = col->next) { char *colName = col->name; char *outString = NULL; /* printf formater for column, i.e. %d for int, '%s' for string */ struct asTypeInfo *lt = col->lowType; enum asTypes type = lt->type; char colInsertBuff[256]; /* what variable name matches up with the printf format character in outString */ boolean colInsertFlag = TRUE; /* if column is not a native type insert NULL with no associated variable */ switch(type) { case t_char: outString = (col->fixedSize > 0) ? "'%s'" : "'%c'"; break; @@ -1106,31 +1106,31 @@ strcat(colInsertBuff, ", "); /* if we have a column to append do so */ if(colInsertFlag) dyStringPrintf(colInsert, "%s", colInsertBuff); } if(hasArray) { fprintf(f, "%s\n", stringDeclarations->string); } fprintf(f, "%s", stringArrays->string); fprintf(f, "%s", update->string); fprintf(f, ")\", \n\ttableName, "); fprintf(f, "%s);\n", colInsert->string); fprintf(f, "sqlUpdate(conn, update->string);\n"); -fprintf(f, "freeDyString(&update);\n"); +fprintf(f, "dyStringFree(&update);\n"); if(hasArray) { fprintf(f, "%s", stringFrees->string); } fprintf(f, "}\n\n"); dyStringFree(&colInsert); dyStringFree(&stringDeclarations); dyStringFree(&stringFrees); dyStringFree(&update); } boolean lastStringType(struct asColumn *colList) /* if there are any more string types returns TRUE else returns false */ { struct asColumn *col;