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

Add support for more GIT SCMs in finding view for the finding URL #9710

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/content/en/integrations/source-code-repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ For Interactive Engagement it needs to be the URL including the branch:
For CI/CD Engagement, where user could set commit hash, branch/tag and code line it should look like examples below:
- for GitHub - like https://github.com/DefectDojo/django-DefectDojo
- for GitLab - like https://gitlab.com/gitlab-org/gitlab
- for public BitBucket - like https://bitbucket.org/some-user/some-project.git (like git clone url)
- for public BitBucket, Gitea and Codeberg - like https://bitbucket.org/some-user/some-project.git (like git clone url)
- for standalone/onpremise BitBucket https://bb.example.com/scm/some-project.git or https://bb.example.com/scm/some-user-name/some-repo.git for user public repo (like git clone url)

If user does not set commit hash or branch/tag in appropriate fields of CI/CD Engagement edit form, the URL should look like in Interactive Engagement edit form.
Expand All @@ -39,7 +39,7 @@ Product SCM type add:

![Product scm type](../../../static/images/product-scm-type_1.png)

Possible SCM types could be 'github', 'gitlab', 'bitbucket', 'bitbucket-standalone' or nothing (for default github).
Possible SCM types could be 'github', 'gitlab', 'bitbucket', 'bitbucket-standalone', 'gitea', 'codeberg' or nothing (for default github).


## Link in Finding
Expand Down
53 changes: 19 additions & 34 deletions dojo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3150,33 +3150,34 @@ def get_scm_type(self):
st = dojo_meta.value.strip()
if st:
return st.lower()
return 'github'
return ''

def bitbucket_public_prepare_scm_base_link(self, uri):
# bitbucket public (https://bitbucket.org) url template for browse is:
# https://bitbucket.org/<username>/<repository-slug>
def scm_public_prepare_base_link(self, uri):
# scm public (https://scm-domain.org) url template for browse is:
# https://scm-domain.org/<username>/<repository-slug>
# but when you get repo url for git, its template is:
# https://bitbucket.org/<username>/<repository-slug>.git
# https://scm-domain.org/<username>/<repository-slug>.git
# so to create browser url - git url should be recomposed like below:

parts_uri = uri.split('.git')
return parts_uri[0]

def bitbucket_public_prepare_scm_link(self, uri):
def git_public_prepare_scm_link(self, uri, scm_type):
# if commit hash or branch/tag is set for engagement/test -
# hash or branch/tag should be appended to base browser link
intermediate_path = '/blob/' if scm_type in ['github', 'gitlab'] else '/src/'

link = self.bitbucket_public_prepare_scm_base_link(uri)
link = self.scm_public_prepare_base_link(uri)
if self.test.commit_hash:
link += '/src/' + self.test.commit_hash + '/' + self.file_path
link += intermediate_path + self.test.commit_hash + '/' + self.file_path
elif self.test.engagement.commit_hash:
link += '/src/' + self.test.engagement.commit_hash + '/' + self.file_path
link += intermediate_path + self.test.engagement.commit_hash + '/' + self.file_path
elif self.test.branch_tag:
link += '/src/' + self.test.branch_tag + '/' + self.file_path
link += intermediate_path + self.test.branch_tag + '/' + self.file_path
elif self.test.engagement.branch_tag:
link += '/src/' + self.test.engagement.branch_tag + '/' + self.file_path
link += intermediate_path + self.test.engagement.branch_tag + '/' + self.file_path
else:
link += '/src/master/' + self.file_path
link += intermediate_path + 'master/' + self.file_path

return link

Expand Down Expand Up @@ -3218,43 +3219,27 @@ def bitbucket_standalone_prepare_scm_link(self, uri):

return link

def github_prepare_scm_link(self, uri):
link = uri

if self.test.commit_hash:
link += '/blob/' + self.test.commit_hash + '/' + self.file_path
elif self.test.engagement.commit_hash:
link += '/blob/' + self.test.engagement.commit_hash + '/' + self.file_path
elif self.test.branch_tag:
link += '/blob/' + self.test.branch_tag + '/' + self.file_path
elif self.test.engagement.branch_tag:
link += '/blob/' + self.test.engagement.branch_tag + '/' + self.file_path
else:
link += '/' + self.file_path

