node.js Archives - Lightrun https://lightrun.com/tag/node-js/ Developer Observability Platform Thu, 22 Jun 2023 15:34:20 +0000 en-US hourly 1 https://wordpress.org/?v=6.4.2 https://lightrun.com/wp-content/uploads/2022/11/cropped-fav-1-32x32.png node.js Archives - Lightrun https://lightrun.com/tag/node-js/ 32 32 Debugging Node.js HTTP Requests https://lightrun.com/debugging-node-js-http-requests/ Thu, 15 Sep 2022 17:27:35 +0000 https://lightrun.com/?p=7909 HTTP is the backbone of all API-centric, modern web apps. APIs are the place where the core business logic of an application lives. As a result, developers spend a lot of time optimizing the API business logic.  This article addresses a Node.js developer’s dilemma while debugging an HTTP API request. We take a sample Node.js/Express.js-based […]

The post Debugging Node.js HTTP Requests appeared first on Lightrun.

]]>
HTTP is the backbone of all API-centric, modern web apps. APIs are the place where the core business logic of an application lives. As a result, developers spend a lot of time optimizing the API business logic. 

This article addresses a Node.js developer’s dilemma while debugging an HTTP API request. We take a sample Node.js/Express.js-based HTTP service to demonstrate a new way of debugging Node.js applications using the Lightrun observability platform. 

First, let’s understand what debugging a standard HTTP request entails in the next section. If you want to jump straight into the Node.js HTTP debugging demonstration, you can skip this part.

Debugging HTTP Requests

HTTP follows a request-response pattern. A client sends a request to the server. The server executes business logic and returns a response. The scope for debugging HTTP requests can be broadly categorized into:

  • Debugging Functional Code: Functional code contains the business logic that generates a response by translating HTTP request data to the eventual output returned to the client application, along with an HTTP status code. 
  • Debugging Non-functional Parameters: Non-functional parameters impact the performance of the server. The most common non-functional parameters are response time, authentication, and session related parameters.
  • Debugging Deployment Issues: Deployment-related issues impact the server’s overall health that hosts the backend application serving the HTTP requests. Some of the common problems tracked for debugging these issues include average latency, uptime, CPU/Memory utilization.

Continuous Debugging with Lightrun

For developers, the traditional way of debugging applications is to capture logs. During the development phase, they can edit code for live debugging of the application, running in a local developer environment. However, in a production environment, they are limited to predefined log statements. Adding additional logs involves re-deployment, which has additional process overheads.

Lightrun offers a developer-native observability platform to solve this problem. It provides the agility to perform continuous debugging with a lazy agent that can be attached or detached to the live application at will. Developers can add logs and snapshots to the live application to capture code-specific variables, arguments, or metrics. All of this can be accomplished without touching the source code.

As a result, Lightrun expedites the root cause analysis of any reported bug by directly capturing the application’s internal state.

Setting Up Local Debugging Environment for Node.js

Let’s consider a sample Node.js based HTTP application to take Lightrun for a test drive. 

Follow the sub-sections below to set up your local development environment for debugging a sample application.

Prerequisites

  1. Node.js: Ensure that you have the NVM tool installed on your local development environment. Also, take note to install Node version above v14 via the ‘nvm install’ command.
  2. VS Code: This demonstration uses the popular VS Code IDE. Install it from the official website
  3. Postman: Postman is a popular tool for testing HTTP APIs. Download and install it from the official website. You can also use the popular cURL utility tool for this purpose.
  4. Lightrun account: Create your Lightrun account. You will be taken through a series of guided steps post the signup. Make sure to choose Node.js as the programming language and VS Code as the IDE.

After your Lightrun account is created and VS Code is set up, you should see the Lightrun extension installed and an additional icon displayed on the sidebar of the VS Code IDE.

Setting Up Local Debugging Environment for Node.js

Note: Lightrun currently supports Windows, Ubuntu, and Mac for Node.js. However, the prerequisites and the subsequent steps shown in this demonstration have been performed on a Windows 10 OS.

A Sample Node.js / Express.js HTTP Service

Now let’s create a sample Node.js application with HTTP service.

Step 1: Create a Node.js empty project

Fire up a command-line terminal. Create a new project directory named ‘nodejs-http’ somewhere under your development environment. Change to that directory and create a new Node.js empty project with the default options.

mkdir nodejs-http
cd nodejs-http
npm init -y

