<?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>DEV Community: Shreyash Mishra</title>
    <description>The latest articles on DEV Community by Shreyash Mishra (@shreyash_jhon_doe).</description>
    <link>https://dev.to/shreyash_jhon_doe</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%2F3251256%2F038dcbea-bc87-4bf3-b3cd-471a370cb768.jpg</url>
      <title>DEV Community: Shreyash Mishra</title>
      <link>https://dev.to/shreyash_jhon_doe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shreyash_jhon_doe"/>
    <language>en</language>
    <item>
      <title>Thought I was clever with an inline SVG icon in my email template. Looked 🔥 in my editor, but in the inbox? Total flop. Email clients nuked the SVG—not just a glitch, but a dodge against XS Cue my deep dive into web security nightmares—XSS, CSRF, DDOS</title>
      <dc:creator>Shreyash Mishra</dc:creator>
      <pubDate>Sun, 13 Jul 2025 14:40:34 +0000</pubDate>
      <link>https://dev.to/shreyash_jhon_doe/thought-i-was-clever-with-an-inline-svg-icon-in-my-email-template-looked-in-my-editor-but-in-2np8</link>
      <guid>https://dev.to/shreyash_jhon_doe/thought-i-was-clever-with-an-inline-svg-icon-in-my-email-template-looked-in-my-editor-but-in-2np8</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/shreyash_jhon_doe/app-security-common-attacks-how-to-prevent-them-20fb" class="crayons-story__hidden-navigation-link"&gt;App Security: Common Attacks &amp;amp; How to Prevent Them&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/shreyash_jhon_doe" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3251256%2F038dcbea-bc87-4bf3-b3cd-471a370cb768.jpg" alt="shreyash_jhon_doe profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/shreyash_jhon_doe" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Shreyash Mishra
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Shreyash Mishra
                
              
              &lt;div id="story-author-preview-content-2683761" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/shreyash_jhon_doe" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3251256%2F038dcbea-bc87-4bf3-b3cd-471a370cb768.jpg" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Shreyash Mishra&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/shreyash_jhon_doe/app-security-common-attacks-how-to-prevent-them-20fb" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Jul 13 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/shreyash_jhon_doe/app-security-common-attacks-how-to-prevent-them-20fb" id="article-link-2683761"&gt;
          App Security: Common Attacks &amp;amp; How to Prevent Them
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/programming"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;programming&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/python"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;python&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/javascript"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;javascript&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
            &lt;a href="https://dev.to/shreyash_jhon_doe/app-security-common-attacks-how-to-prevent-them-20fb#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              &lt;span class="hidden s:inline"&gt;Add Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            6 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>programming</category>
      <category>python</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>App Security: Common Attacks &amp; How to Prevent Them</title>
      <dc:creator>Shreyash Mishra</dc:creator>
      <pubDate>Sun, 13 Jul 2025 14:37:27 +0000</pubDate>
      <link>https://dev.to/shreyash_jhon_doe/app-security-common-attacks-how-to-prevent-them-20fb</link>
      <guid>https://dev.to/shreyash_jhon_doe/app-security-common-attacks-how-to-prevent-them-20fb</guid>
      <description>&lt;p&gt;Web applications are everywhere, from personal blogs to massive enterprise platforms, and they’re all potential targets for attackers. Securing them isn’t just a nice-to-have—it’s critical. Whether you’re a frontend developer, backend engineer, or full-stack pro, understanding the most common attacks and how to prevent them is essential to building robust apps. This guide breaks down the major threats, explains how they work, and shares practical ways to protect your applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Distributed Denial of Service (DDoS)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
