[SERVER-8931] Shell's cd() and mkdir() functions should validate user input and not print stack trace Created: 11/Mar/13  Updated: 11/Jul/16  Resolved: 30/May/13

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 2.4.0-rc2
Fix Version/s: 2.5.1

Type: Bug Priority: Minor - P4
Reporter: Norberto Fernando Rocha Leite (Inactive) Assignee: Tad Marshall
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

macosx source build


Backwards Compatibility: Minor Change
Operating System: ALL
Participants:

 Description   

running cd() function on the shell produced the following stack trace:

> cd 
function cd() { [native code] }
> cd()
Mon Mar 11 09:38:08.331 Assertion: 13111:field not found, expected type 2
0x102b04e6b 0x102ae36be 0x102ae377d 0x102a2b571 0x102a2b5f9 0x102a2d936 0x102ac84e0 0x102ac713d 0x102bc4a40 0x33e29f306362 
 0   mongo                               0x0000000102b04e6b _ZN5mongo15printStackTraceERSo + 43
 1   mongo                               0x0000000102ae36be _ZN5mongo11msgassertedEiPKc + 174
 2   mongo                               0x0000000102ae377d _ZN5mongo11msgassertedEiRKSs + 29
 3   mongo                               0x0000000102a2b571 _ZNK5mongo11BSONElement3chkEi + 793
 4   mongo                               0x0000000102a2b5f9 _ZNK5mongo11BSONElement6StringEv + 41
 5   mongo                               0x0000000102a2d936 _ZN5mongo11shell_utils2cdERKNS_7BSONObjEPv + 102
 6   mongo                               0x0000000102ac84e0 _ZN5mongo7V8Scope14nativeCallbackEPS0_RKN2v89ArgumentsE + 680
 7   mongo                               0x0000000102ac713d _ZN5mongo7V8Scope10v8CallbackERKN2v89ArgumentsE + 123
 8   mongo                               0x0000000102bc4a40 _ZN2v88internalL21Builtin_HandleApiCallENS0_12_GLOBAL__N_116BuiltinArgumentsILNS0_21BuiltinExtraArgumentsE1EEEPNS0_7IsolateE + 464
 9   ???                                 0x000033e29f306362 0x0 + 57048426373986
Mon Mar 11 09:38:08.337 JavaScript execution failed: Error: field not found, expected type 2

The mkdir() function has the same problem.

These functions should throw a UserAssertion on a failure to validate their input arguments, resulting in a JavaScript exceptiojn that can be trapped by user code. They should not print a stack trace.



 Comments   
Comment by auto [ 30/May/13 ]

Author:

{u'username': u'tadmarshall', u'name': u'Tad Marshall', u'email': u'tad@10gen.com'}

Message: SERVER-8931 Do not display stack trace on invalid cd() and mkdir() commands

Validate user input for the cd() and mkdir() JavaScript functions
and use uassert (UserAssertion) on validation failures. This will
be translated into a JavaScript exception (and so can be caught by
user code) and will not generate a stack trace.
Branch: master
https://github.com/mongodb/mongo/commit/3ad9a467b346a617f318f08479f4d194f50c6bdc

Comment by Tad Marshall [ 21/May/13 ]

Thinking about this some more, I don't think that cd() should return a value; it should throw a JavaScript exception so that user code can use try/catch to handle the error. This is more consistent with the behavior of other commands.

Comment by Tad Marshall [ 11/Mar/13 ]

That's OK with me. This will mimic the Windows behavior. We already have a pwd() command, so cd() will just be another way of doing the same thing.

Comment by Norberto Fernando Rocha Leite (Inactive) [ 11/Mar/13 ]

tad IMHO what it should not do is a print a stack trace. People usually associate this with buggy software.
Regarding the expected behavior I think it should present current dir(pwd command). If we would try to mimic the UNIX behavior we would probably be making things a bit more complex since once you connect to a database ( even though the mongo shell is just a js interpreter, people associate this as "the database") you have all set of users dimension and that might cause confusion to the "customer" if we change the current path into the home directory of the running process user.

Definitely would go for a pwd command.

Comment by Tad Marshall [ 11/Mar/13 ]

The basic error message ("field not found, expected type 2") has been around for a while (I tested 2.0.4), but the stack trace is newer than that.

The cd command is doing no checking that it has a first argument before it tries to read the first argument as a string (type 2), so it just gets a failure reading the field and reports it to the user.

The only question is what should cd() with no argument do? In the Windows command prompt, the command "cd" with no argument just displays the current working directory, the same thing that pwd does in Unix. In Unix (Linux, Mac), it changes to the home directory. Another option is to issue a friendlier error message (e.g. "The cd command takes one argument; cd(directory)" or something like that).

What do you think it should do?

Generated at Thu Feb 08 03:18:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.