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

fix: escape the event data for slack webhook payload #18424

Merged
merged 1 commit into from
Mar 31, 2023

Conversation

chlins
Copy link
Member

@chlins chlins commented Mar 25, 2023

Escape the event data of slack webhook as original payload is invalid when send to slack.

Fixes: #18423

Thank you for contributing to Harbor!

Comprehensive Summary of your change

Issue being fixed

Fixes #18423

Please indicate you've done the following:

  • Well Written Title and Summary of the PR
  • Label the PR as needed. "release-note/ignore-for-release, release-note/new-feature, release-note/update, release-note/enhancement, release-note/community, release-note/breaking-change, release-note/docs, release-note/infra, release-note/deprecation"
  • Accepted the DCO. Commits without the DCO will delay acceptance.
  • Made sure tests are passing and test coverage is added if needed.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed in website repository.

@codecov
Copy link

codecov bot commented Mar 25, 2023

Codecov Report

Merging #18424 (5026beb) into main (95972ba) will decrease coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #18424      +/-   ##
==========================================
- Coverage   67.40%   67.37%   -0.04%     
==========================================
  Files         982      982              
  Lines      106836   106842       +6     
  Branches     2670     2670              
==========================================
- Hits        72016    71986      -30     
- Misses      30950    30983      +33     
- Partials     3870     3873       +3     
Flag Coverage Δ
unittests 67.37% <100.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...pkg/notifier/handler/notification/slack_handler.go 83.92% <100.00%> (+1.92%) ⬆️

... and 8 files with indirect coverage changes

Comment on lines +122 to +126
{
name: `escape \\"`,
args: args{str: `{\"foo\":\"bar\"}`},
want: `{\\\"foo\\\":\\\"bar\\\"}`,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

according to the args & want, it seems that the code is escaping \", instead of \\". Could you please elaborate it? Thanks

Copy link
Member Author

@chlins chlins Mar 29, 2023

Choose a reason for hiding this comment

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

If the input is {\"foo\":\"bar\"}, then escapeEventData function will firstly escape the symbol ", so the input now will look like {\\"foo\\":\\"bar\\"}, but it's not valid because \ also need to be escaped, so escapeEventData function again convert \\" => \\\" to escape the \.

Copy link
Contributor

@zyyw zyyw Mar 29, 2023

Choose a reason for hiding this comment

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

sorry, it seems that I understand it now. {\"foo\":\"bar\"} is replaced to {\\"foo\\":\\"bar\\"} first by

	// escape " to \"
	str = strings.Replace(str, `"`, `\"`, -1)

and then replaced to {\\\"foo\\\":\\\"bar\\\"} by

	// escape \\" to \\\"
	str = strings.Replace(str, `\\"`, `\\\"`, -1)

@chlins chlins force-pushed the fix/tag-retention-webhook branch from 77c6313 to 9429d9f Compare March 30, 2023 01:58
Copy link
Contributor

@wy65701436 wy65701436 left a comment

Choose a reason for hiding this comment

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

lgtm

Escape the event data of slack webhook as original payload is invalid
when send to slack.

Fixes: goharbor#18423

Signed-off-by: chlins <[email protected]>
@chlins chlins force-pushed the fix/tag-retention-webhook branch from 9429d9f to 5026beb Compare March 31, 2023 05:56
@chlins chlins merged commit 5d953b4 into goharbor:main Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Slack cannot receive TAG_RETENTION webhook
5 participants