From 654a97064495213531efdfc2571f659b45236633 Mon Sep 17 00:00:00 2001 From: zeripath Date: Thu, 18 Jun 2020 15:07:09 +0100 Subject: [PATCH] Fix commenting on non-utf8 encoded files (#11916) * Add comment on non-unicode line to force fail Signed-off-by: Andrew Thornton * Just quote/unquote patch Signed-off-by: Andrew Thornton --- integrations/api_pull_review_test.go | 9 +++-- .../5f/22f7d0d95d614d25a5b68592adb345a4b5c7fd | Bin 0 -> 185 bytes .../90/dcd07da077d1e7cd6032b52d1f79ae2b5f19b2 | 2 ++ .../dd/59742c0f6672911f2b64cba5711ac00593ed32 | Bin 0 -> 118 bytes .../user2/repo1.git/refs/pull/3/head | 2 +- models/issue_comment.go | 32 +++++++++++++++++- 6 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 integrations/gitea-repositories-meta/user2/repo1.git/objects/5f/22f7d0d95d614d25a5b68592adb345a4b5c7fd create mode 100644 integrations/gitea-repositories-meta/user2/repo1.git/objects/90/dcd07da077d1e7cd6032b52d1f79ae2b5f19b2 create mode 100644 integrations/gitea-repositories-meta/user2/repo1.git/objects/dd/59742c0f6672911f2b64cba5711ac00593ed32 diff --git a/integrations/api_pull_review_test.go b/integrations/api_pull_review_test.go index c90a5c11cd..611b34712c 100644 --- a/integrations/api_pull_review_test.go +++ b/integrations/api_pull_review_test.go @@ -86,6 +86,11 @@ func TestAPIPullReview(t *testing.T) { Body: "first old line", OldLineNum: 1, NewLineNum: 0, + }, { + Path: "iso-8859-1.txt", + Body: "this line contains a non-utf-8 character", + OldLineNum: 0, + NewLineNum: 1, }, }, }) @@ -93,7 +98,7 @@ func TestAPIPullReview(t *testing.T) { DecodeJSON(t, resp, &review) assert.EqualValues(t, 6, review.ID) assert.EqualValues(t, "PENDING", review.State) - assert.EqualValues(t, 2, review.CodeCommentsCount) + assert.EqualValues(t, 3, review.CodeCommentsCount) // test SubmitPullReview req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, review.ID, token), &api.SubmitPullReviewOptions{ @@ -104,7 +109,7 @@ func TestAPIPullReview(t *testing.T) { DecodeJSON(t, resp, &review) assert.EqualValues(t, 6, review.ID) assert.EqualValues(t, "APPROVED", review.State) - assert.EqualValues(t, 2, review.CodeCommentsCount) + assert.EqualValues(t, 3, review.CodeCommentsCount) // test DeletePullReview req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token), &api.CreatePullReviewOptions{ diff --git a/integrations/gitea-repositories-meta/user2/repo1.git/objects/5f/22f7d0d95d614d25a5b68592adb345a4b5c7fd b/integrations/gitea-repositories-meta/user2/repo1.git/objects/5f/22f7d0d95d614d25a5b68592adb345a4b5c7fd new file mode 100644 index 0000000000000000000000000000000000000000..17fdf18edb0f23c12d7bcb31a8c1897dd4ff4219 GIT binary patch literal 185 zcmV;q07n0K0gaAZYQr!P0Q;>|?0<>Tu3i?DlH8#eSW9afsHGrpXpUbUq5m_(lztp{ z1@iGthX~Xfa`IH{9%G`6xYHJW**GLbF4l(Zq&hMW9?}#%yO6w#HB;C^nJ%ul??{}p z4{ois1g4k`KlQs0c%Cv4xYi-(iC1~?@%WLY^aDP4D5Y!Rro1gp^b;^-a*kYF;oYLO nW^!{^Wx%b?#4mi!Xw%;QWt&^Aa6kKHT|>U$qAxXnBUMYYLF-#{ literal 0 HcmV?d00001 diff --git a/integrations/gitea-repositories-meta/user2/repo1.git/objects/90/dcd07da077d1e7cd6032b52d1f79ae2b5f19b2 b/integrations/gitea-repositories-meta/user2/repo1.git/objects/90/dcd07da077d1e7cd6032b52d1f79ae2b5f19b2 new file mode 100644 index 0000000000..1404abdaa2 --- /dev/null +++ b/integrations/gitea-repositories-meta/user2/repo1.git/objects/90/dcd07da077d1e7cd6032b52d1f79ae2b5f19b2 @@ -0,0 +1,2 @@ +xeN0D#4 +JA5,xzsV5Dػ7,=o.E卢q5J= r=>4 O!Š6ms8&\EatTIzԅ! dso@a&K5B)r4Q`YLb o`a3@(eԭ5 H\sH99R3)@S_"4sE0R.U|/mۿ]Uz \ No newline at end of file diff --git a/integrations/gitea-repositories-meta/user2/repo1.git/objects/dd/59742c0f6672911f2b64cba5711ac00593ed32 b/integrations/gitea-repositories-meta/user2/repo1.git/objects/dd/59742c0f6672911f2b64cba5711ac00593ed32 new file mode 100644 index 0000000000000000000000000000000000000000..f9137c5f774514b271fb589c407f37fcde96f750 GIT binary patch literal 118 zcmV-+0Ez#20V^p=O;s>7uwXDWFfcPQQ7~p;DCN7ue$6rV4Es9fz?YWQOiJ5#K_!D+ z9bJ4~^>R}fygdTf2HwxAov(Vb(j>C-)$3@J`A|if#re7x7N(ZEhI%CxB@7epT&P`8 Ye)0L)1f#9G@|Ekf<0Ur%0H_Wrbv(N_xBvhE literal 0 HcmV?d00001 diff --git a/integrations/gitea-repositories-meta/user2/repo1.git/refs/pull/3/head b/integrations/gitea-repositories-meta/user2/repo1.git/refs/pull/3/head index 98593d6537..33a9eaa7f1 100644 --- a/integrations/gitea-repositories-meta/user2/repo1.git/refs/pull/3/head +++ b/integrations/gitea-repositories-meta/user2/repo1.git/refs/pull/3/head @@ -1 +1 @@ -4a357436d925b5c974181ff12a994538ddc5a269 +5f22f7d0d95d614d25a5b68592adb345a4b5c7fd diff --git a/models/issue_comment.go b/models/issue_comment.go index 7e940cbec4..452afc79f0 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -11,7 +11,9 @@ import ( "encoding/json" "fmt" "regexp" + "strconv" "strings" + "unicode/utf8" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -143,7 +145,8 @@ type Comment struct { RenderedContent string `xorm:"-"` // Path represents the 4 lines of code cemented by this comment - Patch string `xorm:"TEXT"` + Patch string `xorm:"-"` + PatchQuoted string `xorm:"TEXT patch"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` @@ -199,6 +202,33 @@ func (c *Comment) loadIssue(e Engine) (err error) { return } +// BeforeInsert will be invoked by XORM before inserting a record +func (c *Comment) BeforeInsert() { + c.PatchQuoted = c.Patch + if !utf8.ValidString(c.Patch) { + c.PatchQuoted = strconv.Quote(c.Patch) + } +} + +// BeforeUpdate will be invoked by XORM before updating a record +func (c *Comment) BeforeUpdate() { + c.PatchQuoted = c.Patch + if !utf8.ValidString(c.Patch) { + c.PatchQuoted = strconv.Quote(c.Patch) + } +} + +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (c *Comment) AfterLoad(session *xorm.Session) { + c.Patch = c.PatchQuoted + if len(c.PatchQuoted) > 0 && c.PatchQuoted[0] == '"' { + unquoted, err := strconv.Unquote(c.PatchQuoted) + if err == nil { + c.Patch = unquoted + } + } +} + func (c *Comment) loadPoster(e Engine) (err error) { if c.PosterID <= 0 || c.Poster != nil { return nil