<?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: MxL Devs</title>
    <description>The latest articles on DEV Community by MxL Devs (@mxldevs).</description>
    <link>https://dev.to/mxldevs</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%2F423951%2F67195488-985f-4edc-b337-d8de62c23b10.png</url>
      <title>DEV Community: MxL Devs</title>
      <link>https://dev.to/mxldevs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mxldevs"/>
    <language>en</language>
    <item>
      <title>Should You Use Password Authentication?</title>
      <dc:creator>MxL Devs</dc:creator>
      <pubDate>Mon, 13 Jul 2020 23:41:09 +0000</pubDate>
      <link>https://dev.to/mxldevs/should-you-use-password-authentication-h46</link>
      <guid>https://dev.to/mxldevs/should-you-use-password-authentication-h46</guid>
      <description>&lt;p&gt;Today I wanted to sign into a service that I haven't used for awhile, but I forgot my password. So I hit "Forgot Password?", entered my username, and then it sent a password reset link to my email. Fortunately I still remember the password to my email.&lt;/p&gt;

&lt;p&gt;I opened up my email, pressed the link, and it directed me to a password reset form where I entered my new password that required at least 8 characters including one uppercase, one lowercase, one numeric, and one special character like &lt;code&gt;$*@&amp;amp;!&lt;/code&gt;,&lt;/p&gt;

&lt;p&gt;Spoiler alert, I'm not going to remember that password next time either. Chrome asked me if I wanted to store the password, and I said no. I'll deal with password reset again next time.&lt;/p&gt;

&lt;p&gt;So this got me thinking about the age-old problem of security: authentication.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Authentication
&lt;/h2&gt;

&lt;p&gt;There's already a lot of material out there. I would recommend reading &lt;a href="https://en.wikipedia.org/wiki/Authentication"&gt;this wikipedia article&lt;/a&gt; about it because it's a fantastic start and goes through all sorts of topics.&lt;/p&gt;

&lt;p&gt;Basically it's to prove you are who you claim to be. If you walked up to the bank and said you're _______ and wanted to withdraw suspicious amounts of cash from your account, what's stopping the bank from just taking your word?&lt;/p&gt;

&lt;p&gt;There are many different strategies involved, and security researchers have come up with three factors that go into the process&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what you know (knowledge)&lt;/li&gt;
&lt;li&gt;what you have (ownership)&lt;/li&gt;
&lt;li&gt;what you are (inherence)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Authentication in Action
&lt;/h2&gt;

&lt;p&gt;So for example today I went to order a &lt;a href="https://www.starbucks.ca/menu/product/425/iced?parent=%2Fdrinks%2Ffrappuccino-blended-beverages%2Fcoffee-frappuccino"&gt;Venti Javachip Frappucino&lt;/a&gt; from Starbucks. No sponsorship, I'm just a fan, even if it's loaded with sugar.&lt;/p&gt;

&lt;p&gt;When it came time to pay, I used my chip-enabled credit card where I entered a PIN to approve the transaction.&lt;/p&gt;

&lt;p&gt;This is a simple example of authentication: I &lt;strong&gt;have&lt;/strong&gt; a credit card that's tied to my bank account and only I should &lt;strong&gt;know&lt;/strong&gt; the PIN code for the card.&lt;/p&gt;

&lt;p&gt;Of course then they introduced tap-to-pay and suddenly that PIN code just goes out the window for most people if they don't disable it.&lt;/p&gt;

&lt;p&gt;Convenience truly is the bane of security.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Authentication Matters
&lt;/h2&gt;

&lt;p&gt;It's pretty simple: we all have things that should be private. A bank account, an email account, your computer, your home even. I still use ancient-tech metal lock-and-key which can be easily picked, while some people use like digital keypads (just another password) or more sophisticated things like finger-print scanners, or voice- and facial-recognition systems. Open sesame?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Passwords?
&lt;/h2&gt;

&lt;p&gt;You'd think with advancement in technology we'd be able to move on from passwords, but probably 99% of the systems out there still use basic password authentication, whether it's a string you type in to unlock your computer, or that lockpad pattern on your phone.&lt;/p&gt;

&lt;p&gt;Personally I don't like passwords at all.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You'll probably write it down somewhere, because who's going to remember all these dumb uppercase lowercase numbers and special characters&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You'll probably re-use them, so if someone gets it somehow, all of your accounts can be compromised&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You'll probably share it with someone else, who you then have to trust to keep it secret&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And especially as a developer, suddenly the stakes are &lt;em&gt;really&lt;/em&gt; high: I just want you to be able to have private access to your account; &lt;strong&gt;I don't want to have to figure out how to keep your secrets from being leaked&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alternative to Passwords
&lt;/h2&gt;

&lt;p&gt;Fortunately, the internet has evolved and there are various tools at our disposal. &lt;a href="https://en.wikipedia.org/wiki/Passwordless_authentication"&gt;Passwordless authentication&lt;/a&gt; had been quite popular and is used in a lot of different services now. SSH keys are a common example that devs probably are familiar with. Then you have fingerprint unlock for your apps which my banking apps offer and are quite convenient.&lt;/p&gt;

&lt;p&gt;But outside the technical stuff, you might have come across some passwordless services yourself already like when you "log in with facebook" or "log in with google" and don't need to remember additional passwords. Basically outsource the security to billion dollar companies that probably have more people making sure those secrets will be kept well.&lt;/p&gt;

&lt;p&gt;There's a library called &lt;a href="http://www.passportjs.org/"&gt;passportJS&lt;/a&gt; for NodeJS that basically let's you implement authentication with about 500 different strategies easily and I'm sure various frameworks also have similar libraries. If not, might be something you can build :)&lt;/p&gt;

&lt;h2&gt;
  
  
  But is Passwordless Good Enough?
&lt;/h2&gt;

&lt;p&gt;It's pretty nice to be able to use your google account to authenticate into dozens of services, but what happens if your google account gets compromised? Now all of your services that use that google account are also compromised.&lt;/p&gt;

&lt;p&gt;However, that's a risk that the end-user takes on, and not something you - the developer or the business owner - needs to necessarily worry about. Even if hackers find exploits in your system and dump your database, the extent of damage is probably going to be limited to whatever information your users give you, which won't include their passwords that they might be re-using in a bunch of other services!&lt;/p&gt;

&lt;p&gt;Personally I would recommend looking into passwordless authentication options and not having to worry about dealing with anyone's passwords or secret questions/answers and so on because it's just less legal stress to worry about and much less technical effort on your part.&lt;/p&gt;

&lt;p&gt;Sure, sometimes it might be convenient for me to create a throwaway user account without having to bind one of my throwaway email addresses to it, but maybe this is also a good thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  But I still want passwords!
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;(added 2020-07-14)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are cases where you may choose to offer the option to allowing users to authenticate with a simple password, for example if they don't have access to alternative authentication methods that you've provided.&lt;/p&gt;

&lt;p&gt;In this case, the user is trusting you with their password, so you'll have to make sure that you keep it secure! It would be good to read about and follow good &lt;a href="https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html"&gt;password storage practices&lt;/a&gt; like hashing and salting the passwords and storing the result in the database. You should never have access to the user's password because that's something you don't, and shouldn't, need to know.&lt;/p&gt;

&lt;h2&gt;
  
  
  Feedback
&lt;/h2&gt;

&lt;p&gt;Let me know your thoughts about user authentication and security. I don't have a security background so my understanding is quite limited to high-level concepts. Maybe passwordless is also not that great? Are there other forms of authentication that you use, or read about, or have thought about?&lt;/p&gt;

</description>
      <category>security</category>
      <category>webdev</category>
      <category>password</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Why Good Code Needs Comments</title>
      <dc:creator>MxL Devs</dc:creator>
      <pubDate>Thu, 09 Jul 2020 02:58:54 +0000</pubDate>
      <link>https://dev.to/mxldevs/why-good-code-needs-comments-1bog</link>
      <guid>https://dev.to/mxldevs/why-good-code-needs-comments-1bog</guid>
      <description>&lt;p&gt;Sometimes I see devs say things like "good code doesn't need comments". The rationale being, if your code is well-written, it should be self-explanatory to the next developer that's looking at the code.&lt;/p&gt;

