Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cases] Escape special characters in Parent issue field of Jira connector #145610

Merged

Conversation

js-jankisalvi
Copy link
Contributor

@js-jankisalvi js-jankisalvi commented Nov 17, 2022

Summary

Fixes #131281

Escapes special characters + - & | ! ( ) { } [ ] ^ ~ * ? \ : from parent issue field.

Before
image

After
image

image

Checklist

@js-jankisalvi js-jankisalvi added bug Fixes for quality problems that affect the customer experience Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) Feature:Cases Cases feature v8.6.0 labels Nov 17, 2022
@js-jankisalvi js-jankisalvi self-assigned this Nov 17, 2022
@js-jankisalvi js-jankisalvi requested a review from a team as a code owner November 17, 2022 18:41
@js-jankisalvi js-jankisalvi requested a review from a team November 17, 2022 18:41
@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops-cases (Feature:Cases)

@js-jankisalvi js-jankisalvi changed the title fix: escape special characters in Parent issue filed of stack connectors [Cases] escape special characters in Parent issue filed of stack connectors Nov 17, 2022
@cnasikas cnasikas changed the title [Cases] escape special characters in Parent issue filed of stack connectors [Cases] escape special characters in Parent issue filed of Jira connector Nov 17, 2022
@cnasikas cnasikas changed the title [Cases] escape special characters in Parent issue filed of Jira connector [Cases] Escape special characters in Parent issue filed of Jira connector Nov 17, 2022
@cnasikas cnasikas added v8.7.0 release_note:skip Skip the PR/issue when compiling release notes labels Nov 17, 2022
configurationUtilities,
data: {
fields: {
summary: '[th!s^is()a-te+st-{~is*s|ue?or&and\\bye:}]',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm should the characters here be escaped?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it escapes only special characters, rest will be as it is. For above example the it will be: \\[th\\!s\\^is\\(\\)a\\-te\\+st\\-\\{\\~is\\*s\\|ue\\?or\\&and\\\\\\bye\\:\\}\\]

@@ -136,6 +136,9 @@ export const createExternalService = (
}, '');
};

const escapeSpecialCharacters = (str: string) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's generally not an ideal solution to expose internal functions just for testing but could we do that here? Regex always makes me scared haha 😨 so it'd be good to have some tests around this function directly. It'll be way easier to test if we just export it from this file tool.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also add a comment that includes this link: https://confluence.atlassian.com/jirasoftwareserver/search-syntax-for-text-fields-939938747.html

maybe something like:

These characters need to be escaped per Jira's search syntax, see for more details.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added comment in a73197

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's generally not an ideal solution to expose internal functions just for testing but could we do that here? Regex always makes me scared haha 😨 so it'd be good to have some tests around this function directly. It'll be way easier to test if we just export it from this file tool.

Shall I put this function in a x-pack/plugins/stack_connectors/server/connector_types/cases/jira/utils.ts for better access and tests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in f060450

@@ -136,6 +136,9 @@ export const createExternalService = (
}, '');
};

const escapeSpecialCharacters = (str: string) =>
str.replace(/[!^&*()+\-[\]\\/{}|:?~]/g, '\\\\$&')?.replace(/-/g, '\\\\x2d');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to escape unicode? I don't see it initially from searching this site: https://confluence.atlassian.com/jirasoftwareserver/search-syntax-for-text-fields-939938747.html

It'd be worth some manual testing though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need it. The encodeURIComponent will encode the URL correctly.

Copy link
Contributor

@jonathan-buttner jonathan-buttner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an additional improvement could we do a check for that particular error message that we're getting and transform it into something a little more useful for the user?

Maybe something like Search input is not formatted correctly and the include the jira error after that in the message.

@@ -443,6 +443,70 @@ describe('Jira service', () => {
});
});