This will create an empty package.json file.

Step 2: Add the Node.js HTTP service code

Open your VS Code IDE and point to the project directory. Next, create a new file ‘index.js’

Copy and paste the sample code from this GitHub Gist link in the file.

This code contains a straightforward Node.js/Express.js API service which defines two HTTP API endpoints for a basic task management service:

  •  ‘/POST /task’ : Adds a new task with an id and a description. The id is generated internally, starting with one and incremented for every new task created.
  • ‘GET /task/:id’ : Retrieve a task based on its id.
Note: For this sample application, we have used a simple business logic without incorporating Express.js routing features, HTTP authentication or database access. This is done to keep things simple. This code is only used to demonstrate the debugging capabilities of Lightrun and should not be considered for real world deployment.

Take note of the Node.js specific account credentials for your Lightrun account at the beginning of the source file.

require('lightrun').start({
    lightrunSecret: '<YOUR_LIGHTRUN_SECRET>',
    company: '<YOUR_COMPANY_NAME>',
});

It contains two placeholders. <YOUR_LIGHTRUN_SECRET> is the unique key assigned to your account, and <YOUR_COMPANY_NAME> is the name of your company that you entered while creating your Lightrun account.

Make sure to replace the placeholders <YOUR_LIGHTRUN_SECRET> and <YOUR_COMPANY_NAME> with your Lightrun account credentials. This is available on the home page of the management console, under the “Install the Agent” section.

Getting Started with Lightrun

Update the source file with credentials, save it, and you are ready to start debugging with Lightrun.

Step 3: Activate Lightrun in VS Code

Activate the Lightrun panel on the VS Code IDE. If you are not logged in to the management console, you will be prompted to do so.

Step 4: Install Node.js Project Dependencies

Before running the application, install the Express.js and Lightrun dependencies using npm. Run the following commands in the terminal, under the project directory.

npm install express
npm install lightrun

Step 5: Run the Node.js HTTP Service

Now you are ready to launch the service. Run the following command on the terminal.

node index.js

This will launch the task management API on http://localhost:80. Switch back to the VS Code IDE, and you will be able to see the Lightrun agent running on the Lightrun panel.

 Run the Node.js HTTP Service

The Lightrun agent runs as a lightweight thread within the Node.js V8 runtime. It enables the capture of logs and snapshots from anywhere within the code. Thereby, it gives you the ability to perform debugging on-demand without any changes in the code or runtime environment.

Before proceeding further, let’s do a quick test via Postman to ensure that the APIs are working as expected.

You can add new tasks using the ‘POST /task’ endpoint by sending the following data.

{
    “descr”: “First task”
}

Add POST

Here we have added two tasks with task ids 1 and 2.

To get a specific task by its id, you can trigger the ‘GET /task/:id’ endpoint, replacing id with the actual task id.

Add GET

Debugging Our Node.js Application with Lightrun

With the Node.js application running, and the Lightrun agent attached to it, it’s time for you to learn some debugging tricks.

Some of the most common debugging use cases encountered by developers are:

  1. Functional Code Analysis: This is required for analyzing bugs that cause improper or inconsistent responses from the HTTP service whenever the API endpoint is triggered. Such issues are mostly caused due to:
    1. Logic errors in the code, or
    2. Invalid user input triggering a specific combination of the business logic, which was not anticipated previously.
  2. Performance Analysis: This is required to unearth reliability issues in the system caused by factors that impact its performance. In the case of HTTP service, one of the typical performance issues is the delay of API response.

Functional Code Analysis for Node.js HTTP Service

Lightrun allows you to add log statements dynamically in the runtime application, where previously you would have had to edit the code. 

Think of the countless ‘console.log( )’ statements polluting your Node.js codebase.  Lightrun gets rid of that with a simple action on the VS Code IDE.

createTask

Here we have added a static log message that asserts that the function createTask( ) is invoked when triggered by the ‘POST /task’ endpoint. 

But a simple log statement like this only helps you trace the code execution path. What if you want to check the values of variables and arguments?

With Lightrun, you can display logs with values of any variable present within the scope of the line number where the log is added. 

 display logs with values

Additionally, you can also capture snapshots of the entire stack trace, anywhere in the code.

 capture snapshots

Here we have added a watch expression to capture the newTask object. The ‘POST /task‘ API is triggered twice to create two tasks, whose snapshots can be examined in the snapshot console. Similarly, you can add more watch expressions and set a hit limit for capturing snapshots up to the specified limit. 

 The logs and snapshots are part of Lightrun actions that you can add to the VS Code IDE. 

