Applied Patching

Overview

The web application was relatively securely coded, except for the eval injection vulnerability. We'll provide:

  1. Alternative approaches

  2. Extra security measures

  3. Specific code patches


Recommendations Summary

Priority
Recommendation

1

Replace eval with function callback

2

If eval required, use safe-eval (with caution)

3

Always sanitize user input

4

Always validate user input


The Problem

function validateString(input, onError) {
  if (...) {
    eval(onError);  // ← User input reaches eval
  }
}

The Solution

Use onError as a function instead of string:

Updated generateQR Call

Why This Works

  • User input stays within message string

  • No code evaluation occurs

  • Same functionality maintained


Alternative 2: safe-eval (Use with Caution)

Installation

Warning: Known Vulnerabilities!

Recommendation

⚠️ Avoid eval whenever possible!

Only use safe-eval when:

  • eval is absolutely required

  • Combined with other security measures

  • Risk is understood and accepted


Extra Security: Sanitization

Option 1: Third-Party Package

Add middleware in routes/service-routes.js:

Use in controller:

Comparison

Method
Pros
Cons

Package

More comprehensive

Dependency overhead

Manual

Simple, no deps

Limited to specific chars

Recommendation: Use manual for this app (specific, simple requirement).


Extra Security: Validation

Why Packages Are Better

  • Validation requires thorough understanding

  • Multiple input types need different rules

  • Built-in patterns reduce errors

  • Maintained by security experts

Using Yup

Install:

Basic Schema:

With Role-Based Error Messages

Replace validateEmail

Before:

After:


Complete Patch Summary

1. Replace eval with function

2. Update validateString calls

3. Add input sanitization

4. Add input validation


Patched service-controllers.js


Verification Steps

1. Re-run PoC Exploit

2. Test Normal Functionality

3. Test Validation


Secure Coding Tips

General

Tip
Description

Avoid eval()

Use safer alternatives

Validate all input

Server-side, always

Sanitize all input

Remove unwanted chars

Use typed functions

Functions over strings

Use security packages

yup, validator, helmet

JavaScript Specific

npm Security


Checklist

Patching

Verification

Documentation

Last updated