-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix NodeJS SecureSession handling nullptr #698
Fix NodeJS SecureSession handling nullptr #698
Conversation
Checking whether passed key is a private key is not enough since not all private keys are supported. For example, `secure_session_create()` may return NULL for RSA key. In this case, we get no exception on JS side but as soon as we try to connect to other peer (or accept connection) using created object, we get error. This commit makes sure we throw JS exception in case `secure_session_create()` returns NULL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch!
It's sad that we don't test RSA keys in JsThemis. (Nor is it mentioned anywhere in the docs...)
// secure_session_create() inside SecureSession::SecureSession() | ||
// may fail, we need to catch this and throw exception to JS | ||
ThrowParameterError("SecureSession", "unsupported private key"); | ||
args.GetReturnValue().SetUndefined(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I wanted to note that there may be other reasons for a failure here, but most of them are actually checked and reported before this call. It can still fail due to allocation errors and such but that's rare so I think this message should be fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, secure_session_create()
only gives NULL
or some valid pointer, and we can't get the actual error code in case of error.
bool isCreated() | ||
{ | ||
return session_ != nullptr; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this method can be private.
(I know, no one really cares in this case, but good habits are important.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree
Make `isCreated()` a private method
* Add Go tests * Add JS tests * Add Python tests, fix Python wrapper * Add Ruby tests, fix Ruby wrapper * Add C++ tests, fix typos * Check Secure Session constuction failure in ObjCThemis Verify that Secure Session has been constructed successfully. This should catch RSA keys being passed to the initializer earlier. * Update changelog, mention fixes of NodeJS (#698), Python, Ruby, ObjC, Swift wrappers * Update .gitignore by adding few things like Ruby `.gem` packages, Python package metadata, another Rust `target` directory The 'tests' above are: * Whether Secure Session constructor accepts EC private key * Whether Secure Session constructor rejects EC public and RSA private and RSA public keys The fixes mentioned above were wrong/missing checks whether `secure_session_create()` returned NULL. Python and Ruby were doing it wrong and didn't throw the exception while ObjC was completely missing the check. Co-authored-by: Alexei Lozovsky <[email protected]>
Checking whether passed key is a private key is not enough since not all
private keys are supported. For example,
secure_session_create()
mayreturn NULL for RSA key. In this case, we get no exception on JS side
but as soon as we try to connect to other peer (or accept connection)
using created object, we get error. This commit makes sure we throw JS
exception in case
secure_session_create()
returns NULL.Not sure whether this is the best way to do the check but AFAIK using C++ exceptions
inside NodeJS bindings may not be a good idea. Other idea was to throw something from
SecureSession::SecureSession()
and then putSecureSession* obj = new SecureSession(...);
into
try {}
block. Also, why throw the exception if it will be immediately checked afterwards,and it does not contain any useful information, just failed or succeed (no exception).
Checklist
Change is covered by automated testsThis change (obviously) does not break existing tests.
However, tests like "make sure SecureSession does now allow RSA private keys (as well as any public)"
will be done as part of different PR. And it won't be NodeJS wrapper only.