Security question

How can I generate safe path?

const myPath= `\`${userInput}\``;

   .insert(myPath,myData,{ createParents: true })

userInput is a string , comes from end user , it can be dangerous , I just limit the size of string to 100 characters

In the following I show some userInput ==> myPath example

foobar ===>`foobar`
foo bar ===>`foo bar`
it`s ==>`it`s`
it`.`childObject ===>`it`.`childObject`

As you can see, Here we have a security issue

  1. How can I have a safe path?
  2. What is the name of this issue? it is like SQL injection
  3. As I show , user can change our structure , you want to save data at not ,Is there any other issues?

Hey @socketman2016,

The pathing available from our sub-document API is relatively limited. If you want to prevent the user from pathing deeper than a single level, you should be able to simply strip away any . characters. There is no need to worry about most things from SQL as there is no way to move ‘up or across’ levels with the sub-document pathing.

Cheers, Brett

My path is based on email and contains . character


I just replace ` with `` , it is okay?

@brett19 can you confirm that replacing ` to`` can guaranteed safety

@brett19 reply please …

Hey @socketman2016,

Escaping of a the . to prevent the user specifying a path which nests deeper than expected should be enough. There are no other safety concerns with sub-document, as it doesn’t allow you to perform any unsafe forms of operations from a path perspective.

Cheers, Brett

How can I escape . ? Replace with . ?
What about my approach? Replace with `` and sourond with `

@brett19 Sorry that I mention you again, But I want to sure

Hey @socketman2016,

I actually recently stumbled upon our documented best practices for handling sub-document paths. It should help you immensely, you can find it here:

You can also find a pending version of that document with all javascript examples here:

Cheers, Brett

1 Like