test('escapes special characters from summary', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests the createIncident function. We need to test the getIssues function. Something like this:

test('it should escape JQL special characters', async () => {
      requestMock.mockImplementation(() =>
        createAxiosResponse({
          data: {
            issues: issuesResponse,
          },
        })
      );

      await service.getIssues('[th!s^is()a-te+st-{~is*s|ue?or&and\\bye:}]');
      expect(requestMock).toHaveBeenLastCalledWith({
        axios,
        logger,
        method: 'get',
        configurationUtilities,
        url: <the_url_escaped>,
      });
    });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in a73197

Copy link
Member

@cnasikas cnasikas Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Janki. I don't think we need the tests for the createIncident. We do not escape anything in the function neither we added any new logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, will remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@js-jankisalvi
Copy link
Contributor Author

js-jankisalvi commented Nov 18, 2022

For an additional improvement could we do a check for that particular error message that we're getting and transform it into something a little more useful for the user?

Maybe something like Search input is not formatted correctly and the include the jira error after that in the message.

Made a change in createErrorMessage function to handle error messages better in commit

So in this function we have 2 props of error response: const { errorMessages, errors } = errorResponse;
Turns out sometimes errors is null or empty but errorMessages has a descriptive error message. Which was not shown because our first condition was
if (errors == null) { return 'unknown: errorResponse.errors was null'; }

  • To reproduce this scenario, you can comment my escape character function and try putting some special characters on the Parent issue field:

Screenshot 2022-11-18 at 14 29 42

@peteharverson peteharverson changed the title [Cases] Escape special characters in Parent issue filed of Jira connector [Cases] Escape special characters in Parent issue field of Jira connector Nov 22, 2022
Copy link
Member

@cnasikas cnasikas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Janki! Amazing job 🚀

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Unknown metric groups

ESLint disabled in files

id before after diff
osquery 1 2 +1

ESLint disabled line counts

id before after diff
enterpriseSearch 19 21 +2
fleet 59 65 +6
osquery 109 115 +6
securitySolution 442 448 +6
total +20

Total ESLint disabled count

id before after diff
enterpriseSearch 20 22 +2
fleet 67 73 +6
osquery 110 117 +7
securitySolution 519 525 +6
total +21

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @js-jankisalvi

@js-jankisalvi js-jankisalvi merged commit a42314d into elastic:main Nov 22, 2022
kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Nov 22, 2022
…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)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.6

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Nov 22, 2022
… connector (#145610) (#146009)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[Cases] Escape special characters in Parent issue field of Jira
connector (#145610)](#145610)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Janki
Salvi","email":"[email protected]"},"sourceCommit":{"committedDate":"2022-11-22T15:06:29Z","message":"[Cases]
Escape special characters in Parent issue field of Jira connector
(#145610)\n\n## Summary\r\n\r\nFixes #131281\r\n \r\nEscapes special
characters `+ - & | ! ( ) { } [ ] ^ ~ * ? \\ :` from\r\nparent issue
field.\r\n\r\n**Before**\r\n\r\n![image](https://user-images.githubusercontent.com/117571355/202526389-a3428c44-45b5-498c-98af-4ca709ae6937.png)\r\n\r\n**After**\r\n\r\n![image](https://user-images.githubusercontent.com/117571355/202525304-5023f27c-c3df-4839-8c5d-231dcd4a74e6.png)\r\n\r\n\r\n![image](https://user-images.githubusercontent.com/117571355/202526111-4324ce5b-ea13-4bb6-8d96-388178b1b60d.png)\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"a42314d27046c34c7eebc4570f548870636ffe93","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","Team:ResponseOps","Feature:Cases","v8.6.0","v8.7.0"],"number":145610,"url":"https://github.com/elastic/kibana/pull/145610","mergeCommit":{"message":"[Cases]
Escape special characters in Parent issue field of Jira connector
(#145610)\n\n## Summary\r\n\r\nFixes #131281\r\n \r\nEscapes special
characters `+ - & | ! ( ) { } [ ] ^ ~ * ? \\ :` from\r\nparent issue
field.\r\n\r\n**Before**\r\n\r\n![image](https://user-images.githubusercontent.com/117571355/202526389-a3428c44-45b5-498c-98af-4ca709ae6937.png)\r\n\r\n**After**\r\n\r\n![image](https://user-images.githubusercontent.com/117571355/202525304-5023f27c-c3df-4839-8c5d-231dcd4a74e6.png)\r\n\r\n\r\n![image](https://user-images.githubusercontent.com/117571355/202526111-4324ce5b-ea13-4bb6-8d96-388178b1b60d.png)\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"a42314d27046c34c7eebc4570f548870636ffe93"}},"sourceBranch":"main","suggestedTargetBranches":["8.6"],"targetPullRequestStates":[{"branch":"8.6","label":"v8.6.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/145610","number":145610,"mergeCommit":{"message":"[Cases]
Escape special characters in Parent issue field of Jira connector
(#145610)\n\n## Summary\r\n\r\nFixes #131281\r\n \r\nEscapes special
characters `+ - & | ! ( ) { } [ ] ^ ~ * ? \\ :` from\r\nparent issue
field.\r\n\r\n**Before**\r\n\r\n![image](https://user-images.githubusercontent.com/117571355/202526389-a3428c44-45b5-498c-98af-4ca709ae6937.png)\r\n\r\n**After**\r\n\r\n![image](https://user-images.githubusercontent.com/117571355/202525304-5023f27c-c3df-4839-8c5d-231dcd4a74e6.png)\r\n\r\n\r\n![image](https://user-images.githubusercontent.com/117571355/202526111-4324ce5b-ea13-4bb6-8d96-388178b1b60d.png)\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"a42314d27046c34c7eebc4570f548870636ffe93"}}]}]
BACKPORT-->

Co-authored-by: Janki Salvi <[email protected]>
@js-jankisalvi js-jankisalvi deleted the third-party-connector-special-chars branch January 4, 2023 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes for quality problems that affect the customer experience Feature:Cases Cases feature release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v8.6.0 v8.7.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ResponseOps] the 3rd party connector 'parent issue' field throws a 400 error if you type special chars
6 participants