Skip to content
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

Conversation

iamnotacake
Copy link
Contributor

@iamnotacake iamnotacake commented Aug 13, 2020

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.

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 put SecureSession* 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 tests
    This 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.
  • The coding guidelines are followed

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.
@iamnotacake iamnotacake added the W-JsThemis 🍭 Wrapper: JsThemis for Node.js, JavaScript API, npm packages label Aug 13, 2020
Copy link
Collaborator

@ilammy ilammy left a 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...)

Comment on lines +150 to +153
// 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();
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Comment on lines 34 to 37
bool isCreated()
{
return session_ != nullptr;
}
Copy link
Collaborator

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.)

Copy link
Contributor

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
@iamnotacake iamnotacake merged commit ff420fd into cossacklabs:master Aug 13, 2020
@iamnotacake iamnotacake deleted the anatolii-T1750-js-securesession-handle-null branch August 13, 2020 14:36
iamnotacake added a commit that referenced this pull request Aug 19, 2020
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
W-JsThemis 🍭 Wrapper: JsThemis for Node.js, JavaScript API, npm packages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants