Securing a Webapp - Step 1: Start As You Mean To Go On
Tags: security, owasp, secureheaders
Published: 2020-02-03 23:47:06

---
title: Securing a Webapp - Step 1: Start As You Mean To Go On
published: true
description: Security starts at the very inception of an idea, not at the end
tags: security, owasp, secure headers
series: Securing A Website
cover_image: https://images.unsplash.com/photo-1454165804606-c3d57bc86b40?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80
canonical_url: https://rjj-software.co.uk/blog/securing-web-apps-pt1/
---

_The cover images for this post is by [Helloquence](https://unsplash.com/photos/5fNmWej4tAA)_

> “Begin at the beginning," the King said, very gravely, "and go on till you come to the end: then stop.”
> - Lewis Carroll, Alice in Wonderland

For those who may have missed it, this is part one in a series of posts on how you can secure a web app. [Here is a link to part 0](https://dev.to/rjjsoftware/securing-a-webapp-step-0-an-introduction-5no), which lays out what I'm going to achieve with this series of posts.

As a quick reminder (and a form of tl;dr): I'm in the process of building a web application for a real client, and I'm going to be covering what I'll be doing in order to ensure that the app has security baked in from the get-go.

The point of this post is to get you thinking about the things you should be including in your design decisions _at the start_ of your project. Some of these features require explicit design from the start (multifactor auth, for example), and some can be "bolted on" afterwards.

Before we start, I'm not a security expert. I'm simply collecting all of the knowledge that I've gained over the years into a series of posts. I want to give you a great place to start in discovering the ways to lock down the systems that you are designing. It's also not meant to be an exhaustive list of things to include but should be enough to get you out of the unknown-unknowns and into the known-unknowns

_i.e. you should have enough information to do some more research of your own_

### Security From The Ground Up

The first thing that you need to be thinking about when you start a new project is how you can keep the bad guys<sup>TM</sup> out. The easiest way to achieve this is to not build the app. Seriously, if it doesn't exist then they can't break-in.

How can someone steal your car, if you don't have one?

But your clients are going to get quite upset with you if you take their money and don't produce anything in return. So we need to take the next step up: add security in from the get-go.

Imagine that the building you work in has those card-based entry points. You know the ones: you have to present a card at each door for it to unlock and if you're not authorised to get in, then the door doesn't unlock.

![A card-based entry system](https://dev-to-uploads.s3.amazonaws.com/i/7mdrhjjblyf3cinrj1o7.jpg)

Then you look up and realise that the building has [those ceiling tiles which can be pushed slightly out of the way](https://en.wikipedia.org/wiki/Dropped_ceiling), revealing a walk-way.

![A dropped ceiling](https://dev-to-uploads.s3.amazonaws.com/i/5pt474qyabndsp6z8hd4.jpg)

How secure is that door now? Not very, right? Sure the bad guys need to bring a ladder, but that's totally doable.

Worse yet, you have floor to ceiling glass doors with a single lock on them; bolting them both closed and to the floor. What if I cut out the lock?

_this may or may not have actually happened at a previous employer's office_

With that in mind, we want to include security at the very beginning.

### But I Don't Have a Site Yet

And this is the best time to start. Adding things like [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) in after the fact will just give you headaches.

Adding security at this stage (i.e. before there's a design) will inform how the design comes together.

Let's say that your client wants an e-commerce site. They want to be able to show off their wares, have users make orders, and take payments. Well, you don't _have_ to build all of that functionality. Imagine having to create all of that, secure it, and be the person to blame when something goes wrong.

_because it's going to go wrong_

You don't need all that stress, so just drag in Stripe (or your payment processor of choice) and throw together a [forms over data](https://dotnetcore.show/episode-18-the-history-of-net-with-richard-campbell/) view for the database.

But even that needs security adding to it.

### Single Responsibility Principle

Putting aside all of the complex stuff that you _can_ to in order to make an app more secure, let's start right at the beginning: the [single responsibility principle](https://dev.to/dotnetcoreblog/an-intro-on-http-security-580a#single-responsibility-principle) states that a user can do one thing only:

- Is the user a blog post editor? Then they don't need access to the database settings
- Is the user someone who packs customer orders into a box? Then they don't need access to credit card details

This goes hand-in-hand with the [Principle of Least Privilege](https://en.wikipedia.org/wiki/Principle_of_least_privilege). Average users of Windows (i.e. usually not tech workers) fall afoul of this more often than Unix and Linux users, as the default type of user on Windows is an Administrator.

Create an account for your two-year-old so that she can play around with the keyboard and make the lights flash, and suddenly she's making system-wide changes to your computer, for instance.

_that may or may not have happened to someone I know_

I don't mean to pick on Windows here, I just use it as an example.

So now that you've decided that you have a number of user types, and you've started to slice the functionality into pieces, next is how the users log in.

### Single vs Multi-Factor Auth

[Multi-factor authentication](https://en.wikipedia.org/wiki/Multi-factor_authentication) isn't necessarily a new thing

_hardly anything in tech really is new, these days_

but it has been shown to vastly improve the security of a system with very little downsides. Sure it can be a headache for some users, but we're seeing the widescale adoption of multi-factor auth in systems. This means that - from an auth perspective at least - things are getting better.

But what _is_ multifactor auth? We're all familiar with single-factor auth (i.e. username and password), as something that we know. Multifactor auth is an extension on this and includes:

1. Something you know (username and password, or pin)
1. Something you have (cell phone, YubiKey)
1. Something you are (biometrics, geolocation)

Multifactor auth can help in protecting both users and the software that they use by adding an extra step in the auth cycle. That way, if the credentials of a user are discovered, a malicious party won't be able to use them to log in - they'll need to figure out how to beat the multifactor auth steps.

Make no mistake, multifactor auth isn't a silver bullet and can still be beaten. But you only need to be more secure than your competitors.

### User Input

Imagine that you have a series of antiques in your living room. One day, you have friends over and their three-year-old child causes some kind of [Rube Goldberg machine](https://en.wikipedia.org/wiki/Rube_Goldberg_machine) like effect by knocking one of your chairs over, causing a chain reaction which destroys all of your antiques.

You question the little one, asking for the truth. "No one will be upset if you tell the truth," you say. And when the little one in question opens their mouth, they tell the most fantastical story involving aliens, soldiers, knights, and a crocodile.

There's a chance that you live in a country which has constant issues with aliens and crocs, but most of us don't.

The point is: do you trust what the little one said?

Just like the little one's story, you should never trust user input. It's ridiculously easy to target websites with things like SQL injection attacks - remember little Johnny Drop-Tables:

![Exploits of a Mom](https://imgs.xkcd.com/comics/exploits_of_a_mom.png)

_[source](https://xkcd.com/327/)_

Each time you allow a user to supply any kind of input (text, files, telemetry, etc.) you are opening your system up to attack. Most of these attacks can be mitigated by enabling validation on three surfaces:

- Client
- Server
- Database

---

_EDIT:_
It has been pointed out in the comments, that the original version of this paragraph seemed at odds with my intent. I'll leave the original paragraph here, but will also include a revised one

_Original:_

You can enable validation built into the HTML standard, or use libraries like [jQuery Validation](https://jqueryvalidation.org/), for the client. Checking the input _before_ it is sent to the server can be a lifesaver, and will save you from having to validate on the server - not to mention the roundtrip back to the server.

_Revised:_

You can enable validation built into the HTML standard, or use libraries like [jQuery Validation](https://jqueryvalidation.org/), for the client. Checking the input _before_ it is sent to the server can save bandwidth, but should never be trusted. You can't trust client-side validation for a number of reasons: chief of them being that I can turn JS off, alter the JS before it's executed, or add breakpoints to alter it whilst it's executing.

Relying on client-side validation alone is effectively suicide, as Pablo pointed out in the comments:

{% devcomment l66e %}


---

All major server-side frameworks include some form of validation. I'm a .NET developer and I know that support for validation is built into ASP .NET Core. It requires a call to `ModelState.IsValid` in a controller method, in order to check the model. You can even have those errors sent back to the client to be displayed.

Validation at the database level is a little harder to achieve and is very specific to the database technology that you are using. Some database technologies don't have validation and will blindly accept any data that they are fed, whereas some have validation enabled out of the box.

If you want to go the whole hog, you can validate the data on the way out of the database, too. This might be required for your industry, or it might be seen as a little too much. YMMV.