# User API keys should use OAEP padding

**URL:** https://meta.discourse.org/t/user-api-keys-should-use-oaep-padding/354056
**Category:** Feature
**Tags:** user-api
**Created:** [June 1, 2024, 7:00am UTC](https://meta.discourse.org/t/user-api-keys-should-use-oaep-padding/354056 "2024-06-01T07:00:36Z")
**Posts on this page:** 1
**Showing post:** 1

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [June 1, 2024, 7:00am UTC](https://meta.discourse.org/t/user-api-keys-should-use-oaep-padding/354056/1 "2024-06-01T07:00:36Z")

</div>

A minor issue and maybe a bigger one:

There’s a required `nonce` param that’s not mentioned in the documentation:

```ruby
  def require_params
    %i[public_key nonce scopes client_id application_name].each { |p| params.require(p) }
  end

```

Now the trickier issue. Discourse calls the `public_encrypt` method with no arguments:

> <https://github.com/discourse/discourse/blob/main/app/controllers/user_api_keys_controller.rb#L84>

That means the `padding` argument defaults to `PKCS1_PADDING`. From the [Ruby documentation](https://docs.ruby-lang.org/en/3.2/OpenSSL/PKey/RSA.html):

> Encrypt `string` with the public key. `padding` defaults to [`PKCS1_PADDING`](https://docs.ruby-lang.org/en/3.2/OpenSSL/PKey/RSA.html#PKCS1_PADDING), which is known to be insecure but is kept for backwards compatibility.

Unfortunately, Node v20.14.0 (the current LTS) returns an error if you attempt to call `crypto.privateDecrypt` with `RSA_PKCS1_PADDING `:

```javascript
function decryptData(data: string, privateKey: string) {
  const buffer = Buffer.from(data, "base64");
  const decrypted = crypto.privateDecrypt(
    {
      key: privateKey,
      padding: crypto.constants.RSA_PKCS1_PADDING,
    },
    buffer
  );
  return decrypted.toString("utf8");
}

```

> TypeError: RSA\_PKCS1\_PADDING is no longer supported for private decryption, this can be reverted with --security-revert=CVE-2023-46809

A possible fix for Node apps is to run Node with the insecure flag:

```plaintext
node --security-revert=CVE-2023-46809 

```

A fix on the Discourse end would be easy, but I suspect it would break a lot of existing integrations:

```ruby
public_key = OpenSSL::PKey::RSA.new(params[:public_key])
@payload = Base64.encode64(public_key.public_encrypt(@payload, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING))

```

---

_[View the full topic](https://meta.discourse.org/t/user-api-keys-should-use-oaep-padding/354056)._