A DDoS attack overwhelms your server with a flood of traffic, making it impossible for legitimate users to access your app. Think of it like a traffic jam that clogs up your server’s ability to function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use a Web Application Firewall (WAF), such as Cloudflare or AWS Shield, to filter malicious traffic.&lt;/li&gt;
&lt;li&gt;Set up rate limiting and IP blacklisting to control excessive requests.&lt;/li&gt;
&lt;li&gt;Enable auto-scaling and redundancy in your infrastructure to handle sudden spikes.&lt;/li&gt;
&lt;li&gt;Monitor traffic patterns with tools like Grafana, Prometheus, or Datadog to spot attacks early.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Relying solely on your server’s resources without rate limiting, which leaves you vulnerable to brute-force attacks.&lt;/li&gt;
&lt;li&gt;Not using a Content Delivery Network (CDN), which can expose your origin server to direct attacks.&lt;/li&gt;
&lt;li&gt;Failing to monitor traffic, delaying your ability to detect and respond to an attack.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Cross-Site Scripting (XSS)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
XSS lets attackers inject malicious JavaScript into your web pages, which then runs in the browsers of unsuspecting users. It’s like planting a hidden trap that activates when someone visits your site.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stored XSS: The malicious script is saved on your server (e.g., in a database) and served to users.&lt;/li&gt;
&lt;li&gt;Reflected XSS: The script is embedded in a URL or form input and reflected back in the response.&lt;/li&gt;
&lt;li&gt;DOM-based XSS: The attack happens entirely in the browser via client-side scripts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Escape all HTML, JavaScript, and URL outputs to neutralize harmful code.&lt;/li&gt;
&lt;li&gt;Sanitize user input using libraries like DOMPurify.&lt;/li&gt;
&lt;li&gt;Implement Content Security Policy (CSP) headers to restrict script sources.&lt;/li&gt;
&lt;li&gt;Avoid using innerHTML—
use textContent or safe templating engines instead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inserting user input directly into HTML without sanitizing it.&lt;/li&gt;
&lt;li&gt;Using risky JavaScript functions like &lt;code&gt;eval()&lt;/code&gt;, &lt;code&gt;document.write()&lt;/code&gt;, or innerHTML.&lt;/li&gt;
&lt;li&gt;Disabling browser XSS protections during development and forgetting to re-enable them.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Cross-Site Request Forgery (CSRF)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
CSRF tricks a logged-in user’s browser into sending unauthorized requests to a site where they’re authenticated. For example, an attacker might get a user to unknowingly transfer money or change their password.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Include unique CSRF tokens in forms to verify legitimate requests.&lt;/li&gt;
&lt;li&gt;Set cookies to &lt;code&gt;SameSite=Strict&lt;/code&gt; or Lax to limit cross-site misuse.&lt;/li&gt;
&lt;li&gt;Use double-submit cookies for an extra layer of protection.&lt;/li&gt;
&lt;li&gt;Never use &lt;code&gt;GET&lt;/code&gt; requests for sensitive actions—stick to &lt;code&gt;POST&lt;/code&gt; or other methods.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Skipping CSRF protection for internal APIs or admin routes, assuming they’re safe.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;SameSite=None&lt;/code&gt; without HTTPS, which can expose cookies to attacks.&lt;/li&gt;
&lt;li&gt;Relying only on cookies for security without validating tokens.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  SQL Injection (SQLi)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
SQL injection happens when attackers sneak malicious SQL code into unsanitized inputs, letting them read, modify, or delete database data. It’s like giving an attacker the keys to your database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always use prepared statements or parameterized queries to safely handle inputs.&lt;/li&gt;
&lt;li&gt;Never concatenate user input directly into SQL queries.&lt;/li&gt;
&lt;li&gt;Validate and sanitize all user inputs, even for simple fields.&lt;/li&gt;
&lt;li&gt;Use ORM frameworks like Django ORM or SQLAlchemy, which provide built-in protections.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Building SQL queries by stitching together user input (ex &lt;code&gt;"SELECT * FROM users WHERE name='" + input + "'"&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Not validating numeric or enum inputs, which can allow attackers to bypass logic.&lt;/li&gt;
&lt;li&gt;Assuming ORMs are bulletproof—misusing raw query functions can still expose vulnerabilities
.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  CORS Misconfiguration
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
Cross-Origin Resource Sharing (CORS) misconfigurations let malicious sites access sensitive data from your API by exploiting improper access controls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; to specific, trusted origins instead of a wildcard &lt;code&gt;(*)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Avoid enabling credentials &lt;code&gt;(Access-Control-Allow-Credentials: true)&lt;/code&gt;
unless necessary.&lt;/li&gt;
&lt;li&gt;Test and validate CORS policies to ensure they’re not overly permissive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allowing CORS globally with &lt;code&gt;Access-Control-Allow-Origin: *&lt;/code&gt;, even for sensitive endpoints.&lt;/li&gt;
&lt;li&gt;Enabling credentials with wildcard origins, which browsers may block but attackers can exploit.&lt;/li&gt;
&lt;li&gt;Misunderstanding how preflight requests work, leading to broken or insecure rules.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Server-Side Request Forgery (SSRF)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
SSRF tricks your server into making unauthorized requests, often to internal systems that should be off-limits, like metadata services or internal APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid letting user input dictate URLs unless absolutely necessary.&lt;/li&gt;
&lt;li&gt;Validate and sanitize any URLs your app processes.&lt;/li&gt;
&lt;li&gt;Block internal IP ranges (ex 169.254.169.254) in your firewall.&lt;/li&gt;
&lt;li&gt;Use metadata filtering to prevent access to sensitive internal endpoints.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fetching user-provided URLs without validation, opening the door to internal system access.&lt;/li&gt;
&lt;li&gt;Allowing unrestricted file downloads or previews from arbitrary URLs.&lt;/li&gt;
&lt;li&gt;Using third-party tools or libraries that make internal calls without proper safeguards.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Insecure Deserialization
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
Attackers exploit serialized objects to execute malicious code when your server deserializes them, potentially taking over your system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don’t accept serialized objects from untrusted sources.&lt;/li&gt;
&lt;li&gt;Use JSON instead of native serialization formats when possible.&lt;/li&gt;
&lt;li&gt;Validate the structure of objects before deserializing them.&lt;/li&gt;
&lt;li&gt;Add digital signature verification to ensure data integrity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Trusting serialized data from clients, like cookies or tokens in binary formats.&lt;/li&gt;
&lt;li&gt;Using outdated or vulnerable serialization libraries.&lt;/li&gt;
&lt;li&gt;Not validating deserialized objects, which can allow arbitrary code execution.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Clickjacking
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
Clickjacking tricks users into clicking hidden elements by embedding your site in an invisible &lt;code&gt;iframe&lt;/code&gt; on a malicious page. It’s like setting a trap that looks like a legitimate button.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set &lt;code&gt;X-Frame-Options: DENY&lt;/code&gt; or &lt;code&gt;SAMEORIGIN&lt;/code&gt; to block iframe embedding.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;Content-Security-Policy: frame-ancestors 'none'&lt;/code&gt; for added protection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not setting frame-related headers, leaving your site open to embedding.&lt;/li&gt;
&lt;li&gt;Allowing iframes on sensitive pages like login or payment forms.&lt;/li&gt;
&lt;li&gt;Forgetting to test how your UI behaves when embedded elsewhere.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Man-in-the-Middle (MITM)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
An attacker intercepts communication between your users and your server, eavesdropping or altering data in transit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enforce HTTPS with HTTP Strict Transport Security (HSTS) headers.&lt;/li&gt;
&lt;li&gt;Use strong TLS encryption for all connections.&lt;/li&gt;
&lt;li&gt;Validate SSL certificates and avoid self-signed ones in production.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Supporting both HTTP and HTTPS, which can allow attackers to downgrade connections.&lt;/li&gt;
&lt;li&gt;Skipping HSTS headers, making SSL stripping attacks easier.&lt;/li&gt;
&lt;li&gt;Using weak or self-signed certificates, which attackers can exploit.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Broken Authentication
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
Flaws in login or session management let attackers hijack user accounts, often by exploiting weak credentials or session handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enforce strong password policies and encourage complexity.&lt;/li&gt;
&lt;li&gt;Implement Multi-Factor Authentication (MFA) wherever possible.&lt;/li&gt;
&lt;li&gt;Regenerate session tokens after login to prevent session fixation.&lt;/li&gt;
&lt;li&gt;Use secure, HttpOnly, and SameSite cookies for session management.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Storing passwords in plaintext instead of hashing them with strong algorithms like bcrypt or scrypt.&lt;/li&gt;
&lt;li&gt;Not invalidating sessions after logout or password changes.&lt;/li&gt;
&lt;li&gt;Using predictable or short session tokens that are easy to guess.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Security Misconfiguration
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
Default settings, open ports, or overly verbose error messages can expose sensitive information or leave your app vulnerable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Disable unused services and debug logs in production.&lt;/li&gt;
&lt;li&gt;Follow the principle of least privilege for all accounts and services.&lt;/li&gt;
&lt;li&gt;Keep your server, database, and dependencies updated.&lt;/li&gt;
&lt;li&gt;Use automated scanners like OWASP ZAP to catch misconfigurations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Leaving debug mode enabled in production environments.&lt;/li&gt;
&lt;li&gt;Exposing stack traces or file paths in error messages.&lt;/li&gt;
&lt;li&gt;Running apps with unnecessary root or admin privileges.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  API Abuse &amp;amp; Excessive Data Exposure
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Is&lt;/strong&gt;&lt;br&gt;
Unsecured APIs can leak sensitive data or allow attackers to overload your system with excessive requests, leading to breaches or slowdowns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Prevent It&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use API gateways to enforce throttling and rate-limiting.&lt;/li&gt;
&lt;li&gt;Return only the data fields needed for each request.&lt;/li&gt;
&lt;li&gt;Implement object-level and field-level access controls.&lt;/li&gt;
&lt;li&gt;Log and monitor API usage to detect suspicious activity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Common Mistakes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Returning full data objects, including sensitive fields like &lt;code&gt;is_admin&lt;/code&gt; or &lt;code&gt;password_hash&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Exposing internal API endpoints without proper authentication.&lt;/li&gt;
&lt;li&gt;Not rate-limiting APIs, allowing bots or scripts to abuse them.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Bonus: Secure Development Practices
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;HTTP Security Headers&lt;/strong&gt;&lt;br&gt;
Add these headers to your server configuration for an extra layer of protection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`Strict-Transport-Security: max-age=63072000; includeSubDomains
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Referrer-Policy: no-referrer
Content-Security-Policy: default-src 'self'`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Tools to Use&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Static Analysis: Tools like &lt;a href="https://github.com/PyCQA/bandit" rel="noopener noreferrer"&gt;Bandit&lt;/a&gt; or &lt;a href="https://docs.sonarsource.com/sonarqube-server/latest/?_gl=1*1r8jfhz*_gcl_au*MTA2NTA4NDY0OS4xNzUyNDE2NjE2*_ga*OTc4NTQ2Mjk3LjE3NTI0MTY2MTY.*_ga_9JZ0GZ5TC6*czE3NTI0MTY2MTUkbzEkZzEkdDE3NTI0MTY2MTYkajU5JGwwJGgw" rel="noopener noreferrer"&gt;SonarQube&lt;/a&gt; to catch code-level issues.&lt;/li&gt;
&lt;li&gt;Dependency Scanning: Run &lt;code&gt;npm audit&lt;/code&gt; or &lt;code&gt;pip-audit&lt;/code&gt; to check for vulnerable packages.&lt;/li&gt;
&lt;li&gt;Dynamic Testing: Use OWASP ZAP or Burp Suite to test running applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Development Tips&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Never expose sensitive files like .env, .git, or backups.&lt;/li&gt;
&lt;li&gt;Integrate linting and static checks into your CI pipeline.&lt;/li&gt;
&lt;li&gt;Rotate keys, tokens, and secrets regularly to minimize risks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Building secure web applications isn’t a one-time task—it’s an ongoing commitment. Attackers only need one weak spot to cause havoc, so you have to stay vigilant and cover all bases. By following these best practices and weaving security into every stage of development, you can protect your users and your app. Stay proactive, keep learning, and code with confidence!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>python</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Writing Scalable &amp; Maintainable Unit Tests in Django — A Practical Guide with Real Examples</title>
      <dc:creator>Shreyash Mishra</dc:creator>
      <pubDate>Sat, 07 Jun 2025 14:52:36 +0000</pubDate>
      <link>https://dev.to/shreyash_jhon_doe/writing-scalable-maintainable-unit-tests-in-django-a-practical-guide-with-real-examples-47a4</link>
      <guid>https://dev.to/shreyash_jhon_doe/writing-scalable-maintainable-unit-tests-in-django-a-practical-guide-with-real-examples-47a4</guid>
      <description>&lt;p&gt;When building production-ready Django applications, writing robust unit tests is non-negotiable. A well-structured unit testing strategy ensures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Changes don’t break existing functionality.&lt;/li&gt;
&lt;li&gt;Business logic works as expected.&lt;/li&gt;
&lt;li&gt;You have confidence in refactoring.
This guide walks through how to write scalable unit tests using a structured and reusable pattern—complete with mock data, a shared test base, map factories, and advanced mocking techniques like MagicMock and @patch.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Project Test Structure Overview
&lt;/h2&gt;

&lt;p&gt;Let’s use a modular and DRY (Don’t Repeat Yourself) structure for our Django test suite:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;your_project/&lt;br&gt;
├── base_test/&lt;br&gt;
│   ├── base_map_factory.py&lt;br&gt;
│   ├── constant_model_map.py&lt;br&gt;
│   └── base_test_case.py&lt;br&gt;
├── your_app/&lt;br&gt;
│   ├── tests/&lt;br&gt;
│   │   ├── maps/&lt;br&gt;
│   │   │   └── merchant_map.py&lt;br&gt;
│   │   └── test_send_key_salt.py&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  🔧 1. Base Map Factory — Reusable Test Data Provider
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;📁 base_test/base_map_factory.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

class BaseMapFactory:
    def __init__(self, map=None):
        self.map = map or {}
    def get_map(self, key=None, updates={}):
        try:
            data = self.map
            for k in key:
                data = copy.deepcopy(data[k])
            if updates:
                if isinstance(data, list):
                    for item in data:
                        item.update(updates)
                elif isinstance(data, dict):
                    data.update(updates)
            return data
        except (KeyError, TypeError) as e:
            print("Error fetching map:", e)
            return None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🔍 Why This?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Encapsulates test data in an extendable pattern.&lt;/li&gt;
&lt;li&gt;Prevents mutation of original data.&lt;/li&gt;
&lt;li&gt;Enables easy overrides using updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Constant Test Data for DB Models
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;📁 base_test/constant_model_map.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from base_test.base_map_factory import BaseMapFactory

class ConstantModelMap(BaseMapFactory):
    def __init__(self):
        self.map = {
            "merchant_credentials": {
                "id": 1,
                "merchant_id": "EXAMPLE123",
                "api_key": "secureapikey",
                "callback_url": "https://callback.test.com"
            },
            "merchant_info": {
                "merchant_id": 123,
                "merchant_name": "Test Merchant",
                "email": "merchant@example.com"
            }
        }
        super().__init__(self.map)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🔍 Why This?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maintains a single source of truth for model test data.&lt;/li&gt;
&lt;li&gt;Easy to maintain and change without digging into tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🧪 3. Shared Base Test Case
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;📁 base_test/base_test_case.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.test import TestCase, Client
from base_test.constant_model_map import ConstantModelMap
from your_app.models import Merchant, MerchantCredentials

class BaseDjangoTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.client = Client()
        cls.models_map = ConstantModelMap()

        cls.merchant = Merchant.objects.create(
            **cls.models_map.get_map(["merchant_info"])
        )
        cls.credentials = MerchantCredentials.objects.create(
            **cls.models_map.get_map(["merchant_credentials"])
        )

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🔍 Why This?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Promotes code reuse across test files.&lt;/li&gt;
&lt;li&gt;Sets up test models in a shared, structured way.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  💼 4. Map for Service/API-Specific Static Data
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;📁 your_app/tests/maps/merchant_map.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from base_test.base_map_factory import BaseMapFactory

class MerchantTestMap(BaseMapFactory):
    def __init__(self):
        self.map = {
            "common_request_data": {
                "merchant_id": 123,
                "key": "value"
            },
            "successful_response": {
                "status": "success",
                "data": {"message": "Key sent"}
            },
            "error_response": {
                "status": "error",
                "data": {"message": "Invalid merchant_id"}
            }
        }
        super().__init__(self.map)

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  🚀 5. Test File — Using All the Building Blocks
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;📁 your_app/tests/test_send_key_salt.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.urls import reverse
from unittest.mock import patch, MagicMock

from base_test.base_test_case import BaseDjangoTestCase
from your_app.tests.maps.merchant_map import MerchantTestMap
from shared.utility.loggers.logging import AppLogger
from shared.utility.push import Push

class SendKeySaltTests(BaseDjangoTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.endpoint = reverse("send-key-salt")
        cls.map = MerchantTestMap()
        cls.error_prefix = ":: SendKeySaltTests :: "

    @patch.object(AppLogger, "info")
    def test_successful_key_sending(self, mock_info):
        response = self.client.post(self.endpoint, self.map.get_map(["common_request_data"]))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), self.map.get_map(["successful_response"]))
        self.assertTrue(mock_info.called)

    @patch.object(AppLogger, "info")
    @patch.object(AppLogger, "exception")
    def test_missing_merchant_id(self, mock_exception, mock_info):
        bad_data = self.map.get_map(["common_request_data"], updates={"merchant_id": ""})
        response = self.client.post(self.endpoint, bad_data)
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json(), self.map.get_map(["error_response"]))
        self.assertTrue(mock_exception.called)

    @patch.object(Push, "push_mail")
    def test_push_notification_mock(self, mock_push):
        mock_push.return_value = True
        response = self.client.post(self.endpoint, self.map.get_map(["common_request_data"]))
        self.assertEqual(response.status_code, 200)
        mock_push.assert_called_once()

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  📚 Concepts Explained
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;🔧 MagicMock and @patch&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;@patch.object(SomeClass, "method") dynamically replaces a method for the duration of a test.&lt;/li&gt;
&lt;li&gt;MagicMock is used to create dummy objects or simulate return values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use case:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@patch.object(Logger, "info")
def test_logs_info(self, mock_info):
    call_my_view()
    mock_info.assert_called_once()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ This avoids real logging and isolates the unit of work.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Quick Summary About Packages And Concept
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;✅ Django Test Framework (django.test)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;django.test.TestCase: Django’s built-in test class (inherits from Python’s unittest.TestCase).&lt;/li&gt;
&lt;li&gt;client = Client(): Simulates HTTP requests for views (like POST, GET, PUT).&lt;/li&gt;
&lt;li&gt;Used for integration-style tests that hit the full Django stack including URLs, middleware, views, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;✅ unittest.mock&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;patch: A decorator/context manager to replace objects with mocks during tests.&lt;/li&gt;
&lt;li&gt;MagicMock: A flexible mock object that simulates return values, method calls, etc.&lt;/li&gt;
&lt;li&gt;Why use?&lt;/li&gt;
&lt;li&gt;Prevent hitting external APIs, file systems, DBs, or logs.&lt;/li&gt;
&lt;li&gt;Assert if external services like Push.push_mail() or PGLogger.info() were called with correct data.
Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@patch.object(PGLogger, "info")
def test_logging(self, mock_log):
view()
assert mock_log.called
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;✅ openpyxl&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used to create in-memory Excel files in tests for upload scenarios.&lt;/li&gt;
&lt;li&gt;Workbook(), ws.append(...): Used to mock file content for forms, upload testing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;✅ django.urls.reverse&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dynamically builds URLs from view names.&lt;/li&gt;
&lt;li&gt;Helps you avoid hardcoding endpoint paths, improving test portability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;✅ django.core.files.uploadedfile.SimpleUploadedFile&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used to create mock files in memory (text, Excel, PDF).&lt;/li&gt;
&lt;li&gt;Useful for testing file upload views.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;✅ copy.deepcopy&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used in BaseMapFactory to return cloned test data and prevent mutation of original test dictionaries.&lt;/li&gt;
&lt;li&gt;Ensures that get_map(..., updates={}) doesn’t affect future test cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  📈 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Investing time in writing clean, isolated, and scalable unit tests pays off enormously in the long run. With a base test case, reusable factory maps, and clever mocking, your Django tests can be as maintainable as your production code.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>python</category>
      <category>django</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
