forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cases] Escape special characters in Parent issue field of Jira conne…
…ctor (elastic#145610) ## Summary Fixes elastic#131281 Escapes special characters `+ - & | ! ( ) { } [ ] ^ ~ * ? \ :` from parent issue field. **Before** ![image](https://user-images.githubusercontent.com/117571355/202526389-a3428c44-45b5-498c-98af-4ca709ae6937.png) **After** ![image](https://user-images.githubusercontent.com/117571355/202525304-5023f27c-c3df-4839-8c5d-231dcd4a74e6.png) ![image](https://user-images.githubusercontent.com/117571355/202526111-4324ce5b-ea13-4bb6-8d96-388178b1b60d.png) ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios Co-authored-by: kibanamachine <[email protected]> (cherry picked from commit a42314d)
- Loading branch information
1 parent
6f0a5b3
commit fa46a72
Showing
4 changed files
with
159 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
x-pack/plugins/stack_connectors/server/connector_types/cases/jira/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { escapeJqlSpecialCharacters } from './utils'; | ||
|
||
describe('escapeJqlSpecialCharacters', () => { | ||
it('should escape jql special characters', () => { | ||
const str = '[th!s^is()a-te+st-{~is*s&ue?or|and\\bye:}]"}]'; | ||
const escapedStr = escapeJqlSpecialCharacters(str); | ||
expect(escapedStr).toEqual( | ||
'\\\\[th\\\\!s\\\\^is\\\\(\\\\)a\\\\-te\\\\+st\\\\-\\\\{\\\\~is\\\\*s\\\\&ue\\\\?or\\\\|and\\\\bye\\\\:\\\\}\\\\]\\\\}\\\\]' | ||
); | ||
}); | ||
|
||
it('should remove double quotes', () => { | ||
const str = '"Hello"'; | ||
const escapedStr = escapeJqlSpecialCharacters(str); | ||
expect(escapedStr).toEqual('Hello'); | ||
}); | ||
|
||
it('should replace single quotes with backslash', () => { | ||
const str = "Javascript's beauty is simplicity!"; | ||
const escapedStr = escapeJqlSpecialCharacters(str); | ||
expect(escapedStr).toEqual('Javascript\\\\s beauty is simplicity\\\\!'); | ||
}); | ||
|
||
it('should replace single backslash with four backslash', () => { | ||
const str = '\\I have one backslash'; | ||
const escapedStr = escapeJqlSpecialCharacters(str); | ||
expect(escapedStr).toEqual('\\\\I have one backslash'); | ||
}); | ||
|
||
it('should not escape other special characters', () => { | ||
const str = '<it is, a test.>'; | ||
const escapedStr = escapeJqlSpecialCharacters(str); | ||
expect(escapedStr).toEqual('<it is, a test.>'); | ||
}); | ||
|
||
it('should not escape alpha numeric characters', () => { | ||
const str = 'here is a case 29'; | ||
const escapedStr = escapeJqlSpecialCharacters(str); | ||
expect(escapedStr).toEqual('here is a case 29'); | ||
}); | ||
|
||
it('should not escape unicode spaces', () => { | ||
const str = 'comm\u2000=\u2001"hello"\u3000'; | ||
const escapedStr = escapeJqlSpecialCharacters(str); | ||
expect(escapedStr).toEqual('comm = hello '); | ||
}); | ||
|
||
it('should not escape non ASCII characters', () => { | ||
const str = 'Apple’s amazing idea♥'; | ||
const escapedStr = escapeJqlSpecialCharacters(str); | ||
expect(escapedStr).toEqual('Apple’s amazing idea♥'); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
x-pack/plugins/stack_connectors/server/connector_types/cases/jira/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
// These characters need to be escaped per Jira's search syntax, see for more details: https://confluence.atlassian.com/jirasoftwareserver/search-syntax-for-text-fields-939938747.html | ||
export const JQL_SPECIAL_CHARACTERS_REGEX = /[-!^+&*()[\]/{}|:?~]/; | ||
|
||
const DOUBLE_BACKSLASH_REGEX = '\\\\$&'; | ||
|
||
export const escapeJqlSpecialCharacters = (str: string) => { | ||
return str | ||
.replaceAll('"', '') | ||
.replaceAll(/\\/g, '\\\\') | ||
.replaceAll(/'/g, '\\\\') | ||
.replaceAll(new RegExp(JQL_SPECIAL_CHARACTERS_REGEX, 'g'), DOUBLE_BACKSLASH_REGEX); | ||
}; |