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/cirm/cdw/lib/cdwStep.c src/hg/cirm/cdw/lib/cdwStep.c index afb8b22..30d831f 100644 --- src/hg/cirm/cdw/lib/cdwStep.c +++ src/hg/cirm/cdw/lib/cdwStep.c @@ -1,640 +1,640 @@ /* cdwStep.c was originally generated by the autoSql program, which also * generated cdwStep.h and cdwStep.sql. This module links the database and * the RAM representation of objects. */ #include "common.h" #include "linefile.h" #include "dystring.h" #include "jksql.h" #include "cdwStep.h" char *cdwStepDefCommaSepFieldNames = "id,name,description"; void cdwStepDefStaticLoad(char **row, struct cdwStepDef *ret) /* Load a row from cdwStepDef table into ret. The contents of ret will * be replaced at the next call to this function. */ { ret->id = sqlUnsigned(row[0]); ret->name = row[1]; ret->description = row[2]; } struct cdwStepDef *cdwStepDefLoadByQuery(struct sqlConnection *conn, char *query) /* Load all cdwStepDef from table that satisfy the query given. * Where query is of the form 'select * from example where something=something' * or 'select example.* from example, anotherTable where example.something = * anotherTable.something'. * Dispose of this with cdwStepDefFreeList(). */ { struct cdwStepDef *list = NULL, *el; struct sqlResult *sr; char **row; sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) { el = cdwStepDefLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void cdwStepDefSaveToDb(struct sqlConnection *conn, struct cdwStepDef *el, char *tableName, int updateSize) /* Save cdwStepDef as a row to the table specified by tableName. * As blob fields may be arbitrary size updateSize specifies the approx size * of a string that would contain the entire query. Arrays of native types are * converted to comma separated strings and loaded as such, User defined types are * inserted as NULL. This function automatically escapes quoted strings for mysql. */ { -struct dyString *update = newDyString(updateSize); +struct dyString *update = dyStringNew(updateSize); sqlDyStringPrintf(update, "insert into %s values ( %u,'%s','%s')", tableName, el->id, el->name, el->description); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct cdwStepDef *cdwStepDefLoad(char **row) /* Load a cdwStepDef from row fetched with select * from cdwStepDef * from database. Dispose of this with cdwStepDefFree(). */ { struct cdwStepDef *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->name = cloneString(row[1]); ret->description = cloneString(row[2]); return ret; } struct cdwStepDef *cdwStepDefLoadAll(char *fileName) /* Load all cdwStepDef from a whitespace-separated file. * Dispose of this with cdwStepDefFreeList(). */ { struct cdwStepDef *list = NULL, *el; struct lineFile *lf = lineFileOpen(fileName, TRUE); char *row[3]; while (lineFileRow(lf, row)) { el = cdwStepDefLoad(row); slAddHead(&list, el); } lineFileClose(&lf); slReverse(&list); return list; } struct cdwStepDef *cdwStepDefLoadAllByChar(char *fileName, char chopper) /* Load all cdwStepDef from a chopper separated file. * Dispose of this with cdwStepDefFreeList(). */ { struct cdwStepDef *list = NULL, *el; struct lineFile *lf = lineFileOpen(fileName, TRUE); char *row[3]; while (lineFileNextCharRow(lf, chopper, row, ArraySize(row))) { el = cdwStepDefLoad(row); slAddHead(&list, el); } lineFileClose(&lf); slReverse(&list); return list; } struct cdwStepDef *cdwStepDefCommaIn(char **pS, struct cdwStepDef *ret) /* Create a cdwStepDef out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new cdwStepDef */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->id = sqlUnsignedComma(&s); ret->name = sqlStringComma(&s); ret->description = sqlStringComma(&s); *pS = s; return ret; } void cdwStepDefFree(struct cdwStepDef **pEl) /* Free a single dynamically allocated cdwStepDef such as created * with cdwStepDefLoad(). */ { struct cdwStepDef *el; if ((el = *pEl) == NULL) return; freeMem(el->name); freeMem(el->description); freez(pEl); } void cdwStepDefFreeList(struct cdwStepDef **pList) /* Free a list of dynamically allocated cdwStepDef's */ { struct cdwStepDef *el, *next; for (el = *pList; el != NULL; el = next) { next = el->next; cdwStepDefFree(&el); } *pList = NULL; } void cdwStepDefOutput(struct cdwStepDef *el, FILE *f, char sep, char lastSep) /* Print out cdwStepDef. Separate fields with sep. Follow last field with lastSep. */ { fprintf(f, "%u", el->id); fputc(sep,f); if (sep == ',') fputc('"',f); fprintf(f, "%s", el->name); if (sep == ',') fputc('"',f); fputc(sep,f); if (sep == ',') fputc('"',f); fprintf(f, "%s", el->description); if (sep == ',') fputc('"',f); fputc(lastSep,f); } char *cdwStepRunCommaSepFieldNames = "id,stepDef,stepVersion"; void cdwStepRunStaticLoad(char **row, struct cdwStepRun *ret) /* Load a row from cdwStepRun table into ret. The contents of ret will * be replaced at the next call to this function. */ { ret->id = sqlUnsigned(row[0]); ret->stepDef = sqlUnsigned(row[1]); ret->stepVersion = row[2]; } struct cdwStepRun *cdwStepRunLoadByQuery(struct sqlConnection *conn, char *query) /* Load all cdwStepRun from table that satisfy the query given. * Where query is of the form 'select * from example where something=something' * or 'select example.* from example, anotherTable where example.something = * anotherTable.something'. * Dispose of this with cdwStepRunFreeList(). */ { struct cdwStepRun *list = NULL, *el; struct sqlResult *sr; char **row; sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) { el = cdwStepRunLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void cdwStepRunSaveToDb(struct sqlConnection *conn, struct cdwStepRun *el, char *tableName, int updateSize) /* Save cdwStepRun as a row to the table specified by tableName. * As blob fields may be arbitrary size updateSize specifies the approx size * of a string that would contain the entire query. Arrays of native types are * converted to comma separated strings and loaded as such, User defined types are * inserted as NULL. This function automatically escapes quoted strings for mysql. */ { -struct dyString *update = newDyString(updateSize); +struct dyString *update = dyStringNew(updateSize); sqlDyStringPrintf(update, "insert into %s values ( %u,%u,'%s')", tableName, el->id, el->stepDef, el->stepVersion); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct cdwStepRun *cdwStepRunLoad(char **row) /* Load a cdwStepRun from row fetched with select * from cdwStepRun * from database. Dispose of this with cdwStepRunFree(). */ { struct cdwStepRun *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->stepDef = sqlUnsigned(row[1]); ret->stepVersion = cloneString(row[2]); return ret; } struct cdwStepRun *cdwStepRunLoadAll(char *fileName) /* Load all cdwStepRun from a whitespace-separated file. * Dispose of this with cdwStepRunFreeList(). */ { struct cdwStepRun *list = NULL, *el; struct lineFile *lf = lineFileOpen(fileName, TRUE); char *row[3]; while (lineFileRow(lf, row)) { el = cdwStepRunLoad(row); slAddHead(&list, el); } lineFileClose(&lf); slReverse(&list); return list; } struct cdwStepRun *cdwStepRunLoadAllByChar(char *fileName, char chopper) /* Load all cdwStepRun from a chopper separated file. * Dispose of this with cdwStepRunFreeList(). */ { struct cdwStepRun *list = NULL, *el; struct lineFile *lf = lineFileOpen(fileName, TRUE); char *row[3]; while (lineFileNextCharRow(lf, chopper, row, ArraySize(row))) { el = cdwStepRunLoad(row); slAddHead(&list, el); } lineFileClose(&lf); slReverse(&list); return list; } struct cdwStepRun *cdwStepRunCommaIn(char **pS, struct cdwStepRun *ret) /* Create a cdwStepRun out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new cdwStepRun */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->id = sqlUnsignedComma(&s); ret->stepDef = sqlUnsignedComma(&s); ret->stepVersion = sqlStringComma(&s); *pS = s; return ret; } void cdwStepRunFree(struct cdwStepRun **pEl) /* Free a single dynamically allocated cdwStepRun such as created * with cdwStepRunLoad(). */ { struct cdwStepRun *el; if ((el = *pEl) == NULL) return; freeMem(el->stepVersion); freez(pEl); } void cdwStepRunFreeList(struct cdwStepRun **pList) /* Free a list of dynamically allocated cdwStepRun's */ { struct cdwStepRun *el, *next; for (el = *pList; el != NULL; el = next) { next = el->next; cdwStepRunFree(&el); } *pList = NULL; } void cdwStepRunOutput(struct cdwStepRun *el, FILE *f, char sep, char lastSep) /* Print out cdwStepRun. Separate fields with sep. Follow last field with lastSep. */ { fprintf(f, "%u", el->id); fputc(sep,f); fprintf(f, "%u", el->stepDef); fputc(sep,f); if (sep == ',') fputc('"',f); fprintf(f, "%s", el->stepVersion); if (sep == ',') fputc('"',f); fputc(lastSep,f); } char *cdwStepInCommaSepFieldNames = "id,stepRunId,name,ix,fileId"; void cdwStepInStaticLoad(char **row, struct cdwStepIn *ret) /* Load a row from cdwStepIn table into ret. The contents of ret will * be replaced at the next call to this function. */ { ret->id = sqlUnsigned(row[0]); ret->stepRunId = sqlUnsigned(row[1]); ret->name = row[2]; ret->ix = sqlUnsigned(row[3]); ret->fileId = sqlUnsigned(row[4]); } struct cdwStepIn *cdwStepInLoadByQuery(struct sqlConnection *conn, char *query) /* Load all cdwStepIn from table that satisfy the query given. * Where query is of the form 'select * from example where something=something' * or 'select example.* from example, anotherTable where example.something = * anotherTable.something'. * Dispose of this with cdwStepInFreeList(). */ { struct cdwStepIn *list = NULL, *el; struct sqlResult *sr; char **row; sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) { el = cdwStepInLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void cdwStepInSaveToDb(struct sqlConnection *conn, struct cdwStepIn *el, char *tableName, int updateSize) /* Save cdwStepIn as a row to the table specified by tableName. * As blob fields may be arbitrary size updateSize specifies the approx size * of a string that would contain the entire query. Arrays of native types are * converted to comma separated strings and loaded as such, User defined types are * inserted as NULL. This function automatically escapes quoted strings for mysql. */ { -struct dyString *update = newDyString(updateSize); +struct dyString *update = dyStringNew(updateSize); sqlDyStringPrintf(update, "insert into %s values ( %u,%u,'%s',%u,%u)", tableName, el->id, el->stepRunId, el->name, el->ix, el->fileId); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct cdwStepIn *cdwStepInLoad(char **row) /* Load a cdwStepIn from row fetched with select * from cdwStepIn * from database. Dispose of this with cdwStepInFree(). */ { struct cdwStepIn *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->stepRunId = sqlUnsigned(row[1]); ret->name = cloneString(row[2]); ret->ix = sqlUnsigned(row[3]); ret->fileId = sqlUnsigned(row[4]); return ret; } struct cdwStepIn *cdwStepInLoadAll(char *fileName) /* Load all cdwStepIn from a whitespace-separated file. * Dispose of this with cdwStepInFreeList(). */ { struct cdwStepIn *list = NULL, *el; struct lineFile *lf = lineFileOpen(fileName, TRUE); char *row[5]; while (lineFileRow(lf, row)) { el = cdwStepInLoad(row); slAddHead(&list, el); } lineFileClose(&lf); slReverse(&list); return list; } struct cdwStepIn *cdwStepInLoadAllByChar(char *fileName, char chopper) /* Load all cdwStepIn from a chopper separated file. * Dispose of this with cdwStepInFreeList(). */ { struct cdwStepIn *list = NULL, *el; struct lineFile *lf = lineFileOpen(fileName, TRUE); char *row[5]; while (lineFileNextCharRow(lf, chopper, row, ArraySize(row))) { el = cdwStepInLoad(row); slAddHead(&list, el); } lineFileClose(&lf); slReverse(&list); return list; } struct cdwStepIn *cdwStepInCommaIn(char **pS, struct cdwStepIn *ret) /* Create a cdwStepIn out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new cdwStepIn */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->id = sqlUnsignedComma(&s); ret->stepRunId = sqlUnsignedComma(&s); ret->name = sqlStringComma(&s); ret->ix = sqlUnsignedComma(&s); ret->fileId = sqlUnsignedComma(&s); *pS = s; return ret; } void cdwStepInFree(struct cdwStepIn **pEl) /* Free a single dynamically allocated cdwStepIn such as created * with cdwStepInLoad(). */ { struct cdwStepIn *el; if ((el = *pEl) == NULL) return; freeMem(el->name); freez(pEl); } void cdwStepInFreeList(struct cdwStepIn **pList) /* Free a list of dynamically allocated cdwStepIn's */ { struct cdwStepIn *el, *next; for (el = *pList; el != NULL; el = next) { next = el->next; cdwStepInFree(&el); } *pList = NULL; } void cdwStepInOutput(struct cdwStepIn *el, FILE *f, char sep, char lastSep) /* Print out cdwStepIn. Separate fields with sep. Follow last field with lastSep. */ { fprintf(f, "%u", el->id); fputc(sep,f); fprintf(f, "%u", el->stepRunId); fputc(sep,f); if (sep == ',') fputc('"',f); fprintf(f, "%s", el->name); if (sep == ',') fputc('"',f); fputc(sep,f); fprintf(f, "%u", el->ix); fputc(sep,f); fprintf(f, "%u", el->fileId); fputc(lastSep,f); } char *cdwStepOutCommaSepFieldNames = "id,stepRunId,name,ix,fileId"; void cdwStepOutStaticLoad(char **row, struct cdwStepOut *ret) /* Load a row from cdwStepOut table into ret. The contents of ret will * be replaced at the next call to this function. */ { ret->id = sqlUnsigned(row[0]); ret->stepRunId = sqlUnsigned(row[1]); ret->name = row[2]; ret->ix = sqlUnsigned(row[3]); ret->fileId = sqlUnsigned(row[4]); } struct cdwStepOut *cdwStepOutLoadByQuery(struct sqlConnection *conn, char *query) /* Load all cdwStepOut from table that satisfy the query given. * Where query is of the form 'select * from example where something=something' * or 'select example.* from example, anotherTable where example.something = * anotherTable.something'. * Dispose of this with cdwStepOutFreeList(). */ { struct cdwStepOut *list = NULL, *el; struct sqlResult *sr; char **row; sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) { el = cdwStepOutLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void cdwStepOutSaveToDb(struct sqlConnection *conn, struct cdwStepOut *el, char *tableName, int updateSize) /* Save cdwStepOut as a row to the table specified by tableName. * As blob fields may be arbitrary size updateSize specifies the approx size * of a string that would contain the entire query. Arrays of native types are * converted to comma separated strings and loaded as such, User defined types are * inserted as NULL. This function automatically escapes quoted strings for mysql. */ { -struct dyString *update = newDyString(updateSize); +struct dyString *update = dyStringNew(updateSize); sqlDyStringPrintf(update, "insert into %s values ( %u,%u,'%s',%u,%u)", tableName, el->id, el->stepRunId, el->name, el->ix, el->fileId); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct cdwStepOut *cdwStepOutLoad(char **row) /* Load a cdwStepOut from row fetched with select * from cdwStepOut * from database. Dispose of this with cdwStepOutFree(). */ { struct cdwStepOut *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->stepRunId = sqlUnsigned(row[1]); ret->name = cloneString(row[2]); ret->ix = sqlUnsigned(row[3]); ret->fileId = sqlUnsigned(row[4]); return ret; } struct cdwStepOut *cdwStepOutLoadAll(char *fileName) /* Load all cdwStepOut from a whitespace-separated file. * Dispose of this with cdwStepOutFreeList(). */ { struct cdwStepOut *list = NULL, *el; struct lineFile *lf = lineFileOpen(fileName, TRUE); char *row[5]; while (lineFileRow(lf, row)) { el = cdwStepOutLoad(row); slAddHead(&list, el); } lineFileClose(&lf); slReverse(&list); return list; } struct cdwStepOut *cdwStepOutLoadAllByChar(char *fileName, char chopper) /* Load all cdwStepOut from a chopper separated file. * Dispose of this with cdwStepOutFreeList(). */ { struct cdwStepOut *list = NULL, *el; struct lineFile *lf = lineFileOpen(fileName, TRUE); char *row[5]; while (lineFileNextCharRow(lf, chopper, row, ArraySize(row))) { el = cdwStepOutLoad(row); slAddHead(&list, el); } lineFileClose(&lf); slReverse(&list); return list; } struct cdwStepOut *cdwStepOutCommaIn(char **pS, struct cdwStepOut *ret) /* Create a cdwStepOut out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new cdwStepOut */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->id = sqlUnsignedComma(&s); ret->stepRunId = sqlUnsignedComma(&s); ret->name = sqlStringComma(&s); ret->ix = sqlUnsignedComma(&s); ret->fileId = sqlUnsignedComma(&s); *pS = s; return ret; } void cdwStepOutFree(struct cdwStepOut **pEl) /* Free a single dynamically allocated cdwStepOut such as created * with cdwStepOutLoad(). */ { struct cdwStepOut *el; if ((el = *pEl) == NULL) return; freeMem(el->name); freez(pEl); } void cdwStepOutFreeList(struct cdwStepOut **pList) /* Free a list of dynamically allocated cdwStepOut's */ { struct cdwStepOut *el, *next; for (el = *pList; el != NULL; el = next) { next = el->next; cdwStepOutFree(&el); } *pList = NULL; } void cdwStepOutOutput(struct cdwStepOut *el, FILE *f, char sep, char lastSep) /* Print out cdwStepOut. Separate fields with sep. Follow last field with lastSep. */ { fprintf(f, "%u", el->id); fputc(sep,f); fprintf(f, "%u", el->stepRunId); fputc(sep,f); if (sep == ',') fputc('"',f); fprintf(f, "%s", el->name); if (sep == ',') fputc('"',f); fputc(sep,f); fprintf(f, "%u", el->ix); fputc(sep,f); fprintf(f, "%u", el->fileId); fputc(lastSep,f); } /* -------------------------------- End autoSql Generated Code -------------------------------- */