Performance Analysis for Our Node.js HTTP Service

Lightrun Logs aren’t limited to capturing the variables and stack trace within the application code. They can also evaluate a legit Node.js/JavaScript expression and display the result as a log message.

This is particularly useful for measuring API performance. One of the simplest ways to measure this is to calculate the delay in responding to an HTTP request.

Using the Date.now( ) utility function in JavaScript, you can add two logs at the beginning and the end of a function to capture the Unix timestamp and derive the time delay in handling the HTTP request.

Performance Analysis

As you can see above, we have added two logs to indicate the execution time for function getTask( ) which is called as part of ‘GET /task/:id’ endpoint. 

The resulting log output suggests that the function execution started at time 1644222340499 and ended at 1644222340523. Therefore, by subtracting the start time from the end time, you can gauge that it took approximately 24 milliseconds to execute the function.

This provides you with a data point to check how performant the API is.

Get Ahead With Debugging On-Demand for Node.js HTTP Requests

With this sample app, you can get a sense of how Lightrun saves you countless hours spent recreating the environment, reproducing the bug, and performing the root cause analysis.

This demonstration uses a local development environment based on the VS Code. Lightrun also allows you to add logs and analyze the code running on a remote server using a CLI in the most non-invasive way, be it a remote server, K8 or a serverless environment. Moreover, you can also integrate with external log analysis tools such as Datadog and Splunk to build your custom observability analytics dashboard.

We will soon add metrics, such as counters, timings, and system stats, to make debugging more exciting within the Node.js Lightrun agent. These features will let you use Lightrun more effectively for functional and non-functional debugging. 

Signup for the Lightrun beta program and stay tuned for more tutorials describing the use cases for advanced features coming soon. 

The post Debugging Node.js HTTP Requests appeared first on Lightrun.

]]>
How to use VSCode to debug a Node.js application https://lightrun.com/vscode-to-debug-node-js-application/ Sat, 21 May 2022 10:43:37 +0000 https://lightrun.com/?p=7280 Debugging is an essential step in software development, as it allows developers to fix errors before releasing the software to the public. Debugging tools can be integrated into code editors, making the debugging process more efficient. This tutorial will show you how to debug Node.js in Visual Studio Code (a.k.a VSCode). What is Node.js? Node.js […]

The post How to use VSCode to debug a Node.js application appeared first on Lightrun.

]]>
Debugging is an essential step in software development, as it allows developers to fix errors before releasing the software to the public. Debugging tools can be integrated into code editors, making the debugging process more efficient. This tutorial will show you how to debug Node.js in Visual Studio Code (a.k.a VSCode).

What is Node.js?

Node.js is a JavaScript runtime environment that allows you to run JavaScript code outside a browser. It is not a framework but a technology built on Chrome’s V8 JavaScript engine, allowing it to be used for developing server-side applications and command-line tools. It is also popular on the Internet of Things (IoT). Node.js has a large community of developers who have created a variety of modules that you can use in your own projects.

Node.js apps are applications written in JavaScript that are executed on a server using the Node.js runtime environment. These applications are often built on popular JavaScript frameworks and libraries such as Express and Meteor.

The main features of Node.js are:

  • It is an open-source server-side JavaScript runtime environment.
  • Node.js is a cross-platform runtime environment that allows you to run JavaScript code on the server-side.
  • Applications are written in JavaScript and can be run within the Node.js runtime on OS X, Microsoft Windows, and Linux.
  • Applications are single-threaded, meaning that a single process can handle multiple requests simultaneously.
  • It is asynchronous, meaning that it can handle multiple requests simultaneously without waiting for each one to finish before starting the next.
  • It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
  • It is available under the MIT license.

While all the above is fantastic, there are bugs where there is code. Debugging becomes a common task for every developer as a side effect.

Step-by-step guide on debugging Node.js in VSCode

Prerequisites

Before we begin this tutorial, we will assume that you have both Node.js and VSCode installed.

For this tutorial, we are going to set up a simple Node.js program with an error in it.

We will then follow a debugging process that will give you the basic scope of how to use the Node.js debugger in VSCode.

To create our simple program, run npm init inside an empty project directory in your console. This will give us a package.json file.

In the same directory, create an app.js file with the following code:

const calculate = (A, B) => {
  console.log('Adding...')
  let sum = 0
 
  sum = A + B + B + A
  return sum
 }
 ​
 const num1 = 1
 const num2 = 1
 ​
 const result = calculate(num1, num2)
 console.log(num1 + ' plus ' + num2 + ' = ' + result)

We know this is incorrect. There is a deliberate bug introduced in the sum variable. If we run the program with the command node app.js, we’d get the following output:

 Adding...
 1 plus 1 = 4.

Using the Node.js debugger in VSCode

VSCode comes with a built-in debugging tool that developers can use to debug Node.js applications. This tool is located on the left-hand side panel and looks like this:

Run and Debug panel in VSCode

Alternatively, you can also use the keyword shortcut Ctrl + Shift + D to switch to VSCode’s debugging panel.

Next, click create and launch.json file and select Node.js.

Creating a launch.json file to debug Node.js in VSCode

 

Selecting Node.js as debugging environment in VSCode

You will see a configuration that looks something like this:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "pwa-node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}\\src\\app.js"
        }
    ]
 }

Sometimes, depending on your file directory setup or version, the assigned value to the program may not match the entry main file to your application. For that particular situation, you just need to change it to whatever your main entry file is – which is typically something like app.js or index.js.

You can also configure the Node.js debugger to skip certain files listed under the skipFile array.

Once you’ve completed this, you can save and exit the file. This will change the way the VSCode debugging side panel looks. Here is the updated screenshot.

VSCode ready to debug Node.js

 

Launching the Node.js debugger in VSCode

When you click the play button next to Run, it will launch and run the program through the Node.js debugger. The debugger console will run and exit with the standard output of Process exited with code 0, which means everything worked as expected.

The process of debugging a Node.js app

To debug your app.js file in VSCode, you can set breakpoints by clicking on the red dot when you hover over the line number you wish to put it. A breakpoint is a place where the VSCode debugger intentionally stops or pauses the program’s execution so that you can inspect state changes in the variables and watch and call stack panels located on the left-hand side of the UI.

Node.js local variables shown in VSCode debugger

 

Setting a breakpoint with VSCode's Node.js debugger

Now, when you rerun the program, it will continue down the code and stop at where you placed the red dot. Looking over the variables panel will log out the current state of the values assigned. Simply right-click on the red dot and select Remove breakpoint to get rid of a breakpoint that you no longer need.

Variable values in VSCode's Node.js debugger

Here, our A and B variables are set with values, but the result variable is undefined because it is yet to be defined in the program – meaning that it doesn’t exist yet.

The VARIABLE panel outputs all the current variables and their states.

Meanwhile, the WATCH panel lets you cherry-pick specific variables that you want to keep an eye on. This panel is helpful because it gives you instant insight into what is happening with the code.

If you look to the top of the UI, you will also encounter a control panel that looks something like this:

VSCode debugger control panel

This panel controls the VSCode debugger’s progression through the program. Each control does the following:

  • Continue (F5) will run past the breakpoint and continue with the rest of the program until it hits the next breakpoint.
  • Step over (F10) will take the debugger down to the next line.
  • Step into (F11) will take the debugger into the next function.
  • Step out (F12) will take the debugger out from the function and into the next step.
  • Restart (Ctrl+shift+F5) will relaunch the VSCode debugger.
  • Stop (shift+F5) will stop debugging and exit the process being debugged.

When you click on one of the above controls, you are essentially slowing down the program so you can see the updates occur in the console, the variables, watch, and call stack panels.

Going back to our code sample, it will take us to the calculate() function if we click Step Into. If we look at the VARIABLE panel, we see that the values in it have changed to reflect the local variables currently in scope. When you right-click on a variable, you can select it as a target to watch. Setting this will add the variable to the WATCH panel as per below.

Adding a watch in VSCode's Node.js debugger

 

Undefined watch value in VSCode's Node.js debugger

If you click Step Over, it will run the following line. Our console will simply say Adding… because it’s executed the console.log() call. However, the watched variable has now changed from undefined to 0.

Watch value changed in Node.js debugger

If we run the following line, we will see that the sum variable inside watch has changed to an incorrect value. You can deduce that something wrong occurred in the previous line. We can now fix the issue, rerun the entire VSCode debugger, and ensure everything is working.

Finding the offending line on code in VSCode's Node.js debugger

Debugging Node.js in production with Lightrun

