Day 11 : Lightning Web Component Challenge — Review #TexeiAdventCalendar

By

3 minutes de lecture

Today we are going to talk about LWC security. But before, after 10 days of close inspection, it is time to give you answers of the day 1 challenge (you can still find here: https://blog.texei.com/day-1-lightning-web-component-challenge-texeiadventcalendar-231743ef2b19).

First of all, let’s remind us of how LWC works : As classical MVC framework, LWC is locally store in your Web browser. It means users can see your .html and .js file, and can modify it as wanted. You have to be aware that everything store in those file is accessible.

LWC security
LWC environment overview

Component 1

Component 1 use a <template if:true={isUserConnected}> to display the code only to connected user. The key is hard-coded in the .html file and the part of file is only displayed if the value isUserConnected is true. We can easily see the key in component1.js. See Component 1 code

Step 1 : Open your chrome console
Step 2 : Go to Sources and the find the component in s/module/c/component1.js and click on small bracket to format the file

Component 2

The component 2 display a list specific field of some record. But in the background, every fields of the objet are send from the APEX controller. As in the component 1, you just have to look into chrome console but not in static file this time, in the debugging statement. This way you can to see every filed retrieved of the objets (with the one containing the key). See Component 2 code

After opening chrome console open s/module/c/component2.js and click on small bracket to format the file

We have to look for our .js file component2.js and identify the wire targeted. then we just have to put a breakpoint just after and check the object

LWC security: How to secure Component 2 then ?

  • Remove the field from the initial SOQL request as we dont need it in the component here
  • Use the key word WITH SECURITY_ENFORCE in your SOQL request which avoir user to retrieve data he can’t usually access

Component 3

To get Component 3 key, you just need to do a basic SOQL injection. SOQL executed by the component was given (component 3 code):

You have to found what to put as searchKey to return all record and the key. using x%’)OR(Name like ‘ you can get the result expected

LWC security: How to secure Component 3 then?

  • Avoid dynamic SOQL request extracting searchKey from the request first
  • Using String.escapeSingleQuotes function to update the entry provided by the user. This method appends the escape character (\) to all single quotes in a string passed by a user. This method treats all single quotes as enclosing strings, instead of database commands.

These small examples shown you that when exposing LWC to the public includes some risk. It is necessary to be rigorous in the code implementation otherwise some sensitif data could be exposed.

That’s all for our tip about LWC security. Hope you had a good time! Come back tomorrow for a new article written by Elora Guyader. Want to continue learning? Check out our next article Manage nested Object in your Lightning Web Components.

Follow us on LinkedIn here and on Twitter here!

More to read on LWC security :

https://medium.com/salesforce-platform/is-your-lightning-component-secure-5a5a25b98f17

https://trailhead.salesforce.com/fr/content/learn/modules/secure-clientside-development?trail_id=security_developer

https://trailhead.salesforce.com/fr/content/learn/modules/secure-serverside-development?trail_id=security_developer

Read more posts

Enforce code standards with PMD

Developers working on a project usually set coding rules to have a standardized codebase. It is an important piece of the code maintainability, and it can be very easy …
March 2023
Advices
Scratch orgs

Uncovering Salesforce Settings: A Step-by-Step Guide for Scratch Orgs

Today, it’s pretty easy to build your Scratch Org definition file when you know what Settings you want to activate, as they are mapped with the same setting names …
February 2023
Advices
Business Analyst

Core qualities of a Business Analyst?

A common definition we are used to hear is that being a Business Analyst means to have a combination of both hard skills and soft skills. What does a …
June 2022
Advices
Image d'illustration d'une employée travaillant sur un ordinateur portable

Process builder and workflow make way to Flows (½)

Overview “If you can do it with a Workflow, then do it with a Process Builder, because everything a Workflow does, a Process Builder does it better”. If you …
March 2022
Advices

Day 22 : Salesforce new “Migrate To Flow tool” in Spring 22

As most of you already know, the workflow rules and process builders are planned to be retired in 2023 (no precise date defined so far). Today, I’m going to …
December 2021
Advices

Day 18 : Fake callout responses for test classes !

Hello everybody ! Today let’s talk about Apex tests classes in Salesforce. Everyone loves a good test class, and Salesforce makes it official by requiring to have a minimum …
December 2021
Advices