OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[32354] Correctly escape filters in frontend to avoid unescape
Oliver Günther7b56b1c88663
diff --git a/app/services/api/v3/parse_query_params_service.rb b/app/services/api/v3/parse_query_params_service.rbindex f4ee6993140..f5c2a491e31 100644--- a/app/services/api/v3/parse_query_params_service.rb+++ b/app/services/api/v3/parse_query_params_service.rb@@ -126,7 +126,7 @@ module APIdef filter_from_params(filter)attribute = filter.keys.first # there should only be one attribute per filteroperator = filter[attribute]['operator']- values = Array(filter[attribute]['values']).map { |v| CGI.unescape(v.to_s) }+ values = Array(filter[attribute]['values'])ar_attribute = convert_filter_attribute attribute, append_id: true{ field: ar_attribute,diff --git a/frontend/src/app/components/api/api-v3/api-v3-filter-builder.ts b/frontend/src/app/components/api/api-v3/api-v3-filter-builder.tsindex 904bbfddf9b..be2b4615fec 100644--- a/frontend/src/app/components/api/api-v3/api-v3-filter-builder.ts+++ b/frontend/src/app/components/api/api-v3/api-v3-filter-builder.ts@@ -51,14 +51,15 @@ export class ApiV3FilterBuilder {return JSON.stringify(this.filters);}- public toParams():string {+ public toParams(mergeParams:{[key:string]:string} = {}):string {let transformedFilters:string[] = [];transformedFilters = this.filters.map((filter:ApiV3Filter) => {return this.serializeFilter(filter);});- return `filters=${encodeURI(`[${transformedFilters.join(',')}]`)}`;+ let params = { filters: `[${transformedFilters.join(",")}]`, ...mergeParams }+ return new URLSearchParams(params).toString();}private serializeFilter(filter:ApiV3Filter) {diff --git a/frontend/src/app/modules/common/path-helper/apiv3/apiv3-paths.ts b/frontend/src/app/modules/common/path-helper/apiv3/apiv3-paths.tsindex 92ba89c33a5..eff9af71761 100644--- a/frontend/src/app/modules/common/path-helper/apiv3/apiv3-paths.ts+++ b/frontend/src/app/modules/common/path-helper/apiv3/apiv3-paths.ts@@ -157,7 +157,10 @@ export class ApiV3Paths {// Containing the that substring:filters.add('name', '~', [term]);}- return this.apiV3Base + '/principals' + '?' + filters.toParams() + encodeURI('&sortBy=[["name","asc"]]&offset=1&pageSize=10');++ return this.apiV3Base ++ '/principals?' ++ filters.toParams({ sortBy: '[["name","asc"]]', offset: '1', pageSize: '10' });}public wpBySubjectOrId(term:string, idOnly:boolean = false) {@@ -169,6 +172,8 @@ export class ApiV3Paths {filters.add('subjectOrId', '**', [term]);}- return this.apiV3Base + '/work_packages' + '?' + filters.toParams() + encodeURI('&sortBy=[["updatedAt","desc"]]&offset=1&pageSize=10');+ return this.apiV3Base ++ '/work_packages?' ++ filters.toParams({ sortBy: '[["updatedAt","desc"]]', offset: '1', pageSize: '10' });}}diff --git a/frontend/src/app/modules/common/path-helper/path-helper.service.spec.ts b/frontend/src/app/modules/common/path-helper/path-helper.service.spec.tsindex 768dbbd06f9..29db9e06eb5 100644--- a/frontend/src/app/modules/common/path-helper/path-helper.service.spec.ts+++ b/frontend/src/app/modules/common/path-helper/path-helper.service.spec.ts@@ -31,6 +31,10 @@ import {PathHelperService} from './path-helper.service';describe('PathHelper', function() {var PathHelper:PathHelperService = new PathHelperService();+ function encodeParams(object:any) {+ return new URLSearchParams(object).toString();+ }+describe('apiV3', function() {var projectIdentifier = 'majora';@@ -42,18 +46,39 @@ describe('PathHelper', function() {var projectId = '1';var term = 'Maria';+ let params = {+ filters: '[{"status":{"operator":"!","values":["3"]}},{"member":{"operator":"=","values":["1"]}},{"type":{"operator":"=","values":["User","Group"]}},{"id":{"operator":"!","values":["me"]}},{"name":{"operator":"~","values":["Maria"]}}]',+ sortBy: '[["name","asc"]]',+ offset: '1',+ pageSize: '10'+ };+expect(PathHelper.api.v3.principals(projectId, term)- ).toEqual('/api/v3/principals?filters=' + encodeURI('[{"status":{"operator":"!","values":["3"]}},{"member":{"operator":"=","values":["1"]}},{"type":{"operator":"=","values":["User","Group"]}},{"id":{"operator":"!","values":["me"]}},{"name":{"operator":"~","values":["Maria"]}}]&sortBy=[["name","asc"]]&offset=1&pageSize=10'));+ ).toEqual('/api/v3/principals?' + encodeParams(params));});it('should provide a path to work package query on subject or ID ', function() {+ let params = {+ filters: '[{"subjectOrId":{"operator":"**","values":["bogus"]}}]',+ sortBy: '[["updatedAt","desc"]]',+ offset: '1',+ pageSize: '10'+ };+expect(PathHelper.api.v3.wpBySubjectOrId("bogus")- ).toEqual('/api/v3/work_packages?filters=' + encodeURI('[{"subjectOrId":{"operator":"**","values":["bogus"]}}]&sortBy=[["updatedAt","desc"]]&offset=1&pageSize=10'));+ ).toEqual('/api/v3/work_packages?' + encodeParams(params));++ params = {+ filters: '[{"id":{"operator":"=","values":["1234"]}}]',+ sortBy: '[["updatedAt","desc"]]',+ offset: '1',+ pageSize: '10'+ };expect(PathHelper.api.v3.wpBySubjectOrId("1234", true)- ).toEqual('/api/v3/work_packages?filters=' + encodeURI('[{"id":{"operator":"=","values":["1234"]}}]&sortBy=[["updatedAt","desc"]]&offset=1&pageSize=10'));+ ).toEqual('/api/v3/work_packages?' + encodeParams(params));});});});diff --git a/frontend/src/app/modules/global_search/input/global-search-input.component.ts b/frontend/src/app/modules/global_search/input/global-search-input.component.tsindex 150630ce26b..0fd9040827e 100644--- a/frontend/src/app/modules/global_search/input/global-search-input.component.ts+++ b/frontend/src/app/modules/global_search/input/global-search-input.component.ts@@ -216,7 +216,6 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {return Highlighting.inlineClass('status', statusId);}-// return all project scope items and all items which contain the search termpublic customSearchFn(term:string, item:any):boolean {return item.id === undefined || item.subject.toLowerCase().indexOf(term.toLowerCase()) !== -1;@@ -234,8 +233,6 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {idOnly = true;}- query = encodeURIComponent(query);-let href:string = this.PathHelperService.api.v3.wpBySubjectOrId(query, idOnly);this.addSuggestions();diff --git a/spec/features/search_spec.rb b/spec/features/search_spec.rbindex 227c9c825df..7e9941b9a66 100644--- a/spec/features/search_spec.rb+++ b/spec/features/search_spec.rb@@ -48,7 +48,9 @@ describe 'Search', type: :feature, js: true dolet(:query) { 'Subject' }- let(:params) { [project, { q: query }] }+ let(:params) { [project, {q: query}] }++ let(:run_visit) { true }def expect_range(a, b)(a..b).each do |n|@@ -84,7 +86,7 @@ describe 'Search', type: :feature, js: true dologin_as user- visit search_path(*params)+ visit search_path(*params) if run_visitenddescribe 'autocomplete' do@@ -139,7 +141,7 @@ describe 'Search', type: :feature, js: true dodescribe 'work package search' docontext 'search in all projects' do- let(:params) { [project, { q: query, work_packages: 1 }] }+ let(:params) { [project, {q: query, work_packages: 1}] }context 'custom fields not searchable' dolet(:searchable) { false }@@ -170,8 +172,8 @@ describe 'Search', type: :feature, js: true docontext 'custom fields searchable' doit "finds WP global custom fields" doselect_autocomplete(page.find('.top-menu-search--input'),- query: "text",- select_text: "In all projects ↵")+ query: "text",+ select_text: "In all projects ↵")table = Pages::EmbeddedWorkPackagesTable.new(find('.work-packages-embedded-view--container'))table.ensure_work_package_not_listed!(work_packages[0])table.expect_work_package_subject(work_packages[1].subject)@@ -337,4 +339,45 @@ describe 'Search', type: :feature, js: true doendendend++ describe 'params escaping' do+ let(:wp_1) { FactoryBot.create :work_package, subject: "Foo && Bar", project: project }+ let(:wp_2) { FactoryBot.create :work_package, subject: "Foo # Bar", project: project }+ let(:wp_3) { FactoryBot.create :work_package, subject: "Foo &# Bar", project: project }+ let!(:work_packages) { [wp_1, wp_2, wp_3] }+ let(:global_search) { ::Components::GlobalSearch.new }+ let(:table) { Pages::EmbeddedWorkPackagesTable.new(find('.work-packages-embedded-view--container')) }++ let(:run_visit) { false }++ before do+ visit home_path+ end++ it 'properly transmits parameters used in URL query' do+ global_search.search "Foo &"+ # Bug in ng-select causes highlights to break up entities+ global_search.find_option "Foo && Bar"+ global_search.find_option "Foo &# Bar"+ global_search.submit_in_global_scope++ table.ensure_work_package_not_listed! wp_2+ table.expect_work_package_listed(wp_1, wp_3)++ global_search.search "# Bar"+ global_search.find_option "Foo # Bar"+ global_search.find_option "Foo &# Bar"+ global_search.submit_in_global_scope+ table.ensure_work_package_not_listed! wp_1+ table.expect_work_package_listed(wp_2)++ global_search.search "&"+ # Bug in ng-select causes highlights to break up entities+ global_search.find_option "Foo && Bar"+ global_search.find_option "Foo &# Bar"+ global_search.submit_in_global_scope+ table.ensure_work_package_not_listed! wp_2+ table.expect_work_package_listed(wp_1, wp_3)+ end+ endenddiff --git a/spec/features/work_packages/table/queries/filter_spec.rb b/spec/features/work_packages/table/queries/filter_spec.rbindex a62cbe40c27..f13eba93b43 100644--- a/spec/features/work_packages/table/queries/filter_spec.rb+++ b/spec/features/work_packages/table/queries/filter_spec.rb@@ -252,6 +252,95 @@ describe 'filter work packages', js: true doendend+ context 'by string cf inside a project with url-query relevant chars' do+ let(:type) do+ type = FactoryBot.create(:type)++ project.types << type++ type+ end++ let(:work_package_plus) do+ wp = FactoryBot.create :work_package, project: project, type: type+ wp.send("#{string_cf.accessor_name}=", 'G+H')+ wp.save!+ wp+ end++ let(:work_package_and) do+ wp = FactoryBot.create :work_package, project: project, type: type+ wp.send("#{string_cf.accessor_name}=", 'A&B')+ wp.save!+ wp+ end++ let(:string_cf) do+ cf = FactoryBot.create :string_wp_custom_field++ project.work_package_custom_fields << cf+ type.custom_fields << cf++ cf+ end++ before do+ string_cf+ work_package_plus+ work_package_and++ wp_table.visit!+ end++ it 'allows filtering, saving and retrieving the saved filter' do++ # Wait for form to load+ filters.expect_loaded++ filters.open+ filters.add_filter_by(string_cf.name,+ 'is',+ ['G+H'],+ "customField#{string_cf.id}")++ loading_indicator_saveguard+ wp_table.expect_work_package_listed work_package_plus+ wp_table.ensure_work_package_not_listed! work_package_and++ wp_table.save_as('Some query name')++ filters.remove_filter "customField#{string_cf.id}"++ loading_indicator_saveguard+ wp_table.expect_work_package_listed work_package_plus, work_package_and++ last_query = Query.last++ wp_table.visit_query(last_query)++ loading_indicator_saveguard+ wp_table.expect_work_package_listed work_package_plus+ wp_table.ensure_work_package_not_listed! work_package_and++ filters.open++ filters.expect_filter_by(string_cf.name,+ 'is',+ ['G+H'],+ "customField#{string_cf.id}")++ filters.set_filter(string_cf,+ 'is',+ ['A&B'],+ "customField#{string_cf.id}")++ loading_indicator_saveguard+ wp_table.expect_work_package_listed work_package_and+ wp_table.ensure_work_package_not_listed! work_package_plus++ end+ end+context 'by attachment content' dolet(:attachment_a) { FactoryBot.build(:attachment, filename: 'attachment-first.pdf') }let(:attachment_b) { FactoryBot.build(:attachment, filename: 'attachment-second.pdf') }diff --git a/spec/services/api/v3/parse_query_params_service_spec.rb b/spec/services/api/v3/parse_query_params_service_spec.rbindex c11c1db3797..33bb03328bd 100644--- a/spec/services/api/v3/parse_query_params_service_spec.rb+++ b/spec/services/api/v3/parse_query_params_service_spec.rb@@ -230,16 +230,6 @@ describe ::API::V3::ParseQueryParamsService,endend- it_behaves_like 'transforms' do- let(:params) do- { filters: JSON::dump([{ 'subprojectId' => { 'operator' => '=',- 'values' => %w(a%26%23b 2) } }]) }- end- let(:expected) do- { filters: [{ field: 'subproject_id', operator: '=', values: %w(a&#b 2) }] }- end- end-it_behaves_like 'transforms' dolet(:params) do{ filters: JSON::dump([{ 'watcher' => { 'operator' => '=',