{"id":58585,"date":"2023-09-24T20:06:12","date_gmt":"2023-09-24T14:36:12","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=58585"},"modified":"2023-10-05T20:12:07","modified_gmt":"2023-10-05T14:42:07","slug":"securing-code-with-gitlab-dependency-scanning","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/securing-code-with-gitlab-dependency-scanning\/","title":{"rendered":"Securing Code with GitLab Dependency Scanning"},"content":{"rendered":"<h2><b>Introduction<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">GitLab is a web-based platform that provides tools for version control and source code management. It allows software developers to <\/span><span style=\"font-weight: 400;\">coordinate <\/span><span style=\"font-weight: 400;\">projects, track<\/span><span style=\"font-weight: 400;\"> code changes<\/span><span style=\"font-weight: 400;\">, and manage the entire development lifecycle. GitLab offers features such as code repositories, issue tracking, continuous integration and continuous deployment (CI\/CD) pipelines, code reviews, wikis, and more. It is often used as an alternative to GitHub.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In software development,<\/span><span style=\"font-weight: 400;\"> it is necessary to prioritize the security of your code,<\/span><span style=\"font-weight: 400;\"> but we often overlook one aspect of security i.e., managing and securing dependencies.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Third-party libraries and packages can introduce vulnerabilities into your application, leading to potential security breaches and data leaks. GitLab offers a solution to solve this challenge through its Dependency Scanning feature.<\/span><\/p>\n<h2><b>Problem Statement<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Software projects commonly rely on external libraries and frameworks to accelerate development and enhance functionality. These dependencies can boost productivity but can also introduce vulnerabilities that compromise the security of your application. Developers might not always be aware of the potential risks posed by the libraries they include. The problem becomes challenging when we don&#8217;t have the right tools to find these problems automatically.<\/span><\/p>\n<h2><b>Solution: Gitlab Dependency Scanning<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">GitLab&#8217;s Dependency Scanning addresses this problem head-on by automatically identifying and alerting developers about vulnerabilities in their project&#8217;s dependencies. This feature analyzes the dependencies declared in your project&#8217;s manifest files (such as package.json for JavaScript, Gemfile.lock for Ruby, and requirements.txt for Python) and cross-references them against a comprehensive database of known vulnerabilities.<\/span><\/p>\n<h2><b>How GitLab Dependency Scanning Works:<\/b><\/h2>\n<ol>\n<li style=\"font-weight: 400;\"><b>Scanning Process:<\/b><span style=\"font-weight: 400;\"> When a developer pushes new code to a GitLab repository or opens a merge request, the Dependency Scanning process is triggered as part of GitLab&#8217;s CI\/CD pipeline.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Dependency Analysis:<\/b><span style=\"font-weight: 400;\"> The Dependency Scanning tool extracts the dependency information from the manifest files and queries a vulnerability database, which contains information about known vulnerabilities associated with each library or package.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Identification and Reporting:<\/b><span style=\"font-weight: 400;\"> If any vulnerabilities are detected, GitLab presents the findings in an easy-to-understand format within the merge request or pipeline interface. Each vulnerability is assigned a severity level, helping developers prioritize which issues to address first.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Actionable Insights:<\/b><span style=\"font-weight: 400;\"> Developers are provided with information about the specific vulnerability, including a description, impact, and recommended mitigation steps.<\/span><\/li>\n<\/ol>\n<h2><b>Supported Languages in GitLab dependency scanning :<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">It supports the following languages<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">.NET<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">C#<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">C<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">C++<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">GO<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">JAVA &amp; KOTLIN<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">JAVASCRIPT &amp; TYPESCRIPT<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">PHP<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">PYTHON<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">RUBY\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">SCALA<\/span><\/li>\n<\/ul>\n<h2><b>Requirements<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Dependency Scanning runs in the <\/span><span style=\"font-weight: 400;\">test<\/span><span style=\"font-weight: 400;\"> stage, which is available by default. If you redefine the stages in the <\/span><span style=\"font-weight: 400;\">.gitlab-ci.yml<\/span><span style=\"font-weight: 400;\"> file, the <\/span><b>test<\/b><span style=\"font-weight: 400;\"> stage is required.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To run dependency scanning jobs, by default, you need GitLab Runner with the <\/span><a href=\"https:\/\/docs.gitlab.com\/runner\/executors\/docker.html\"><span style=\"font-weight: 400;\">docker<\/span><\/a><span style=\"font-weight: 400;\"> or <\/span><a href=\"https:\/\/docs.gitlab.com\/runner\/install\/kubernetes.html\"><span style=\"font-weight: 400;\">kubernetes<\/span><\/a><span style=\"font-weight: 400;\"> executor. If you\u2019re using the shared runners on GitLab.com, this is enabled by default. The analyzer images provided are for the Linux\/amd64 architecture.<\/span><\/p>\n<h2><b>Steps to configure dependency scanning in GitLab CI\/CD<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Dependency Scanning automatically detects the languages used in the repository. <\/span><span style=\"font-weight: 400;\">All the analyzers matching the detected languages are run automatically; hence, there is no need to configure analyzers.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. T<\/span><span style=\"font-weight: 400;\">here should be a test stage in the pipeline<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. Add the following to your <\/span><span style=\"font-weight: 400;\">.gitlab-ci.yml<\/span><span style=\"font-weight: 400;\"> file:<\/span><\/p>\n<pre><i><span style=\"font-weight: 400;\">- template: Security\/Dependency-Scanning.gitlab-ci.yml<\/span><\/i><\/pre>\n<p><span style=\"font-weight: 400;\">3. The included template will create a <\/span><span style=\"font-weight: 400;\">dependency_scanning<\/span><span style=\"font-weight: 400;\"> job in your CI\/CD pipeline and scan your project&#8217;s source code for possible vulnerabilities.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4. To override a job definition (for example, to change properties like <\/span><span style=\"font-weight: 400;\">variables<\/span><span style=\"font-weight: 400;\"> or <\/span><span style=\"font-weight: 400;\">dependencies<\/span><span style=\"font-weight: 400;\">), declare a new job with the same name as the one to override. Place this new job after the template inclusion and specify any additional keys under it. For example, this includes a rule that it will run only on the main branch. #<\/span><i><span style=\"font-weight: 400;\">override the dependency scanning job<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">gemnasium-dependency_scanning:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">- if: $CI_COMMIT_BRANCH == \"main\"<\/span><\/pre>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">This will check our repository code for vulnerability and upload <\/span><b>gl-dependency-scanning-report.json<\/b><span style=\"font-weight: 400;\"> file in the artifact<\/span><span style=\"font-weight: 400;\"> section of Gitlab CI\/CD.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">After finding vulnerabilities in the <\/span><span style=\"font-weight: 400;\">gl-dependency-scanning-report.json file, developers<\/span><span style=\"font-weight: 400;\"> have to fix it and rerun the pipeline to check again.<\/span><\/li>\n<\/ol>\n<h2><b>Debugging<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">The dependency scanning job fails in a project containing a valid requirements.txt file (python project) but doesn&#8217;t produce any useful output for why the job failed, simply &#8220;exit status 1&#8221;.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Try installing the required package in before_script of gemnasium-python-dependency_scanning<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">gemnasium-python-dependency_scanning:<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0image: registry.gitlab.com\/gitlab-org\/security-products\/analyzers\/gemnasium-python:2-python-3.9<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0before_script:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0- apt-get -qqy update &amp;&amp; apt-get install -qqy libpq-dev python-dev<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2><b>Conclusion<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">GitLab&#8217;s Dependency Scanning is an important tool for software development, it helps in the identification and mitigation of security vulnerabilities introduced by third-party dependencies. By automating the scanning process and providing actionable insights, GitLab empowers developers to proactively address potential security risks, ensuring that code security remains a top priority throughout the development lifecycle. Embracing Dependency Scanning as part of your DevOps strategy can lead to more secure and robust software applications that stand up to the ever-evolving threat landscape.<\/span><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Introduction GitLab is a web-based platform that provides tools for version control and source code management. It allows software developers to coordinate projects, track code changes, and manage the entire development lifecycle. GitLab offers features such as code repositories, issue tracking, continuous integration and continuous deployment (CI\/CD) pipelines, code reviews, wikis, and more. It is [&hellip;]<\/p>\n","protected":false},"author":1617,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":177},"categories":[2348],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58585"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1617"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=58585"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58585\/revisions"}],"predecessor-version":[{"id":59094,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58585\/revisions\/59094"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=58585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=58585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=58585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}