return link

def get_file_path_with_raw_link(self):
if self.file_path is None:
return None

link = self.test.engagement.source_code_management_uri
scm_type = self.get_scm_type()
if (self.test.engagement.source_code_management_uri is not None):
if scm_type == 'github' or ("https://github.com/" in self.test.engagement.source_code_management_uri):
link = self.github_prepare_scm_link(link)
elif scm_type == 'bitbucket-standalone':
if scm_type == 'bitbucket-standalone':
link = self.bitbucket_standalone_prepare_scm_link(link)
elif scm_type == 'bitbucket':
link = self.bitbucket_public_prepare_scm_link(link)
elif scm_type in ['github', 'gitlab', 'gitea', 'codeberg', 'bitbucket']:
link = self.git_public_prepare_scm_link(link, scm_type)
elif 'https://github.com/' in self.test.engagement.source_code_management_uri:
link = self.git_public_prepare_scm_link(link, 'github')
else:
link += '/' + self.file_path
else:
link += '/' + self.file_path

# than - add line part to browser url
if self.line:
if scm_type == 'github' or scm_type == 'gitlab':
if scm_type in ['github', 'gitlab', 'gitea', 'codeberg'] or 'https://github.com/' in self.test.engagement.source_code_management_uri:
link = link + '#L' + str(self.line)
elif scm_type == 'bitbucket-standalone':
link = link + '#' + str(self.line)
Expand Down
47 changes: 47 additions & 0 deletions unittests/test_finding_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,53 @@ def test_get_file_path_with_link_and_source_code_management_uri_bitbucket_standa

self.assertEqual('<a href=\"https://bb.example.com/users/some-user/repos/some-test-repo/browse/some-folder/some-file.ext?at=some-branch#5432" target=\"_blank\" title=\"some-folder/some-file.ext\">some-folder/some-file.ext</a>', finding.get_file_path_with_link())

def test_get_file_path_with_link_and_source_code_management_uri_gitea_or_codeberg_project_with_no_details_and_line(self):
# checks that for gitea and codeberg in custom field
# dojo makes correct url

# create scm-type custom field with value "gitea"
product_type = self.create_product_type('test_product_type')
product = self.create_product(name='test_product', prod_type=product_type)
product_metadata = DojoMeta(product=product, name="scm-type", value="gitea")
product_metadata.save()

# create finding with scm uri line
test = Test()
engagement = Engagement()
engagement.product = product
test.engagement = engagement
finding = Finding()
finding.test = test
finding.file_path = 'some-folder/some-file.ext'
finding.line = 5432

engagement.source_code_management_uri = 'https://bb.example.com/some-test-user/some-test-repo.git'
self.assertEqual('<a href=\"https://bb.example.com/some-test-user/some-test-repo/src/master/some-folder/some-file.ext#L5432" target=\"_blank\" title=\"some-folder/some-file.ext\">some-folder/some-file.ext</a>', finding.get_file_path_with_link())

def test_get_file_path_with_link_and_source_code_management_uri_gitea_or_codeberg_project_with_commithash_and_line(self):
# checks that for gitea and codeberg in custom field and existing commit hash in finding
# dojo makes correct url

# create scm-type custom field with value "gitea"
product_type = self.create_product_type('test_product_type')
product = self.create_product(name='test_product', prod_type=product_type)
product_metadata = DojoMeta(product=product, name="scm-type", value="gitea")
product_metadata.save()

# create finding with scm uri and commit hash, branch and line
test = Test()
engagement = Engagement()
engagement.product = product
test.engagement = engagement
engagement.commit_hash = "some-commit-hash"
finding = Finding()
finding.test = test
finding.file_path = 'some-folder/some-file.ext'
finding.line = 5432

engagement.source_code_management_uri = 'https://bb.example.com/some-test-user/some-test-repo.git'
self.assertEqual('<a href=\"https://bb.example.com/some-test-user/some-test-repo/src/some-commit-hash/some-folder/some-file.ext#L5432" target=\"_blank\" title=\"some-folder/some-file.ext\">some-folder/some-file.ext</a>', finding.get_file_path_with_link())

def test_get_file_path_with_xss_attack(self):
test = Test()
engagement = Engagement()
Expand Down
Loading