镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Feature Request: Safe/reliable crypto.KeyObject and webcrypto.CryptoKey detection in userland #38611

Description

@mscdex

Currently there are no public methods for checking for KeyObject and CryptoKey objects. Having reliable checks for these would be very nice to have for userland code that wants to support these types of objects in their code.

It seems there are already internal methods for achieving this, but I'm not sure why they are not currently exposed to userland. I was thinking maybe something like how Buffer handles this: KeyObject.isKeyObject() and CryptoKey.isCryptoKey(). The former is currently pretty easy to do in userland since it's just doing an instanceof check, but the latter checks "private" symbols, which is not as easy/ideal for userland (although I guess at least currently instanceof CryptoKey would work?). Either way, having these encapsulated in helper methods would help insulate changes to the checking logic, should it ever change.

Activity

  1. added
    cryptoIssues and PRs related to the crypto subsystem.
    feature requestIssues requesting new Node.js features.
    on May 9, 2021
  2. tniessen commented on May 9, 2021

    @tniessen
    Member

    Re KeyObject: When I designed the new APIs, I intentionally did not expose that class directly. The only reason we did so eventually is such that users can rely on instanceof KeyObject.

  3. tniessen commented on May 9, 2021

    @tniessen
    Member

    See #26200 and #26438.

  4. mscdex commented on May 9, 2021

    @mscdex
    ContributorAuthor

    I still think it would be a good idea to expose static .is*() methods in case the method of detection should change in the future (e.g. instanceof to symbol check) as with Buffer.isBuffer() (where the detection method did change over time).

  5. panva commented on May 10, 2021

    @panva
    Member

    Both instanceof crypto.KeyObject and instanceof crypto.webcrypto.CryptoKey work so long as the code doesn't run e.g. in a vm context.

    I think exposing a symbol check function would be better.

  6. jasnell commented on May 10, 2021

    @jasnell
    Member

    CryptoKey.isCryptoKey() is problematic because it extends the standard API in a non-standard way. As I suggest in @panva's PR, we could add both checks to KeyObject:

    KeyObject.isKeyObject()
    KeyObject.isCryptoKey()

    Alternatively, we could add both to util/types

    const { isKeyObject, isCryptoKey } = require('util/types');
    
    isKeyObject(obj)
    isCryptoKey(obj)
    

    If the Node.js binary is built without crypto support, these would still exist but would always return false.

  7. mscdex commented on May 10, 2021

    @mscdex
    ContributorAuthor

    I don't have a strong preference where the functions live, just that they're available in some sensible place.

  8. added a commit that references this issue on May 18, 2021
    fbf02e3
  9. added a commit that references this issue on May 22, 2026
    3ee1f9a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cryptoIssues and PRs related to the crypto subsystem.feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions