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/encode3/eap/lib/eapDb.c src/hg/encode3/eap/lib/eapDb.c index 2f8fd04..762b9fc 100644 --- src/hg/encode3/eap/lib/eapDb.c +++ src/hg/encode3/eap/lib/eapDb.c @@ -47,35 +47,35 @@ el = eapJobLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void eapJobSaveToDb(struct sqlConnection *conn, struct eapJob *el, char *tableName, int updateSize) /* Save eapJob 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',%lld,%lld,'%s',%d,%d,'%s')", tableName, el->id, el->commandLine, el->startTime, el->endTime, el->stderr, el->returnCode, el->cpusRequested, el->parasolId); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct eapJob *eapJobLoad(char **row) /* Load a eapJob from row fetched with select * from eapJob * from database. Dispose of this with eapJobFree(). */ { struct eapJob *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->commandLine = cloneString(row[1]); ret->startTime = sqlLongLong(row[2]); ret->endTime = sqlLongLong(row[3]); ret->stderr = cloneString(row[4]); ret->returnCode = sqlSigned(row[5]); @@ -226,35 +226,35 @@ el = eapSoftwareLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void eapSoftwareSaveToDb(struct sqlConnection *conn, struct eapSoftware *el, char *tableName, int updateSize) /* Save eapSoftware 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','%s','%s')", tableName, el->id, el->name, el->url, el->email, el->metaUuid); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct eapSoftware *eapSoftwareLoad(char **row) /* Load a eapSoftware from row fetched with select * from eapSoftware * from database. Dispose of this with eapSoftwareFree(). */ { struct eapSoftware *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->name = cloneString(row[1]); ret->url = cloneString(row[2]); ret->email = cloneString(row[3]); safecpy(ret->metaUuid, sizeof(ret->metaUuid), row[4]); return ret; @@ -397,35 +397,35 @@ el = eapSwVersionLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void eapSwVersionSaveToDb(struct sqlConnection *conn, struct eapSwVersion *el, char *tableName, int updateSize) /* Save eapSwVersion 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','%s',%d,'%s','%s')", tableName, el->id, el->software, el->version, el->md5, el->redoPriority, el->notes, el->metaUuid); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct eapSwVersion *eapSwVersionLoad(char **row) /* Load a eapSwVersion from row fetched with select * from eapSwVersion * from database. Dispose of this with eapSwVersionFree(). */ { struct eapSwVersion *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->software = cloneString(row[1]); ret->version = cloneString(row[2]); safecpy(ret->md5, sizeof(ret->md5), row[3]); ret->redoPriority = sqlSigned(row[4]); ret->notes = cloneString(row[5]); @@ -564,43 +564,43 @@ el = eapStepLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void eapStepSaveToDb(struct sqlConnection *conn, struct eapStep *el, char *tableName, int updateSize) /* Save eapStep 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); char *inputTypesArray, *inputFormatsArray, *inputDescriptionsArray, *outputNamesInTempDirArray, *outputFormatsArray, *outputTypesArray, *outputDescriptionsArray; inputTypesArray = sqlStringArrayToString(el->inputTypes, el->inCount); inputFormatsArray = sqlStringArrayToString(el->inputFormats, el->inCount); inputDescriptionsArray = sqlStringArrayToString(el->inputDescriptions, el->inCount); outputNamesInTempDirArray = sqlStringArrayToString(el->outputNamesInTempDir, el->outCount); outputFormatsArray = sqlStringArrayToString(el->outputFormats, el->outCount); outputTypesArray = sqlStringArrayToString(el->outputTypes, el->outCount); outputDescriptionsArray = sqlStringArrayToString(el->outputDescriptions, el->outCount); sqlDyStringPrintf(update, "insert into %s values ( %u,'%s',%d,'%s',%u,'%s','%s','%s',%u,'%s','%s','%s','%s','%s')", tableName, el->id, el->name, el->cpusRequested, el->description, el->inCount, inputTypesArray , inputFormatsArray , inputDescriptionsArray , el->outCount, outputNamesInTempDirArray , outputFormatsArray , outputTypesArray , outputDescriptionsArray , el->metaUuid); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); freez(&inputTypesArray); freez(&inputFormatsArray); freez(&inputDescriptionsArray); freez(&outputNamesInTempDirArray); freez(&outputFormatsArray); freez(&outputTypesArray); freez(&outputDescriptionsArray); } struct eapStep *eapStepLoad(char **row) /* Load a eapStep from row fetched with select * from eapStep * from database. Dispose of this with eapStepFree(). */ { struct eapStep *ret; @@ -982,35 +982,35 @@ el = eapStepSoftwareLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void eapStepSoftwareSaveToDb(struct sqlConnection *conn, struct eapStepSoftware *el, char *tableName, int updateSize) /* Save eapStepSoftware 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->step, el->software); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct eapStepSoftware *eapStepSoftwareLoad(char **row) /* Load a eapStepSoftware from row fetched with select * from eapStepSoftware * from database. Dispose of this with eapStepSoftwareFree(). */ { struct eapStepSoftware *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->step = cloneString(row[1]); ret->software = cloneString(row[2]); return ret; } @@ -1136,35 +1136,35 @@ el = eapStepVersionLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void eapStepVersionSaveToDb(struct sqlConnection *conn, struct eapStepVersion *el, char *tableName, int updateSize) /* Save eapStepVersion 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',%u)", tableName, el->id, el->step, el->version); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct eapStepVersion *eapStepVersionLoad(char **row) /* Load a eapStepVersion from row fetched with select * from eapStepVersion * from database. Dispose of this with eapStepVersionFree(). */ { struct eapStepVersion *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->step = cloneString(row[1]); ret->version = sqlUnsigned(row[2]); return ret; } @@ -1287,35 +1287,35 @@ el = eapStepSwVersionLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void eapStepSwVersionSaveToDb(struct sqlConnection *conn, struct eapStepSwVersion *el, char *tableName, int updateSize) /* Save eapStepSwVersion 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,%u)", tableName, el->id, el->stepVersionId, el->swVersionId); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct eapStepSwVersion *eapStepSwVersionLoad(char **row) /* Load a eapStepSwVersion from row fetched with select * from eapStepSwVersion * from database. Dispose of this with eapStepSwVersionFree(). */ { struct eapStepSwVersion *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->stepVersionId = sqlUnsigned(row[1]); ret->swVersionId = sqlUnsigned(row[2]); return ret; } @@ -1442,35 +1442,35 @@ el = eapRunLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void eapRunSaveToDb(struct sqlConnection *conn, struct eapRun *el, char *tableName, int updateSize) /* Save eapRun 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','%s',%u,'%s',%u,'%s',%d,'%s')", tableName, el->id, el->jobId, el->experiment, el->analysisStep, el->stepVersionId, el->tempDir, el->assemblyId, el->jsonResult, el->createStatus, el->metaUuid); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct eapRun *eapRunLoad(char **row) /* Load a eapRun from row fetched with select * from eapRun * from database. Dispose of this with eapRunFree(). */ { struct eapRun *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->jobId = sqlUnsigned(row[1]); safecpy(ret->experiment, sizeof(ret->experiment), row[2]); ret->analysisStep = cloneString(row[3]); ret->stepVersionId = sqlUnsigned(row[4]); ret->tempDir = cloneString(row[5]); @@ -1634,35 +1634,35 @@ el = eapInputLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void eapInputSaveToDb(struct sqlConnection *conn, struct eapInput *el, char *tableName, int updateSize) /* Save eapInput 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,'%s')", tableName, el->id, el->runId, el->name, el->ix, el->fileId, el->val); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct eapInput *eapInputLoad(char **row) /* Load a eapInput from row fetched with select * from eapInput * from database. Dispose of this with eapInputFree(). */ { struct eapInput *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->runId = sqlUnsigned(row[1]); ret->name = cloneString(row[2]); ret->ix = sqlUnsigned(row[3]); ret->fileId = sqlUnsigned(row[4]); ret->val = cloneString(row[5]); @@ -1803,35 +1803,35 @@ el = eapOutputLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void eapOutputSaveToDb(struct sqlConnection *conn, struct eapOutput *el, char *tableName, int updateSize) /* Save eapOutput 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,'%s')", tableName, el->id, el->runId, el->name, el->ix, el->fileId, el->val); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct eapOutput *eapOutputLoad(char **row) /* Load a eapOutput from row fetched with select * from eapOutput * from database. Dispose of this with eapOutputFree(). */ { struct eapOutput *ret; AllocVar(ret); ret->id = sqlUnsigned(row[0]); ret->runId = sqlUnsigned(row[1]); ret->name = cloneString(row[2]); ret->ix = sqlUnsigned(row[3]); ret->fileId = sqlUnsigned(row[4]); ret->val = cloneString(row[5]); @@ -1977,35 +1977,35 @@ el = eapPhantomPeakStatsLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void eapPhantomPeakStatsSaveToDb(struct sqlConnection *conn, struct eapPhantomPeakStats *el, char *tableName, int updateSize) /* Save eapPhantomPeakStats 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','%s',%d,%g,%d,%g,%g,%g,%d)", tableName, el->fileId, el->numReads, el->estFragLength, el->corrEstFragLen, el->phantomPeak, el->corrPhantomPeak, el->argMinCorr, el->minCorr, el->nsc, el->rsc, el->qualityTag); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct eapPhantomPeakStats *eapPhantomPeakStatsLoad(char **row) /* Load a eapPhantomPeakStats from row fetched with select * from eapPhantomPeakStats * from database. Dispose of this with eapPhantomPeakStatsFree(). */ { struct eapPhantomPeakStats *ret; AllocVar(ret); ret->fileId = sqlUnsigned(row[0]); ret->numReads = sqlUnsigned(row[1]); ret->estFragLength = cloneString(row[2]); ret->corrEstFragLen = cloneString(row[3]); ret->phantomPeak = sqlSigned(row[4]); ret->corrPhantomPeak = sqlDouble(row[5]);