From a3f52751bd06d74e3f565300ca15186c8f084903 Mon Sep 17 00:00:00 2001 From: Deven Bansod Date: Sun, 13 Nov 2016 11:33:41 +0530 Subject: [PATCH 1/7] Change get ajax request to post to avoid long request URIs Also, move HTML-embedded JS function calls into JQuery event handlers Fix #12299 Signed-off-by: Deven Bansod --- js/db_search.js | 176 +++++++++++++++++++++-------------------- js/messages.php | 1 + libraries/DbSearch.php | 34 +++----- 3 files changed, 105 insertions(+), 106 deletions(-) diff --git a/js/db_search.js b/js/db_search.js index 2bde7cd9f2..40a88c0508 100644 --- a/js/db_search.js +++ b/js/db_search.js @@ -19,6 +19,8 @@ * Unbind all event handlers before tearing down a page */ AJAX.registerTeardown('db_search.js', function () { + $('a.browse_results').unbind('click'); + $('a.delete_results').unbind('click'); $('#buttonGo').unbind('click'); $('#togglesearchresultlink').unbind('click'); $("#togglequerybox").unbind('click'); @@ -26,91 +28,6 @@ AJAX.registerTeardown('db_search.js', function () { $(document).off('submit', "#db_search_form.ajax"); }); -/** - * Loads the database search results - * - * @param result_path Url of the page to load - * @param table_name Name of table to browse - * - * @return nothing - */ -function loadResult(result_path, table_name, link) -{ - $(function () { - /** Hides the results shown by the delete criteria */ - var $msg = PMA_ajaxShowMessage(PMA_messages.strBrowsing, false); - $('#sqlqueryform').hide(); - $('#togglequerybox').hide(); - /** Load the browse results to the page */ - $("#table-info").show(); - $('#table-link').attr({"href" : 'sql.php' + link }).text(table_name); - var url = result_path + "#searchresults"; - $.get(url, {'ajax_request': true, 'is_js_confirmed': true}, function (data) { - if (typeof data !== 'undefined' && data.success) { - $('#browse-results').html(data.message); - PMA_ajaxRemoveMessage($msg); - $('.table_results').each(function () { - PMA_makegrid(this, true, true, true, true); - }); - $('#browse-results').show(); - PMA_highlightSQL($('#browse-results')); - $('html, body') - .animate({ - scrollTop: $("#browse-results").offset().top - }, 1000); - } else { - PMA_ajaxShowMessage(data.error, false); - } - }); - }); -} - -/** - * Delete the selected search results - * - * @param result_path Url of the page to load - * @param msg Text for the confirmation dialog - * - * @return nothing - */ -function deleteResult(result_path, msg) -{ - $(function () { - /** Hides the results shown by the browse criteria */ - $("#table-info").hide(); - $('#sqlqueryform').hide(); - $('#togglequerybox').hide(); - /** Conformation message for deletion */ - if (confirm(msg)) { - var $msg = PMA_ajaxShowMessage(PMA_messages.strDeleting, false); - /** Load the deleted option to the page*/ - $('#sqlqueryform').html(''); - $.post(result_path, {'ajax_request': true, 'is_js_confirmed': true}, - function (data) { - if (typeof data === 'undefined' || !data.success) { - PMA_ajaxShowMessage(data.error, false); - return; - } - - $('#sqlqueryform').html(data.sql_query); - /** Refresh the search results after the deletion */ - document.getElementById('buttonGo').click(); - $('#togglequerybox').html(PMA_messages.strHideQueryBox); - /** Show the results of the deletion option */ - $('#browse-results').hide(); - $('#sqlqueryform').show(); - $('#togglequerybox').show(); - $('html, body') - .animate({ - scrollTop: $("#browse-results").offset().top - }, 1000); - PMA_ajaxRemoveMessage($msg); - } - ); - } - }); -} - AJAX.registerOnload('db_search.js', function () { /** Hide the table link in the initial search result */ var icon = PMA_getImage('s_tbl.png', '', {'id': 'table-image'}).toString(); @@ -192,6 +109,95 @@ AJAX.registerOnload('db_search.js', function () { /** avoid default click action */ return false; }); + + /* + * Ajax Event handler for retrieving the results from a table + */ + $(document).on('click', 'a.browse_results', function(e){ + e.preventDefault(); + /** Hides the results shown by the delete criteria */ + var $msg = PMA_ajaxShowMessage(PMA_messages.strBrowsing, false); + $('#sqlqueryform').hide(); + $('#togglequerybox').hide(); + /** Load the browse results to the page */ + $("#table-info").show(); + var table_name = $(this).attr('table_name'); + $('#table-link').attr({"href" : $(this).attr('href')}).text(table_name); + + var url = $(this).attr('href') + "#searchresults"; + var browse_sql = $(this).attr('browse_sql'); + var params = { + 'ajax_request': true, + 'is_js_confirmed': true, + 'sql_query' : browse_sql + }; + $.post(url, params, function (data) { + if (typeof data !== 'undefined' && data.success) { + $('#browse-results').html(data.message); + PMA_ajaxRemoveMessage($msg); + $('.table_results').each(function () { + PMA_makegrid(this, true, true, true, true); + }); + $('#browse-results').show(); + PMA_highlightSQL($('#browse-results')); + $('html, body') + .animate({ + scrollTop: $("#browse-results").offset().top + }, 1000); + } else { + PMA_ajaxShowMessage(data.error, false); + } + }); + }); + + /* + * Ajax Event handler for deleting the results from a table + */ + $(document).on('click', 'a.delete_results', function(e){ + e.preventDefault(); + /** Hides the results shown by the browse criteria */ + $("#table-info").hide(); + $('#sqlqueryform').hide(); + $('#togglequerybox').hide(); + /** Conformation message for deletion */ + var msg = PMA_sprintf( + PMA_messages.strConfirmDeleteResults, + $(this).attr('table_name') + ); + if (confirm(msg)) { + var $msg = PMA_ajaxShowMessage(PMA_messages.strDeleting, false); + /** Load the deleted option to the page*/ + $('#sqlqueryform').html(''); + var params = { + 'ajax_request': true, + 'is_js_confirmed': true, + 'sql_query': $(this).attr('sql_query') + }; + var url = $(this).attr('href'); + + $.post(url, params, function (data) { + if (typeof data === 'undefined' || !data.success) { + PMA_ajaxShowMessage(data.error, false); + return; + } + + $('#sqlqueryform').html(data.sql_query); + /** Refresh the search results after the deletion */ + document.getElementById('buttonGo').click(); + $('#togglequerybox').html(PMA_messages.strHideQueryBox); + /** Show the results of the deletion option */ + $('#browse-results').hide(); + $('#sqlqueryform').show(); + $('#togglequerybox').show(); + $('html, body') + .animate({ + scrollTop: $("#browse-results").offset().top + }, 1000); + PMA_ajaxRemoveMessage($msg); + }); + } + }); + /** * Ajax Event handler for retrieving the result of an SQL Query */ diff --git a/js/messages.php b/js/messages.php index 5475bbdbc6..365d0da19e 100644 --- a/js/messages.php +++ b/js/messages.php @@ -380,6 +380,7 @@ $js_messages['strHideSearchResults'] = __('Hide search results'); $js_messages['strShowSearchResults'] = __('Show search results'); $js_messages['strBrowsing'] = __('Browsing'); $js_messages['strDeleting'] = __('Deleting'); +$js_messages['strConfirmDeleteResults'] = __('Delete the matches for the %s table?'); /* For db_routines.js */ $js_messages['MissingReturn'] diff --git a/libraries/DbSearch.php b/libraries/DbSearch.php index 9299bf2800..34763df270 100644 --- a/libraries/DbSearch.php +++ b/libraries/DbSearch.php @@ -334,29 +334,21 @@ class DbSearch $html_output .= ''; // Displays browse/delete link if result count > 0 if ($res_cnt > 0) { - $this_url_params['sql_query'] = $newsearchsqls['select_columns']; + $this_url_params['db'] = $GLOBALS['db']; + $this_url_params['table'] = $each_table; $browse_result_path = 'sql.php' . PMA_URL_getCommon($this_url_params); - $html_output .= '' + $html_output .= '' . __('Browse') . ''; - $this_url_params['sql_query'] = $newsearchsqls['delete']; - $delete_result_path = 'sql.php' . PMA_URL_getCommon($this_url_params); - $html_output .= '' + + $delete_result_path = $browse_result_path; + $html_output .= '' . __('Delete') . ''; } else { $html_output .= ' ' From 3b8f9a55f6349637264aa82f096c7955859081b4 Mon Sep 17 00:00:00 2001 From: Deven Bansod Date: Mon, 14 Nov 2016 10:55:30 +0530 Subject: [PATCH 2/7] Fix tests related to DBSearch Signed-off-by: Deven Bansod --- js/db_search.js | 2 +- libraries/DbSearch.php | 2 +- test/classes/DbSearchTest.php | 28 ++++++++++------------------ 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/js/db_search.js b/js/db_search.js index 40a88c0508..683229bbaf 100644 --- a/js/db_search.js +++ b/js/db_search.js @@ -171,7 +171,7 @@ AJAX.registerOnload('db_search.js', function () { var params = { 'ajax_request': true, 'is_js_confirmed': true, - 'sql_query': $(this).attr('sql_query') + 'sql_query': $(this).attr('delete_sql') }; var url = $(this).attr('href'); diff --git a/libraries/DbSearch.php b/libraries/DbSearch.php index 34763df270..799e90e7e8 100644 --- a/libraries/DbSearch.php +++ b/libraries/DbSearch.php @@ -348,7 +348,7 @@ class DbSearch $html_output .= '' + . ' delete_sql="' . $newsearchsqls['delete'] . '" >' . __('Delete') . ''; } else { $html_output .= ' ' diff --git a/test/classes/DbSearchTest.php b/test/classes/DbSearchTest.php index 31ca9b2404..9bb6038109 100644 --- a/test/classes/DbSearchTest.php +++ b/test/classes/DbSearchTest.php @@ -41,6 +41,7 @@ class DbSearchTest extends PMATestCase $this->object = new DbSearch('pma_test'); $GLOBALS['server'] = 0; $GLOBALS['db'] = 'pma'; + $GLOBALS['collation_connection'] = 'utf-8'; //mock DBI $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface') @@ -166,28 +167,19 @@ class DbSearchTest extends PMATestCase ), true, '2 matches in table1' - . 'Browse' - . 'Browse' + . 'Delete' + . '&is_js_confirmed=0&server=0&' + . 'lang=en&collation_connection=utf-8&token=token" ' + . 'table_name="table1" delete_sql="column2" ' + . '>Delete' ) ); } From 89dfff786868618dcd222be6f8f4c4f9a91c2ecd Mon Sep 17 00:00:00 2001 From: Deven Bansod Date: Tue, 15 Nov 2016 21:14:07 +0530 Subject: [PATCH 3/7] Change to using HTML5 data-* attributes to allow easy access in JQuery Signed-off-by: Deven Bansod --- js/db_search.js | 8 ++++---- libraries/DbSearch.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/js/db_search.js b/js/db_search.js index 683229bbaf..157cfc8015 100644 --- a/js/db_search.js +++ b/js/db_search.js @@ -121,11 +121,11 @@ AJAX.registerOnload('db_search.js', function () { $('#togglequerybox').hide(); /** Load the browse results to the page */ $("#table-info").show(); - var table_name = $(this).attr('table_name'); + var table_name = $(this).data('table-name'); $('#table-link').attr({"href" : $(this).attr('href')}).text(table_name); var url = $(this).attr('href') + "#searchresults"; - var browse_sql = $(this).attr('browse_sql'); + var browse_sql = $(this).data('browse-sql'); var params = { 'ajax_request': true, 'is_js_confirmed': true, @@ -162,7 +162,7 @@ AJAX.registerOnload('db_search.js', function () { /** Conformation message for deletion */ var msg = PMA_sprintf( PMA_messages.strConfirmDeleteResults, - $(this).attr('table_name') + $(this).data('table-name') ); if (confirm(msg)) { var $msg = PMA_ajaxShowMessage(PMA_messages.strDeleting, false); @@ -171,7 +171,7 @@ AJAX.registerOnload('db_search.js', function () { var params = { 'ajax_request': true, 'is_js_confirmed': true, - 'sql_query': $(this).attr('delete_sql') + 'sql_query': $(this).data('delete-sql') }; var url = $(this).attr('href'); diff --git a/libraries/DbSearch.php b/libraries/DbSearch.php index 799e90e7e8..f6617dd8e6 100644 --- a/libraries/DbSearch.php +++ b/libraries/DbSearch.php @@ -340,15 +340,15 @@ class DbSearch $html_output .= '' + . 'data-browse-sql="' . $newsearchsqls['select_columns'] . '" ' + . 'data-table-name="' . $each_table . '" >' . __('Browse') . ''; $delete_result_path = $browse_result_path; $html_output .= '' + . ' data-table-name="' . $each_table . '"' + . ' data-delete-sql="' . $newsearchsqls['delete'] . '" >' . __('Delete') . ''; } else { $html_output .= ' ' From 21093b22e4c1904a523a29e0bf9f4e3870b704ce Mon Sep 17 00:00:00 2001 From: Deven Bansod Date: Tue, 15 Nov 2016 21:47:32 +0530 Subject: [PATCH 4/7] Escape tablenames, dbnames and properly render html-specific elements/chars Signed-off-by: Deven Bansod --- libraries/DbSearch.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libraries/DbSearch.php b/libraries/DbSearch.php index f6617dd8e6..23f227f6e1 100644 --- a/libraries/DbSearch.php +++ b/libraries/DbSearch.php @@ -334,22 +334,23 @@ class DbSearch $html_output .= ''; // Displays browse/delete link if result count > 0 if ($res_cnt > 0) { - $this_url_params['db'] = $GLOBALS['db']; - $this_url_params['table'] = $each_table; + $this_url_params['db'] = htmlspecialchars($GLOBALS['db']); + $this_url_params['table'] = htmlspecialchars($each_table); $browse_result_path = 'sql.php' . PMA_URL_getCommon($this_url_params); $html_output .= '' + . 'data-browse-sql="' + . htmlspecialchars($newsearchsqls['select_columns']). '" ' + . 'data-table-name="' . htmlspecialchars($each_table) . '" >' . __('Browse') . ''; $delete_result_path = $browse_result_path; $html_output .= '' - . __('Delete') . ''; + . ' data-delete-sql="' . htmlspecialchars($newsearchsqls['delete']) + . ' data-table-name="' . htmlspecialchars($each_table) . '"' + . '" >' . __('Delete') . ''; } else { $html_output .= ' ' . ' '; From 51c7739526a7610a12662170b2e09b7f24a4072e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 15 Nov 2016 17:27:16 +0100 Subject: [PATCH 5/7] Changelog entry for #12696 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michal Čihař --- ChangeLog | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog b/ChangeLog index 025650e463..b5030eacb0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -89,6 +89,7 @@ phpMyAdmin - ChangeLog - issue #12542 Missing table name in account privileges editor - issue #12691 Remove ksort call on empty array in PMA_getPlugins function - issue #12443 Check parameter type before processing +- issue #12299 Avoid generating too long URLs in search 4.6.4 (2016-08-16) - issue [security] Weaknesses with cookie encryption, see PMASA-2016-29 From 59b2c5a63ac6568d47494b256e3531ebc38e44d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 15 Nov 2016 17:41:38 +0100 Subject: [PATCH 6/7] Fix markup and tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michal Čihař --- libraries/DbSearch.php | 6 +++--- test/classes/DbSearchTest.php | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/libraries/DbSearch.php b/libraries/DbSearch.php index 23f227f6e1..38f64885ed 100644 --- a/libraries/DbSearch.php +++ b/libraries/DbSearch.php @@ -348,9 +348,9 @@ class DbSearch $delete_result_path = $browse_result_path; $html_output .= '' . __('Delete') . ''; + . ' data-delete-sql="' . htmlspecialchars($newsearchsqls['delete']) . '"' + . ' data-table-name="' . htmlspecialchars($each_table) . '" >' + . __('Delete') . ''; } else { $html_output .= ' ' . ' '; diff --git a/test/classes/DbSearchTest.php b/test/classes/DbSearchTest.php index 9bb6038109..5492d03295 100644 --- a/test/classes/DbSearchTest.php +++ b/test/classes/DbSearchTest.php @@ -172,13 +172,14 @@ class DbSearchTest extends PMATestCase . '=table1&goto=db_sql.php&pos=0&is_js_confirmed=0&' . 'server=0&lang=en&' . 'collation_connection=utf-8&token=token" ' - . 'browse_sql="column1" table_name="table1" ' + . 'data-browse-sql="column1" data-table-name="table1" ' . '>Browse' . 'Delete' ) ); From accd6aa17e70b300c5053a6632a97ddf016dbfbd Mon Sep 17 00:00:00 2001 From: Deven Bansod Date: Tue, 15 Nov 2016 22:28:45 +0530 Subject: [PATCH 7/7] Add token parameter in AJAX POST request Signed-off-by: Deven Bansod --- js/db_search.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/js/db_search.js b/js/db_search.js index 157cfc8015..e38534ae8e 100644 --- a/js/db_search.js +++ b/js/db_search.js @@ -129,7 +129,8 @@ AJAX.registerOnload('db_search.js', function () { var params = { 'ajax_request': true, 'is_js_confirmed': true, - 'sql_query' : browse_sql + 'sql_query' : browse_sql, + 'token' : PMA_commonParams.get('token') }; $.post(url, params, function (data) { if (typeof data !== 'undefined' && data.success) { @@ -171,7 +172,8 @@ AJAX.registerOnload('db_search.js', function () { var params = { 'ajax_request': true, 'is_js_confirmed': true, - 'sql_query': $(this).data('delete-sql') + 'sql_query': $(this).data('delete-sql'), + 'token' : PMA_commonParams.get('token') }; var url = $(this).attr('href');