|
| 1 | +#include "secret-store.h" |
| 2 | +#include "host_call.h" |
| 3 | + |
| 4 | +namespace builtins { |
| 5 | + |
| 6 | +fastly_secret_handle_t SecretStoreEntry::secret_handle(JSObject *obj) { |
| 7 | + JS::Value val = JS::GetReservedSlot(obj, SecretStoreEntry::Slots::Handle); |
| 8 | + return static_cast<fastly_secret_handle_t>(val.toInt32()); |
| 9 | +} |
| 10 | + |
| 11 | +bool SecretStoreEntry::plaintext(JSContext *cx, unsigned argc, JS::Value *vp) { |
| 12 | + METHOD_HEADER(0) |
| 13 | + |
| 14 | + fastly_option_string_t ret; |
| 15 | + fastly_error_t err; |
| 16 | + // Ensure that we throw an exception for all unexpected host errors. |
| 17 | + if (!xqd_fastly_secret_store_plaintext(SecretStoreEntry::secret_handle(self), &ret, &err)) { |
| 18 | + HANDLE_ERROR(cx, err); |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
| 22 | + JS::RootedString text(cx, JS_NewStringCopyUTF8N(cx, JS::UTF8Chars(ret.val.ptr, ret.val.len))); |
| 23 | + JS_free(cx, ret.val.ptr); |
| 24 | + if (!text) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + args.rval().setString(text); |
| 29 | + return true; |
| 30 | +} |
| 31 | + |
| 32 | +const JSFunctionSpec SecretStoreEntry::methods[] = { |
| 33 | + JS_FN("plaintext", plaintext, 0, JSPROP_ENUMERATE), JS_FS_END}; |
| 34 | + |
| 35 | +const JSPropertySpec SecretStoreEntry::properties[] = {JS_PS_END}; |
| 36 | + |
| 37 | +bool SecretStoreEntry::constructor(JSContext *cx, unsigned argc, JS::Value *vp) { |
| 38 | + JS_ReportErrorUTF8(cx, "SecretStoreEntry can't be instantiated directly"); |
| 39 | + return false; |
| 40 | +} |
| 41 | + |
| 42 | +JSObject *SecretStoreEntry::create(JSContext *cx, fastly_secret_handle_t handle) { |
| 43 | + JS::RootedObject entry( |
| 44 | + cx, JS_NewObjectWithGivenProto(cx, &SecretStoreEntry::class_, SecretStoreEntry::proto_obj)); |
| 45 | + if (!entry) { |
| 46 | + return nullptr; |
| 47 | + } |
| 48 | + |
| 49 | + JS::SetReservedSlot(entry, Slots::Handle, JS::Int32Value(handle)); |
| 50 | + |
| 51 | + return entry; |
| 52 | +} |
| 53 | + |
| 54 | +bool SecretStoreEntry::init_class(JSContext *cx, JS::HandleObject global) { |
| 55 | + return BuiltinImpl<SecretStoreEntry>::init_class_impl(cx, global); |
| 56 | +} |
| 57 | + |
| 58 | +fastly_secret_store_handle_t SecretStore::secret_store_handle(JSObject *obj) { |
| 59 | + JS::Value val = JS::GetReservedSlot(obj, SecretStore::Slots::Handle); |
| 60 | + return static_cast<fastly_secret_store_handle_t>(val.toInt32()); |
| 61 | +} |
| 62 | + |
| 63 | +bool SecretStore::get(JSContext *cx, unsigned argc, JS::Value *vp) { |
| 64 | + METHOD_HEADER(1) |
| 65 | + |
| 66 | + JS::RootedObject result_promise(cx, JS::NewPromiseObject(cx, nullptr)); |
| 67 | + if (!result_promise) { |
| 68 | + return ReturnPromiseRejectedWithPendingError(cx, args); |
| 69 | + } |
| 70 | + |
| 71 | + size_t length; |
| 72 | + JS::UniqueChars key = encode(cx, args[0], &length); |
| 73 | + if (!key) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + // If the converted string has a length of 0 then we throw an Error |
| 77 | + // because keys have to be at-least 1 character. |
| 78 | + if (length == 0) { |
| 79 | + JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_SECRET_STORE_KEY_EMPTY); |
| 80 | + return ReturnPromiseRejectedWithPendingError(cx, args); |
| 81 | + } |
| 82 | + |
| 83 | + // key has to be less than 256 |
| 84 | + if (length > 255) { |
| 85 | + JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_SECRET_STORE_KEY_TOO_LONG); |
| 86 | + return ReturnPromiseRejectedWithPendingError(cx, args); |
| 87 | + } |
| 88 | + |
| 89 | + std::string_view keyView(key.get(), length); |
| 90 | + |
| 91 | + // key must contain only letters, numbers, dashes (-), underscores (_), and periods (.). |
| 92 | + auto is_valid_key = std::all_of(keyView.begin(), keyView.end(), [&](auto character) { |
| 93 | + return std::isalnum(character) || character == '_' || character == '-' || character == '.'; |
| 94 | + }); |
| 95 | + |
| 96 | + if (!is_valid_key) { |
| 97 | + JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 98 | + JSMSG_SECRET_STORE_KEY_CONTAINS_INVALID_CHARACTER); |
| 99 | + return ReturnPromiseRejectedWithPendingError(cx, args); |
| 100 | + } |
| 101 | + |
| 102 | + xqd_world_string_t key_str; |
| 103 | + key_str.len = length; |
| 104 | + key_str.ptr = key.get(); |
| 105 | + fastly_option_secret_handle_t secret; |
| 106 | + fastly_error_t err; |
| 107 | + // Ensure that we throw an exception for all unexpected host errors. |
| 108 | + if (!xqd_fastly_secret_store_get(SecretStore::secret_store_handle(self), &key_str, &secret, |
| 109 | + &err)) { |
| 110 | + HANDLE_ERROR(cx, err); |
| 111 | + return ReturnPromiseRejectedWithPendingError(cx, args); |
| 112 | + } |
| 113 | + |
| 114 | + // When no entry is found, we are going to resolve the Promise with `null`. |
| 115 | + if (!secret.is_some) { |
| 116 | + JS::RootedValue result(cx); |
| 117 | + result.setNull(); |
| 118 | + JS::ResolvePromise(cx, result_promise, result); |
| 119 | + } else { |
| 120 | + JS::RootedObject entry(cx, SecretStoreEntry::create(cx, secret.val)); |
| 121 | + if (!entry) { |
| 122 | + return ReturnPromiseRejectedWithPendingError(cx, args); |
| 123 | + } |
| 124 | + JS::RootedValue result(cx); |
| 125 | + result.setObject(*entry); |
| 126 | + JS::ResolvePromise(cx, result_promise, result); |
| 127 | + } |
| 128 | + |
| 129 | + args.rval().setObject(*result_promise); |
| 130 | + |
| 131 | + return true; |
| 132 | +} |
| 133 | + |
| 134 | +const JSFunctionSpec SecretStore::methods[] = {JS_FN("get", get, 1, JSPROP_ENUMERATE), JS_FS_END}; |
| 135 | + |
| 136 | +const JSPropertySpec SecretStore::properties[] = {JS_PS_END}; |
| 137 | + |
| 138 | +bool SecretStore::constructor(JSContext *cx, unsigned argc, JS::Value *vp) { |
| 139 | + REQUEST_HANDLER_ONLY("The SecretStore builtin"); |
| 140 | + CTOR_HEADER("SecretStore", 1); |
| 141 | + |
| 142 | + size_t length; |
| 143 | + JS::UniqueChars name_chars = encode(cx, args[0], &length); |
| 144 | + if (!name_chars) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + // If the converted string has a length of 0 then we throw an Error |
| 149 | + // because names have to be at-least 1 character. |
| 150 | + if (length == 0) { |
| 151 | + JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_SECRET_STORE_NAME_EMPTY); |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + // If the converted string has a length of more than 255 then we throw an Error |
| 156 | + // because names have to be less than 255 characters. |
| 157 | + if (length > 255) { |
| 158 | + JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_SECRET_STORE_NAME_TOO_LONG); |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + std::string_view name(name_chars.get(), length); |
| 163 | + |
| 164 | + // Name must contain only letters, numbers, dashes (-), underscores (_), and periods (.). |
| 165 | + auto is_valid_name = std::all_of(name.begin(), name.end(), [&](auto character) { |
| 166 | + return std::isalnum(character) || character == '_' || character == '-' || character == '.'; |
| 167 | + }); |
| 168 | + |
| 169 | + if (!is_valid_name) { |
| 170 | + JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 171 | + JSMSG_SECRET_STORE_NAME_CONTAINS_INVALID_CHARACTER); |
| 172 | + return false; |
| 173 | + } |
| 174 | + |
| 175 | + JS::RootedObject secret_store(cx, JS_NewObjectForConstructor(cx, &class_, args)); |
| 176 | + if (!secret_store) { |
| 177 | + return false; |
| 178 | + } |
| 179 | + xqd_world_string_t name_str; |
| 180 | + name_str.ptr = name_chars.get(); |
| 181 | + name_str.len = length; |
| 182 | + fastly_secret_store_handle_t handle = INVALID_HANDLE; |
| 183 | + fastly_error_t err; |
| 184 | + if (!xqd_fastly_secret_store_open(&name_str, &handle, &err)) { |
| 185 | + if (err == FASTLY_ERROR_OPTIONAL_NONE) { |
| 186 | + JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_SECRET_STORE_DOES_NOT_EXIST, |
| 187 | + name.data()); |
| 188 | + return false; |
| 189 | + } else { |
| 190 | + HANDLE_ERROR(cx, err); |
| 191 | + return false; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + JS::SetReservedSlot(secret_store, SecretStore::Slots::Handle, JS::Int32Value(handle)); |
| 196 | + args.rval().setObject(*secret_store); |
| 197 | + return true; |
| 198 | +} |
| 199 | + |
| 200 | +bool SecretStore::init_class(JSContext *cx, JS::HandleObject global) { |
| 201 | + return BuiltinImpl<SecretStore>::init_class_impl(cx, global); |
| 202 | +} |
| 203 | + |
| 204 | +} // namespace builtins |
0 commit comments