&lt;p&gt;Actually I used to say that as well, but that was mostly because I was (and maybe still is?) quite a lazy commenter. I thought "how would I not understand my own code?" as long as it's written clearly enough? Don't even get me started on writing design documents or maintaining them, but I'll leave that for another topic.&lt;/p&gt;

&lt;p&gt;But at some point I started to appreciate comments, so I'd like to share my thoughts on it by sharing some experiences I've had while writing code, which might offer some insight into why even the best code might benefit from having comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Story Time
&lt;/h2&gt;

&lt;p&gt;I &lt;a href="https://dev.to/mxldevs/secure-your-data-api-from-web-scrapers-1c5m"&gt;wrote an article previously about web scraping&lt;/a&gt;. If you're interested in scraping or having issues with getting scraped, might be something to read, but it's not related to this story besides that I write various random tools like scrapers and bots.&lt;/p&gt;

&lt;p&gt;In particular, my tools come in different flavours&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;some of them run on a server constantly listening to web hooks&lt;/li&gt;
&lt;li&gt;some of them wake up once every hour or so to poll for data&lt;/li&gt;
&lt;li&gt;some of them fire up a browser to navigate a couple pages&lt;/li&gt;
&lt;li&gt;some of them transform data from one format to another as part of a pipeline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Basically they all do different kinds of things. They're all tools that are written to solve specific problems, and most of these deal with completely different problem domains. Some of these scripts are about 10-20 lines long, others might be 100+.&lt;/p&gt;

&lt;p&gt;I have dozens of these small tools lying around, some of them I've never looked at for years but still in use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Did I Write That Again?
&lt;/h2&gt;

&lt;p&gt;Websites will change. API's will change. Data formats will change. &lt;/p&gt;

&lt;p&gt;Basically, requirements change. It's just how things are and you just have to deal with it when it happens. If you're lucky, things might not change that often, but on the flip-side, because it doesn't change that often, you don't look at your code that often either.&lt;/p&gt;

&lt;p&gt;Eventually you'll start forgetting details, and one of the most frequent problems I've had when one my tools crashes is &lt;strong&gt;I have no idea why my code is doing what it's doing!&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How Self-Explanatory is Good Code?
&lt;/h2&gt;

&lt;p&gt;When I look at my code, I know exactly what it's doing, because I came up with descriptive variable, method, and class names. I have spaces after each comma, and good indentation or curly brace placement to make that code look clean and absolutely &lt;em&gt;fabulous&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Indeed, it should be quite self-explanatory what the code is doing, but there are so many things that self-explanatory code fails to explain&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is this code correct?&lt;/li&gt;
&lt;li&gt;Why is it written this way?&lt;/li&gt;
&lt;li&gt;How should this code be used?&lt;/li&gt;
&lt;li&gt;What problem is the code meant to solve?&lt;/li&gt;
&lt;li&gt;Are there better ways to solve the problem?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, "what is this code doing?" is an important question as well, but that's just one out of many other important questions that I have when I look at a piece of code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Beyond My Code
&lt;/h2&gt;

&lt;p&gt;At some point, I started moving away from only reading and writing my own code. I started using libraries. I started using frameworks. I started code-reviewing. I started extending other developers' code.&lt;/p&gt;

&lt;p&gt;Not only do I need to understand the problem, I also need to understand how you've attempted to solve the problem through your code. I might be able to understand what your code is doing, but then I might have to put it all together to understand why you wrote it that way.&lt;/p&gt;

&lt;p&gt;Most of the time it's not like my 100-liner simple tools that do just one or two jobs, some of these could be massive 100000+ line codebases with hundreds of classes and components written by dozens of developers over the course of many years.&lt;/p&gt;