While debugging Node.js code during development is easy enough in VSCode, it’s a different kind of ball game when debugging in production. When there’s a bug in production, we cannot simply shut down the server and take the code down to debug. Not only that, replicating production bugs in a development environment may not be possible. This is where a tool like Lightrun can help.

Lightrun is our next-gen remote debugger for your production environment. With Lightrun, you can inject logs without changing code or redeploying, and add snapshots: breakpoints that don’t stop your production applications. Lightrun supports debugging Node.js, Java, and Python production applications, integrates with VSCode, and you can start using it for free.

The post How to use VSCode to debug a Node.js application appeared first on Lightrun.

]]>
Node.js Security and Observability using Lightrun & Snyk https://lightrun.com/nodejs-security-observability-lightrun-snyk/ Mon, 23 Aug 2021 17:48:13 +0000 https://lightrun.com/?p=6353 What if we could also prevent security issues in our code before we ship it? What if we could code-level visibility into what's going on inside our production applications, right there in the IDE?

The post Node.js Security and Observability using Lightrun & Snyk appeared first on Lightrun.

]]>
 

As developers, we spend a lot of time in our IDEs writing new code, refactoring code, adding tests, fixing bugs and more. And in recent years, IDEs have become powerful tools, helping us developers with anything from interacting with HTTP requests to generally boosting our productivity. So you have to ask — what if we could also prevent security issues in our code before we ship it? What if we could code-level visibility into what’s going on inside our production applications, right there in the IDE?

This is where the magic of developer-first class of tools comes into play. With the right plugin for your IDE, observability and security can be built right into the day-to-day workflows of developers — where it’s most effective. 

In this blog post, we’ll take a look at the following:

  • How to gain real-time code-level observability to your data structures right there in your IDE
  • How to find harmful user input in a production Node.js application 
  • How to find and fix security issues in your code and open source dependencies

How to gain real-time application observability in your IDE

Here’s what you need to follow along with this post:

  1. IntelliJ IDEA (Lightrun has Visual Studio Code support coming out soon – sign up to the beta here to get it first!)
  2. A Lightrun account and Lightrun’s IntelliJ Plugin and the Node.js Agent which is currently still in beta

Once the IntelliJ IDEA plugin is installed, click on the Register button, which will take you through a seamless registration process via the browser:

Lightrun plugin in IntelliJ IDEA

Next, we need to get the Lightrun Node.js agent installed and configured for the Node.js application. You will see instructions during the registration process, but if you missed it here they are again:

  1. Install the lightrun agent in our project folder by running: npm install lightrun
  2. Instantiate the Lightrun agent at the beginning of our Node.js application code-base, right at the app.js file entry code, as follows (you will see the `<YOUR-COMPANY-NAME>` and `<YOUR-COMPANY-SECRET>` values on the screen during the registration process):
require('lightrun').start({
    company: '<YOUR-COMPANY-NAME>',
    apiEndpoint: 'app.lightrun.com',
    lightrunSecret: '<YOUR-COMPANY-SECRET>',
    caPath: '',
});

I’d not be doing justice if I didn’t call out the secrets in code scream, so the very first thing I want to point out once you reached this point is to update that line of code for `lightrunSecret` to be: lightrunSecret: process.env.LIGHTRUN_API_KEY

And then when you start the application locally, remember to first make this environment variable available. If you’re running the application from the command line, you can do it like so:

 

export LIGHTRUN_API_KEY=<YOUR-COMPANY-SECRET> npm run start

Note: You only need to run the export command once and it will be available in your terminal shell session as long as you keep on using the same one.

Once you start the application, you’ll notice on the right-side bar that the Lightrun plugin is now both configured well and connected, as you can tell by the annotation of your machine’s name (mine is “Lirans-MBP”):

Lightrun plugin is now both configured well and connected

How to find harmful user input in a production Node.js application

The Node.js application that we will experiment with is Snyk’s Goof TODO application which is an end-to-end JavaScript and Node.js To-Do note taking application.

You can grab a copy of it by cloning the GitHub project and follow the common npm projects installation instructions for dependencies to get started. Mind you, it also needs a MongoDB database to connect to when it spins up.

Once you have it up and running, it should look like the screenshot below. You’ll notice the application has an interesting capability. If I send it a todo string such as `Get my app secured in 15 minutes`, it will use the open source humanize-ms npm package to transform those `15 minutes` text into a formatted style of `[15m]` text. The humanize-ms package has about 4,500,000 weekly downloads, so it seems like a good choice.

