<?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: Josh Hemphill</title>
    <description>The latest articles on DEV Community by Josh Hemphill (@josh_hemphill).</description>
    <link>https://dev.to/josh_hemphill</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%2F337180%2F6af35969-1f10-4f62-817a-ba204a8a4873.jpeg</url>
      <title>DEV Community: Josh Hemphill</title>
      <link>https://dev.to/josh_hemphill</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/josh_hemphill"/>
    <language>en</language>
    <item>
      <title>Brief Intro to Security-First Development</title>
      <dc:creator>Josh Hemphill</dc:creator>
      <pubDate>Tue, 03 Nov 2020 03:05:12 +0000</pubDate>
      <link>https://dev.to/josh_hemphill/brief-intro-to-security-first-development-3mbb</link>
      <guid>https://dev.to/josh_hemphill/brief-intro-to-security-first-development-3mbb</guid>
      <description>&lt;p&gt;Some good ways you can remain security conscious while you code.&lt;/p&gt;

&lt;p&gt;In fast paced software development, we are often given design choices that tempt us to write insecure shortcuts to our desired functionality. I'd like to take you through some simple ways we might keep security on our minds and mitigate the danger of hastily written code.&lt;/p&gt;



&lt;h4&gt;
  
  
  Table of Contents
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
Rules of thumb

&lt;ul&gt;
&lt;li&gt;Use a Proxy&lt;/li&gt;
&lt;li&gt;Don't give something for nothing&lt;/li&gt;
&lt;li&gt;Separate by request size&lt;/li&gt;
&lt;li&gt;Careful what you duplicate&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Buffers&lt;/li&gt;
&lt;li&gt;Validations/Typing&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  Rules of thumb
&lt;/h2&gt;

&lt;p&gt;In javascript land, some good rules of thumb to help you avoid security footguns that consequently also hurt performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use a proxy
&lt;/h3&gt;

&lt;p&gt;As we'll mention later, use a reverse proxy (like &lt;a href="https://nginx.org/en/"&gt;Nginx&lt;/a&gt;) and put a cap on the maximum size of a request that can be forwarded to various parts of your app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Don't give something for nothing
&lt;/h3&gt;

&lt;p&gt;For &lt;strong&gt;unauthenticated APIs&lt;/strong&gt;, don't return a lot more data than you receive, this can be abused by attackers faking an IP address, letting them abuse your server to amplify their &lt;a href="https://www.cloudflare.com/learning/ddos/what-is-a-ddos-attack/"&gt;DDOS&lt;/a&gt; attack on someone else's servers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Separate by request size
&lt;/h3&gt;

&lt;p&gt;Similarly, separate the parts of your app that do need to allow large chunks of data in the requests and limit large or resource-intensive requests as much as possible to users that are authenticated or whom have proven some other sort of trustworthiness.&lt;/p&gt;

&lt;h3&gt;
  
  
  Careful what you duplicate
&lt;/h3&gt;

&lt;p&gt;Avoid duplicating data: things like &lt;code&gt;JSON.parse(JSON.stringify(value))&lt;/code&gt; deeply copy entire objects, and could result in high memory usage if you blindly use it on the data of a request. Instead, as much as you can, work with references to the data you already have, only copy when you need to do final validation before sending to a database; in javascript for example, assigning one variable containing a primitive value to another, copies it; so if &lt;code&gt;x = {y:1}&lt;/code&gt; and you need to pass &lt;code&gt;y&lt;/code&gt; to a function, if you pass &lt;code&gt;fn(x.y)&lt;/code&gt; then &lt;code&gt;1&lt;/code&gt; will be duplicated for &lt;code&gt;fn&lt;/code&gt;; if you can, write &lt;code&gt;fn&lt;/code&gt; to take the object and reference its y value directly. (this makes more sense for larger string values and such)&lt;/p&gt;

&lt;h2&gt;
  
  
  Concepts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Buffers
&lt;/h3&gt;

&lt;p&gt;While things like &lt;a href="https://owasp.org/www-community/vulnerabilities/Buffer_Overflow"&gt;buffer overflows&lt;/a&gt; don't pop up as much in battle-tested, high-level languages like Javascript anymore, buffers created as a result of calling simpler abstracted functions for processing requests can have an impact on both security and performance.&lt;/p&gt;