&lt;p&gt;It really can take a lot of time to truly understand what the code is doing, and not having any comments to help explain the purpose of the code can make it that much harder to put things together before you can actually dig in and write something of your own!&lt;/p&gt;

&lt;h2&gt;
  
  
  We are Problem Solvers
&lt;/h2&gt;

&lt;p&gt;The biggest lesson I've learned in software development is understanding my role as the coder. The code is simply a solution to someone's problem, and there's an entire process that you (or someone else) go through before you arrive at the final solution that will be turned into code and delivered.&lt;/p&gt;

&lt;p&gt;By writing comments to explain the purpose of your code and a bit of the thought-process that goes into it, it will greatly benefit the next person (perhaps yourself even) who looks at your code and wonder why you wrote it and how it should be used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Feedback?
&lt;/h2&gt;

&lt;p&gt;What are your thoughts on comments to explain your code? Are there strategies to good code writing that can allow you to express your thought process without necessarily writing it as comments? I'd love to be able to read a piece of code and understand the big picture behind it since it minimizes the amount of text I'd have to write.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>codequality</category>
      <category>comments</category>
      <category>documentation</category>
    </item>
    <item>
      <title>Secure your Data API from Web Scrapers</title>
      <dc:creator>MxL Devs</dc:creator>
      <pubDate>Sun, 05 Jul 2020 01:35:02 +0000</pubDate>
      <link>https://dev.to/mxldevs/secure-your-data-api-from-web-scrapers-1c5m</link>
      <guid>https://dev.to/mxldevs/secure-your-data-api-from-web-scrapers-1c5m</guid>
      <description>&lt;p&gt;I do a lot of data scraping on the web, and one of the first things I look for is an API. Even if the platform doesn't provide a publicly documented API, generally they will probably have some sort of undocumented "private" API to facilitate client-server communication like search queries or other fun AJAX stuff without reloading the page.&lt;/p&gt;

&lt;p&gt;In fact, because it's undocumented, there may be a lot of security-related issues that they might not think about, simply because it's not intended to be consumed by the public (but probably more likely, who likes to think about security?)&lt;/p&gt;

&lt;p&gt;One thing I've noticed, as a web scraper, is generally how easy it is to make API requests myself.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Simple Example
&lt;/h2&gt;

&lt;p&gt;Let's say you have a blog or other content system and you wanted to implement a search function, where the user enters a search query and then your server uses it to return a list of relevant results from the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Request
&lt;/h2&gt;

&lt;p&gt;After building your API, you might make a request like this on the client-side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST https://api.myblog.com/search

query=scrape&amp;amp;type=tag
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which is intended to return all posts that are tagged with "scrape" in it. And then you proceed to test your API, verify that it works, and commit your code.&lt;/p&gt;

&lt;p&gt;Fantastic, job well done. You release your feature, and now you can tag your posts and let people find them using tag search.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Scraper
&lt;/h2&gt;

&lt;p&gt;So now I come along and I wanted a list of all your posts on your site. Perhaps I want to build my own site that basically mirrors all of your content (think about all those instagram clones) so that I can enjoy some extra traffic without doing any work. All I really have to do is run a scraper periodically checking for new content and then download them to my own server.&lt;/p&gt;

&lt;p&gt;To figure out how your site works, I would come to your blog, type in a search query, and hit submit. I would then notice that you make an API request and then proceed to take a look at how the request and responses are constructed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the headers, like cookies, origin, host, referer, user-agent, custom headers, etc&lt;/li&gt;
&lt;li&gt;the body, to see what data is sent&lt;/li&gt;
&lt;li&gt;any security features like CSRF tokens or authorization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then I would replicate this request and send it from my own server, which bypasses CORS because CORS doesn't mean anything if I can spoof the origin. And also because a lot of people probably don't really understand CORS and set &lt;code&gt;Access-Control-Allow-Origin: *&lt;/code&gt; on their server anyways because &lt;a href="https://stackoverflow.com/search?q=No+%27Access-Control-Allow-Origin%27+header+is+present+on+the+request"&gt;half the answers on StackOverflow&lt;/a&gt; recommend it as a solution. Conceptually, it's not that difficult to understand and I highly recommend reading about it, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"&gt;maybe over at MDN&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some Bad Queries
&lt;/h2&gt;

&lt;p&gt;I would start by trying different things. Maybe try something as simple as an empty string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST https://api.myblog.com/search

query=&amp;amp;type=tag
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Maybe your search engine will match on EVERY tag and then give me everything in one shot.&lt;/p&gt;

&lt;p&gt;Or I might try some wildcards, maybe &lt;code&gt;%&lt;/code&gt; or &lt;code&gt;*&lt;/code&gt; hoping you don't sanitize your parameters (which can potentially open up a different world of hurt!)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST https://api.myblog.com/search

query=%&amp;amp;type=tag
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Solutions???
&lt;/h2&gt;

&lt;p&gt;Generally, depending on what country you're operating in, the law is on your side since your terms of service will include something about unauthorized access to API's. And if you don't, you probably should! But of course not everyone will respect the law, and depending on the kind of data you're working with, sometimes you might want to be a bit more proactive in preventing scrapers from taking all your data too quickly.&lt;/p&gt;

&lt;p&gt;Just like how getting your database hacked and exposing millions of customer data would be a massive blow to your business even if you have the right to sue the hackers, you might not want to wait until it actually happens so that you can play your legal cards.&lt;/p&gt;

&lt;p&gt;Unlike securing a database, you can't just stop people from making requests to your server. After all, how does one distinguish between a request from your website, and a request from a 3rd party client that I wrote in Ruby or Python or Java or straight-up curl?&lt;/p&gt;

&lt;p&gt;I believe the goal in this case is to make scrapers work hard. The harder they have to work, the more requests they need to make, the slower the data collection process becomes, and the easier it is for you to flag it as suspicious activity and then take action automatically.&lt;/p&gt;

&lt;p&gt;Depending on the nature of your content, you might for example &lt;strong&gt;enforce a minimum character limit&lt;/strong&gt; and &lt;strong&gt;sanitize the inputs&lt;/strong&gt; to avoid wild card operations on the &lt;em&gt;server-side&lt;/em&gt;. Where you do your checks is important because I build my own requests, so any front-end validation is basically useless. Relying on the client to be honest is like giving me the key to your safe and hoping that I don't open it.&lt;/p&gt;

&lt;p&gt;Other common examples in other applications I've seen include &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;limiting the number of requests per time interval (ie: request cooldowns)&lt;/strong&gt;. If your app users aren't intended to make 100 requests a second, don't let them. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Paginating your results&lt;/strong&gt;. This is a pretty common strategy for various performance-related purposes (for better or worse), but combining it with request cooldowns, it can be pretty nice.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;geofencing strategies&lt;/strong&gt;, where search results are limited based on a provided location which could be the name of a region, or a pair of latitude, longitude coordinates. Might not apply to you, but if it does, really makes life hard for scrapers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;rate limiting&lt;/strong&gt;, where you impose limits on the number of API requests that can be made before no more requests can be made. This is useful if requests must be authenticated with a token, possibly tied to a user account. This won't be effective if I'm hitting the server directly with the same token that your own client uses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;compile to native code&lt;/strong&gt;. This one is actually quite effective because unlike plain-old javascript that anyone can read like a book, reverse engineering native code requires more than just some basic understanding of how to use code-beautifiers and browser debuggers. Sure, they can still do it given enough time and effort, but probably 95% of people in the world don't have this kind of skill set.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By effectively using filters and cooldowns, you can force scrapers to work hard to obtain your data instead of just coming in and then walking away with everything in 5 seconds!&lt;/p&gt;

&lt;h2&gt;
  
  
  Feedback
&lt;/h2&gt;

&lt;p&gt;Are there any techniques that you like to use to "secure" your data from scrapers? Or perhaps it's not necessary for the average app developer to think about?&lt;/p&gt;

</description>
      <category>security</category>
      <category>webdev</category>
      <category>scrape</category>
      <category>data</category>
    </item>
  </channel>
</rss>