Goof TODO app

Now, if you’re a security-minded person and have done some backend application development in the past, you probably would have ensured that the input adheres to some validations and expected schema.

However, this thing is now running in production and while I’m not sure what sort of user input it is getting, I don’t want to break any functionality. That said, I do want to get some visibility into potential dangerous user input that it might receive. This is where Lightrun shines!

I want to capture alerts in case someone is sending user input with a string text larger than 10,000 characters. To do that, I’ll add a Lightrun log with a condition in IntelliJ right on the line of code that parses the user input text. It looks like this:

Adding a Lightrun log with a condition in IntelliJ

To do so, choose that Log type of annotation when you right-click the line of code, and setup the logging rule as follows: `item.length>10000`

Setting up the logging rule

This essentially establishes what I would call an alert that is now set in my live running Node.js application, so that any request making it to this code path, and triggering the condition with any input length larger than 10,000 characters.

Once I’ve put the Action in, I get this nice little annotation over that line of code, along with the user who added it. It’s a nice way of drawing attention and providing valuable information developers that are responsible for technical debt and refactoring in the future:

Lightrun IDE alert

Adding an alert from the IDE is great, but you know what makes it awesome? Being able to view these alerts straight-up in the IDE console, rather than opening a new web page to browse logs.

To make that magic happen, head over to the right sidebar of the Lightrun plugin, and on the agent entry click the most-right icon that says PIPE. Then, click the Both button. This means now that alert logs will be sent to both the Lightrun SaaS app, as well as directly into my IDE Lightrun console window. Yay!

Alert logs will now be sent to both the Lightrun SaaS app

Now that we have an alert set, let’s experiment with sending some large payloads. To send off these payloads I am going to make use of a few handy command line tools at my disposal:

  1. Oh My Zsh shell setup (who doesn’t like a fancy shell prompt?)
  2. The HTTPie tool to send off the HTTP request that adds a todo item
  3. The Unix seq tool to easily repeat a character X number of times

With those, I am going to send the payload using the following command:

echo 'content=Buy milk in '`seq -s "" -f "5" 60000` 'minutes' | http --form http://localhost:3001/create -v

This creates the todo text `Buy milk in 500000… minutes` with the number `5` having some additional 60,000 zeros after it — a pretty large string.

In the application interface, this looks as follows:

Goof TODO app

As you can see, the application did not crash, the MongoDB database didn’t crash either, and it seems that the overall side-effect is that the humanize-ms npm package simply parses that time into the word Infinity. Overall, it makes sense, and not an actual issue. It seems.

Remember, that we set an alert for it back in the IDE?

Let’s see what that looks like…

Do you see that highlighted line in the Lightrun Console window? We’re getting alerts that meet a logging condition of an HTTP request that was sent to a live running application, right there in the IDE. That’s pretty (pretty helpful, too!).

Virtual log entry in the Lightrun console

But, what’s all the fuss about with this alert? Follow along…

How to find and fix security issues in your code and open source dependencies

So, apparently, that version of the humanize-ms npm package, has some indirect security vulnerabilities. The 4,500,000 weekly downloads were misleading! The security vulnerability in that package could be quite devastating for any Node.js web application.

How do I know that? Because I installed the Snyk IntelliJ plugin and I let it scan my project and  it discovered this and a few other vulnerabilities. Let’s take a look at how to run a scan, as well as how significant that security vulnerability is using the large user input payload alert in Lightrun.

Once you have the Snyk IntelliJ plugin installed and authenticated (see this How to install the plugin step by step guide), click the green Play button on the Snyk plugin window to initiate a security scan.

The results of the scan will be for the following potential security issues:

  1. Security vulnerabilities in your third-party open source libraries, such as those npm packages that you import to the codebase. If you’re scanning projects of other languages like Java, Ruby, Python and such, Snyk will automatically pick up the package manifest file and scan them too.
  2. Code security issues in your own code, currently supporting JavaScript, TypeScript, Java and Python.

Here’s Snyk Open Source security scan results for the third-party npm packages I added as dependencies to this TODO project:

 Snyk Open Source security scan results

Scrolling through these, you can see that this TODO application has quite a few security vulnerabilities in it. It’s an intentionally vulnerable application so we can demonstrate, practice and learn about these vulnerabilities and how to fix them.