&lt;p&gt;For example, if you don't use a reverse proxy to limit the size of the request that the computer will accept, or you have a Server Side Rendered (SSR) application that continues to tally or record information about the user, your computer will have to allocate as much memory/space as possible for each request or user since it doesn't know ahead of time how big a buffer to create; it makes it much easier/faster to grow the size of your alloted memory/space until you run out and your app crashes or errors out and has to completely reload itself to fix the issue. While the performance implication (and UX failure for an SSR app) may be obvious, remembering that performance and security issues are often inseparable is helpful for catching smaller bits of logic in your app where a single request can either pass a large amount of data without restriction, or where your app may unnecessarily produce a lot of data from a small request, consuming memory/space that could be serving other users and giving malicious persons an easy way to &lt;a href="https://owasp.org/www-community/attacks/Denial_of_Service"&gt;DOS&lt;/a&gt; your app.&lt;/p&gt;

&lt;p&gt;Basically this boils down to paying attention to the space and resources something takes up, for both the sake of performance and security.&lt;/p&gt;

&lt;h3&gt;
  
  
  Validations/Typing
&lt;/h3&gt;

&lt;p&gt;Especially with the popularity of TypeScript, other &lt;a href="https://hackernoon.com/statically-typed-vs-dynamically-typed-languages-e4778e1ca55"&gt;statically typed languages&lt;/a&gt;, extensive &lt;a href="https://softwaretestingfundamentals.com/unit-testing/"&gt;unit&lt;/a&gt; / &lt;a href="https://www.katalon.com/resources-center/blog/end-to-end-e2e-testing/"&gt;E2E testing&lt;/a&gt;, and &lt;a href="https://www.npmjs.com/search?q=schema%20validation"&gt;schema validation libraries&lt;/a&gt; it's never been easier to apply strict rules for the properties you send and receive. I used to write small apis where the extent of the checking was &lt;code&gt;if(!request.value) throw Error('missing value');&lt;/code&gt;, take it from me, it's a lot easier to start right off the bat with a validation library than to shoehorn proper validation into your api later.&lt;/p&gt;

&lt;p&gt;Even using Typescript or a &lt;a href="https://hackernoon.com/statically-typed-vs-dynamically-typed-languages-e4778e1ca55"&gt;statically typed language&lt;/a&gt;, it's important to validate the initial inputs, and using something like a validation library means you can get predictable and consistent outputs, the most critical part of this is that you can ensure that things like errors only get logged or returned to what they're supposed to. It's a common vulnerability for things like your application data that you're comparing against to accidentally be returned to the client, or for &lt;a href="https://csrc.nist.gov/glossary/term/PII"&gt;Personally Identifiable Information (PII)&lt;/a&gt; or app secrets to be accidentally logged to insecure logging targets where the data could be compromised; many times these vulnerabilities come from previously unknown and unexpected errors that erupt with properties that your own validation or logger isn't expecting.&lt;/p&gt;

&lt;p&gt;Another benefit of the consistent outputs that come with using a validation library, is it's much easier and safer to send responses with helpful errors without revealing too much about the internal state of your application; which will make your front end developers very happy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Additional resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://cheatsheetseries.owasp.org/"&gt;OWASP Best Practices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.ietf.org/topics/security/"&gt;IETF Guidelines&lt;/a&gt; and &lt;a href="https://tools.ietf.org/html/rfc1818"&gt;Best Current Practices&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;For US Gov compliance:

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.nist.gov/cyberframework"&gt;NIST&lt;/a&gt;/&lt;a href="https://www.nist.gov/itl/current-fips"&gt;FIPS&lt;/a&gt;(&lt;a href="https://www.totem.tech/introduction-to-dfars-cybersecurity-requirements/"&gt;plus DFARS for DoD contractors&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.itgovernanceusa.com/federal-cybersecurity-and-privacy-laws#:~:text=Unlike%20the%20European%20Union%2C%20the,to%20data%20breach%20notification%20laws."&gt;ITGovernanceUSA.com&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h4&gt;
  
  
  Check out my stuff elsewhere
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;I just published the &lt;a href="https://github.com/goldbergyoni/nodebestpractices/blob/master/sections/security/userpasswords.md"&gt;Best Practice for User Password Security&lt;/a&gt; to the &lt;a href="https://github.com/goldbergyoni/nodebestpractices"&gt;Node.js Best Practices&lt;/a&gt;&lt;/strong&gt; with lots of helpful explanations about why we use certain algorithms, salting, and pre-hashing.&lt;/p&gt;

</description>
      <category>security</category>
      <category>development</category>
      <category>bestpractice</category>
    </item>
  </channel>
</rss>
