From 29ce13e9992c296fbb2c4ad2706f53e491143d3e Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Tue, 27 Aug 2019 22:48:24 -0700 Subject: Fix moving issues API failing when text includes commit URLs When a issue is moved from one project to another, all associated Markdown text is rewritten in the context of the new project. If the note contained a link to a commit URL, `CommitRewriter#rewrite` would fail because `Commit#link_reference_pattern` would match `nil` `commit` values in the HTML generated from the Markdown. These `nil` values were passed along to `Project#commits_by` because `Commit#reference_valid?` was always returning `true`. To prevent this issue from happening, we tighten up the check for `Commit#reference_valid?` to look for valid SHA values. Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/66666 --- app/models/commit.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'app/models/commit.rb') diff --git a/app/models/commit.rb b/app/models/commit.rb index 0889ce7e287..1470b50f396 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -35,6 +35,7 @@ class Commit MIN_SHA_LENGTH = Gitlab::Git::Commit::MIN_SHA_LENGTH COMMIT_SHA_PATTERN = /\h{#{MIN_SHA_LENGTH},40}/.freeze + EXACT_COMMIT_SHA_PATTERN = /\A#{COMMIT_SHA_PATTERN}\z/.freeze # Used by GFM to match and present link extensions on node texts and hrefs. LINK_EXTENSION_PATTERN = /(patch)/.freeze @@ -90,7 +91,7 @@ class Commit end def valid_hash?(key) - !!(/\A#{COMMIT_SHA_PATTERN}\z/ =~ key) + !!(EXACT_COMMIT_SHA_PATTERN =~ key) end def lazy(project, oid) @@ -139,6 +140,10 @@ class Commit '@' end + def self.reference_valid?(reference) + !!(reference =~ EXACT_COMMIT_SHA_PATTERN) + end + # Pattern used to extract commit references from text # # This pattern supports cross-project references. -- cgit v1.2.3