Remember, our `humanize-ms` npm package to parse and format time strings? Apparently, it brings in a Regular Expression Denial of Service vulnerability (ReDOS), because it depends on a vulnerable version range of the popular ms npm package

 Regular Expression Denial of Service vulnerability (ReDOS)

Let’s go back to our security exploit payload and take another look at it:

echo 'content=Buy milk in '`seq -s "" -f "5" 60000` 'minutes' 
http --form http://localhost:3001/create -v

As you remember, when we ran this command, it sent a very long string as a todo item, but it didn’t cause any sort of denial of service from what we could tell. The application was continuously processing requests as normal, and the Node.js application sent a response back almost immediately.

However, regular expressions are… tricky. If they’re not written correctly, they could be vulnerable to catastrophic backtracking, which essentially means that for linear input, it will take superlinear time to compute.

The only thing I need to change in my exploit payload to effectively attack this application is simply to rename the word `minutes` to `minutea`, and send the command. To show you the before and after, take a look at this screenshot where I haven’t yet sent the request and the htop command showing normal CPU activity in my laptop:

htop showing normal CPU activity

What I expect to happen with a ReDOS vulnerability is that the Node.js application will take a considerable amount of time to compute the regex, and due to the single-thread nature of the Node.js architecture, all subsequent requests will stall. The CPU will spike to 100% as it consumes all available resources to compute the regex, in vain.

Let’s send our ReDOS exploit payload:

echo 'content=Buy milk in '`seq -s "" -f "5" 60000` 'minutea' 
http --form http://localhost:3001/create -v

Behold! The top right terminal screen shows the sent HTTP request, with no immediate response, and the bottom left terminal screen showing the Node.js process as the green highlighted entry at the top of the table with a 98.4% consumption of CPU resources:

A ReDOS exploit in action

Next up is, of course, fixing this and other security issues I have. Once you import your projects to the Snyk application, it will continuously scan and monitor them in the background. It will also automatically raise pull requests to your Git repositories, whether on GitHub or elsewhere, to upgrade the vulnerable versions in your projects. More on this in the article on getting started with Snyk Open Source.

Developer-first security and observability

Developer-first tools in the forms of Lightrun and Snyk are helping developers to be more productive in their day to day development experiences by bringing the data to them, rather than taking them away from their workflows.

Leveraging observability and security right there in the IntelliJ IDE, or in the command line, helps developers monitor real-time traffic, and find and fix security issues before they even reach the Continuous Integration server — let alone production.

To get you started with both, see the following resources:

  1. Getting started with Snyk
  2. Getting started with Lightrun

The post Node.js Security and Observability using Lightrun & Snyk appeared first on Lightrun.

]]>
Node.js SQLite Tutorial https://lightrun.com/node-js-sqlite-tutorial/ Mon, 27 Jun 2022 15:05:21 +0000 https://lightrun.com/?p=7475 Node.js is a JavaScript runtime environment that allows you to build server-side applications. SQLite is a lightweight, self-contained database popular for its ease of use and portability. The two technologies work well together because developers can easily embed SQLite databases within a Node.js application. Therefore, it is easy to develop applications using data from an […]

The post Node.js SQLite Tutorial appeared first on Lightrun.

]]>
Node.js is a JavaScript runtime environment that allows you to build server-side applications. SQLite is a lightweight, self-contained database popular for its ease of use and portability.

The two technologies work well together because developers can easily embed SQLite databases within a Node.js application. Therefore, it is easy to develop applications using data from an SQLite database without connecting to a separate database server.

The combination of Node.js and SQLite is popular because it provides a fast and efficient way to build web applications. Here are some reasons why.

It is easy to get started

Both node.js and SQLite are open source and have a large community of developers and users.

It is easy to deploy

Both Node.js and SQLite are lightweight and don’t require a lot of resources to run. They can run on various platforms, making them versatile, especially in mobile development.

It is easy to scale

Node.js is an open-source, cross-platform runtime environment for developing server-side and networking applications. SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain SQL database engine.

It is easy to use

With Node.js and SQLite, you can quickly build lightweight, fast, and reliable applications. Node.js and SQLite are easy to use because they are simple, efficient, and fast due to their asynchronous I/O and the use of a single thread. They are also crash-resistant, reliable, and have a small footprint.

It is easy to port

They are both easy to port as SQLite is a self-contained, serverless database, and node.js is a JavaScript runtime that can be run on any platform.

