How Good Developers Fix Vulnerable Apps?
Published: 2021-03-28 08:15:38

## How to separate insecure from critical logic?

[Follow me on Medium](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fsubscribe%2Fuser%2F3ee57b082bb&operation=register&redirect=https%3A%2F%2Flevelup.gitconnected.com%2Fhow-good-developers-fix-insecure-apps-44372b6eac07&source=post_page-3ee57b082bb----44372b6eac07---------------------follow_byline-----------)
[Original article](https://levelup.gitconnected.com/how-good-developers-fix-insecure-apps-44372b6eac07)

# Where does business and UI logic live?

> These are the business rules, validations, and calculations that operate on the data as it is brought into an information system or displayed by it. — Martin Fowler

Your services are in the business layer. Business rules that you apply to data, and create conclusions on the data.

Your controllers are in the UI layer, responsible for UI, pages, and simple logic around them.

![](https://miro.medium.com/max/559/0*4RZOMzo1nu-49GsU.png)

[Layered Architecture](https://dzone.com/articles/layered-architecture-is-good)

# Why is the business layer critical?

Let’s explain in layman’s terms.[Laurent S.](https://stackoverflow.com/users/2187273/laurent-s)has a great explanation for this topic. I’ll try to deliver his words, with my own opinion attached.

![](https://miro.medium.com/max/700/1*SlF9FL95J407dN5M2cYf3g.png)

[source](https://undraw.co/search)

Let’s look at the truck loading process. There are two workers.

One loads the truck, the worker, one is supervising, the supervisor. Worker represents presentational logic. Supervisor — business rules.

The supervisor cares about the truck weight._How much the truck can carry?_— business rule.

The worker puts the boxes in the truck. The worker decides_where to put the box_— presentation logic.

When you don’t have a supervisor, you need to teach the worker the rules. Let’s say it is a simple rule, 1 tone is the weight limit.

Your business expands, you hire more workers (mobile, desktop, other devices). You teach them this rule.

New rules come. Now you need to adhere to the laws of the country. Laws change too often.

You have two options here:

* Hire lawyers to fill the truck.
* Workers call the lawyer.

Let’s say you pick lawyers to fill your truck. They know the laws and can figure out the weight limit.

In software terms, you put more logic in your presentational layer.

The other option is more beneficial. Workers call the lawyer, get an answer if they continue loading or stop.

In software terms, add business rules to the business layer, which UI can call for information.

# What‘s wrong to do in the UI?

Don’t deceive your customers. Look at this article below.

[## Blatant lies revealed in the source code for an online shop### Are 14 people really looking at this product?medium.com](https://medium.com/dev-genius/are-14-people-currently-looking-at-this-product-e7fe8412f16b)

Don’t lie about active customers. Any developer will see through this lie.

Solve this on the backend. Look at active sessions, and extra info to identify the product. A product which the customer is looking at.

Query the backend for data. Calculate active customers, and respond to the UI layer.

Don’t do your business logic on the frontend. Don’t calculate promotions on the frontend, for example.

[## This Website Accidentally Left Promo Codes in Their Source Code### Finding interesting things by inspecting websitesjavascript.plainenglish.io](https://javascript.plainenglish.io/this-website-accidentally-left-promo-codes-in-their-public-source-code-176c52fbfdc3)

Act on the data on the back-end. Hide sensitive info from the front-end.

Keep your promo rules in the backend. Promotions are conditions and actions. You do need some rule engine to decide if the promotion is valid.

After the decision is made, apply promotion, and return the page. In a headless approach, you call the promo API to get the information.

Seems logical, yet we have bad examples.

# What’s appropriate in the UI layer?

Access the backend logic. Send promo codes. Get back the response. Or embed in the page applied promotion.

Call the backend, get relevant active users to count, and present it on the UI. The backend should calculate this count. Not jQuery.

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93p184cn9a82lffrq314.png)

Don’t do this —[source](https://medium.com/dev-genius/are-14-people-currently-looking-at-this-product-e7fe8412f16b)

# Conclusion

These are few examples of how to combat unwanted behavior.

Do your critical rules on the backend. Check promotions, develop algorithms, on the backend. Less critical ones can go on the frontend. Develop conditional styling in this layer.

With this, you can rest assured, your app is protected. Protected from unwanted behavior.

# Resources

[Enterprise Patterns](https://martinfowler.com/articles/enterprisePatterns.html)

Most of this topic was derived from this[question](https://softwareengineering.stackexchange.com/questions/423680/is-it-really-possible-to-decouple-the-ui-from-the-business-logic).

Photo by[Gabby K](https://www.pexels.com/@gabby-k?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels)from[Pexels](https://www.pexels.com/photo/concentrated-diverse-colleagues-brainstorming-together-in-office-6238174/?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels)