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/makeDb/hgLoadSqlTab/hgLoadSqlTab.c src/hg/makeDb/hgLoadSqlTab/hgLoadSqlTab.c index a72f078..2cf4d39 100644 --- src/hg/makeDb/hgLoadSqlTab/hgLoadSqlTab.c +++ src/hg/makeDb/hgLoadSqlTab/hgLoadSqlTab.c @@ -50,63 +50,68 @@ { struct lineFile *lf = lineFileOpen(fileName, TRUE); struct dyString *dy = dyStringNew(0); char *line, *word; if (!lineFileNextReal(lf, &line)) errAbort("No real lines in %s\n", fileName); word = nextWord(&line); if (!sameWord(word, "create")) errAbort("Expecting first word in file to be CREATE. Got %s", word); word = nextWord(&line); if (word == NULL || !sameWord(word, "table")) errAbort("Expecting second word in file to be table. Got %s", emptyForNull(word)); word = nextWord(&line); if (word == NULL) errAbort("Expecting table name on same line as CREATE TABLE"); -sqlDyStringPrintf(dy, "CREATE TABLE %s ", table); +dyStringPrintf(dy, "CREATE TABLE %s ", table); // trust .sql file off disk. if (line != NULL) dyStringAppend(dy, line); dyStringAppendC(dy, '\n'); while (lineFileNext(lf, &line, NULL)) { dyStringAppend(dy, line); dyStringAppendC(dy, '\n'); } lineFileClose(&lf); return dy; } void hgLoadSqlTab(char *database, char *table, char *createFile, int inCount, char *inNames[]) /* hgLoadSqlTab - Load table into database from SQL and text files. */ { struct sqlConnection *conn = sqlConnect(database); int loadOptions = 0; int i=0; boolean oldTable = optionExists("oldTable") || optionExists("append"); if (optionExists("warn")) loadOptions |= SQL_TAB_FILE_WARN_ON_ERROR; if (! optionExists("notOnServer")) loadOptions |= SQL_TAB_FILE_ON_SERVER; if (! oldTable) { + sqlCkId(table); struct dyString *dy = readAndReplaceTableName(createFile, table); - sqlRemakeTable(conn, table, dy->string); + // trust sql loaded from disk file + struct dyString *dyTrust = dyStringNew(1024); + sqlDyStringPrintf(dyTrust, dy->string, NULL); + sqlRemakeTable(conn, table, dyTrust->string); dyStringFree(&dy); + dyStringFree(&dyTrust); } verbose(1, "Scanning through %d files\n", inCount); for (i=0; i < inCount; i++) { verbose(2, "Loading file %s into table %s\n", inNames[i], table); if (sameString("stdin", inNames[i])) sqlLoadTabFile(conn, "/dev/stdin", table, (loadOptions & ~SQL_TAB_FILE_ON_SERVER)); else sqlLoadTabFile(conn, inNames[i], table, loadOptions); } if (oldTable) hgHistoryComment(conn, "Add contents of %d text file(s) to table %s.", inCount, table); else