SQLite usages 

SQLite is a relational database management system (RDBMS), similar to MySQL, Oracle, PostgreSQL, and Microsoft SQL Server. However, unlike these more traditional RDBMSes, SQLite is not a client-server database engine. Instead, it is embedded into the application that accesses the database. SQLite is an ideal choice for mobile apps, web browsers, and other applications where a local database is required, but the overhead of a client-server database is not.

Traditional vs SQLite serverless architecture

SQLite is the most widely deployed database in the world, with billions of copies. SQLite is often the database for small applications, especially those built for devices with limited memory and storage capacity, such as smartphones.

In addition to being used in mobile apps, SQLite is also a popular choice for web browsers. Many web browsers use SQLite to store local data such as history, cookies, and form data. Chrome, Firefox, Safari, and Opera all use SQLite.

Some applications also use SQLite to store configuration data or other types of data that do not need to be relational. For example, the version control system Git uses SQLite to store metadata for its repositories.

Node.js SQLite Tutorial

Developers can use SQLite as the database for a Node.js application. Node.js has built-in support for SQLite3 through the sqlite3 module. Here is a quick tutorial on how to use SQLite in Node.js:

Prerequisites

To use Node.js with SQLite, you need to have:

  1. Node.js installed on your system.
  2. The sqlite3 Node.js module installed.
  3. An SQLite database file.

The sqlite3 module is available on npm.

Installing SQLite

Download SQLite

First, you will need to download SQLite on your computer. You can find the latest version of SQLite here. Scroll down to the “Precompiled Binaries For Various Platforms” section and download the SQLite library and command-line shell for your platform.

Install SQLite

Once you have downloaded SQLite, you can install it by following the instructions for your platform.

SQLite for your Node.js app

The SQLite3 module is bundled with Node.js. To use the module, you need first to install it. The easiest way to do this is with the npm command:

npm install sqlite3

Once you have installed the module, you can use it in your Node.js programs.

Here is an example of a code snippet that uses SQLite in a Node.js application:

var sqlite3 = require('sqlite3');
 ​
 var db = new sqlite3.Database('example.db');
 ​
 db.serialize(function() {
  // Create a table
  db.run("CREATE TABLE IF NOT EXISTS Foo (id INTEGER PRIMARY KEY, name TEXT)");
 ​
  // Insert data into the table
  db.run("INSERT INTO Foo (name) VALUES ('bar')");
 ​
  // Query data from the table
  db.each("SELECT id, name FROM Foo", function(err, row) {
    console.log(row.id + ": " + row.name);
  });
 });
 ​
 db.close();

Add a new database and tables

You can create a new SQLite database using the following:

 var sqlite3 = require('sqlite3').verbose();
 var db = new sqlite3.Database('mydb.db');

Now that you have a database, you can create a table:

 db.run("CREATE TABLE IF NOT EXISTS mytable (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)");

Inserting data

You can also insert data into the table:

db.run("INSERT INTO mytable (name, age) VALUES ('John', 30)");
 db.run("INSERT INTO mytable (name, age) VALUES ('Jane', 20)");

Query the database

To query the database, you use the db.all() method. This method takes a SQL query as a parameter and returns an array of objects, one object for each row in the result set.

For example, to query all rows in the mytable table:

 db.all("SELECT * FROM mytable", function(err, rows) {
  rows.forEach(function (row) {
    console.log(row.id + ": " + row.name + " (" + row.age + ")");
  });
 });

 The above code will output the following:

1: John (30)
 2: Jane (20)

If you only want to query a single row, you can use the db.get() method. This method takes a SQL query as a parameter and returns the first row in the result set.

For example, to query the row with id 1:

 db.get("SELECT * FROM mytable WHERE id = 1", function(err, row) {
  console.log(row.id + ": " + row.name + " (" + row.age + ")");
 });

The above code will output the following:

 1: John (30)

Summary

One of the main features of Lightrun is the ability to track and visualize your node.js performance data, which can be extremely helpful in identifying areas where your node.js applications are performing poorly.

Additionally, Lightrun also provides a variety of tools that can help you optimize your node.js applications. Lightrun works with applications written in node.js, Python, and JVM languages, regardless of how they are deployed. Get started with Lightrun to add metrics and traces to your code in real-time and on-demand without needing to bring down your production stack.

The post Node.js SQLite Tutorial appeared first on Lightrun.

]]>