1 回答
TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊
除了一攬子SQL注入(惡意人員試圖執(zhí)行諸如向命令中添加語句之類的操作)之外,還有另一個(gè)問題。由于您的代碼允許運(yùn)行任何有效的表名,因此它仍然存在泄露架構(gòu)中任何表中包含的數(shù)據(jù)的風(fēng)險(xiǎn)。DELETECOPY
因此,這里安全的做法可能是維護(hù)您希望允許用戶訪問的表的白名單。任何與列表不匹配的輸入表名稱都將被拒絕。假設(shè)您的表列表駐留在 中,我們可以對您的代碼進(jìn)行以下更改:List
public void download(String table, Writer responseWriter) throws SQLException, IOException {
// get list of all allowed tables
List<String> fileList = getAllowedTables();
if (!fileList.contains(table)) {
throw new IllegalAccessException("Someone tried to access a forbidden table.");
}
try (Connection conn = dataSource.getConnection()) {
CopyManager copyManager = new CopyManager(conn.unwrap(BaseConnection.class));
// SQL Injection can happen here!
String statement = "COPY " + table + " TO STDOUT WITH NULL ''";
copyManager.copyOut(statement, responseWriter);
}
}
添加回答
舉報(bào)
