@@ -106,32 +106,42 @@ ObjectStoreHandle object_store_handle(JSObject *obj) {
106
106
107
107
const unsigned ctor_length = 1 ;
108
108
109
- std::optional<char *> parse_and_validate_key (JSContext *cx, JS::HandleValue val, size_t *key_len) {
109
+ std::optional<std::string> parse_and_validate_key (JSContext *cx, JS::HandleValue val) {
110
+ size_t key_len;
110
111
// Convert the key argument into a String following https://tc39.es/ecma262/#sec-tostring
111
- JS::UniqueChars key = encode (cx, val, key_len);
112
- if (!key ) {
112
+ JS::UniqueChars keyString = encode (cx, val, & key_len);
113
+ if (!keyString ) {
113
114
return std::nullopt;
114
115
}
115
116
116
117
// If the converted string has a length of 0 then we throw an Error
117
118
// because ObjectStore Keys have to be at-least 1 character.
118
- if (* key_len == 0 ) {
119
+ if (key_len == 0 ) {
119
120
JS_ReportErrorNumberASCII (cx, GetErrorMessage, nullptr , JSMSG_OBJECT_STORE_KEY_EMPTY);
120
121
return std::nullopt;
121
122
}
122
123
123
124
// If the converted string has a length of more than 1024 then we throw an Error
124
125
// because ObjectStore Keys have to be less than 1025 characters.
125
- if (* key_len > 1024 ) {
126
+ if (key_len > 1024 ) {
126
127
JS_ReportErrorNumberASCII (cx, GetErrorMessage, nullptr , JSMSG_OBJECT_STORE_KEY_TOO_LONG);
127
128
return std::nullopt;
128
129
}
129
130
130
- char *key_chars = key.get ();
131
+ std::string key (keyString.get (), key_len);
132
+ auto key_chars = key.c_str ();
131
133
132
134
if (auto res = find_invalid_character_for_object_store_key (key_chars)) {
133
- JS_ReportErrorNumberASCII (cx, GetErrorMessage, nullptr ,
134
- JSMSG_OBJECT_STORE_KEY_INVALID_CHARACTER, *res);
135
+ if (*res == ' \n ' ) {
136
+ JS_ReportErrorNumberASCII (cx, GetErrorMessage, nullptr ,
137
+ JSMSG_OBJECT_STORE_KEY_INVALID_CHARACTER, " newline" );
138
+ } else if (*res == ' \r ' ) {
139
+ JS_ReportErrorNumberASCII (cx, GetErrorMessage, nullptr ,
140
+ JSMSG_OBJECT_STORE_KEY_INVALID_CHARACTER, " carriage return" );
141
+ } else {
142
+ JS_ReportErrorNumberASCII (cx, GetErrorMessage, nullptr ,
143
+ JSMSG_OBJECT_STORE_KEY_INVALID_CHARACTER, res);
144
+ }
135
145
return std::nullopt;
136
146
}
137
147
auto acme_challenge = " .well-known/acme-challenge/" ;
@@ -145,7 +155,7 @@ std::optional<char *> parse_and_validate_key(JSContext *cx, JS::HandleValue val,
145
155
return std::nullopt;
146
156
}
147
157
148
- return key_chars ;
158
+ return key ;
149
159
}
150
160
151
161
bool check_receiver (JSContext *cx, JS::HandleValue receiver, const char *method_name);
@@ -158,14 +168,14 @@ bool get(JSContext *cx, unsigned argc, JS::Value *vp) {
158
168
return ReturnPromiseRejectedWithPendingError (cx, args);
159
169
}
160
170
161
- size_t key_len ;
162
- std::optional< char *> key_chars = parse_and_validate_key (cx, args. get ( 0 ), &key_len );
171
+ JS::RootedValue key (cx, args. get ( 0 )) ;
172
+ auto key_chars = parse_and_validate_key (cx, key );
163
173
if (!key_chars) {
164
174
return ReturnPromiseRejectedWithPendingError (cx, args);
165
175
}
166
176
BodyHandle body_handle = {INVALID_HANDLE};
167
- int status =
168
- fastly_object_store_get ( object_store_handle (self), key_chars.value (), key_len , &body_handle);
177
+ int status = fastly_object_store_get ( object_store_handle (self), key_chars. value (). c_str (),
178
+ key_chars.value (). length () , &body_handle);
169
179
if (!HANDLE_RESULT (cx, status)) {
170
180
return false ;
171
181
}
@@ -198,8 +208,8 @@ bool put(JSContext *cx, unsigned argc, JS::Value *vp) {
198
208
return ReturnPromiseRejectedWithPendingError (cx, args);
199
209
}
200
210
201
- size_t key_len ;
202
- std::optional< char *> key_chars = parse_and_validate_key (cx, args. get ( 0 ), &key_len );
211
+ JS::RootedValue key (cx, args. get ( 0 )) ;
212
+ auto key_chars = parse_and_validate_key (cx, key );
203
213
if (!key_chars) {
204
214
return ReturnPromiseRejectedWithPendingError (cx, args);
205
215
}
@@ -232,8 +242,8 @@ bool put(JSContext *cx, unsigned argc, JS::Value *vp) {
232
242
JS::RootedObject source_owner (cx, builtins::NativeStreamSource::owner (stream_source));
233
243
BodyHandle body = RequestOrResponse::body_handle (source_owner);
234
244
235
- int status =
236
- fastly_object_store_insert ( object_store_handle (self), key_chars.value (), key_len , body);
245
+ int status = fastly_object_store_insert ( object_store_handle (self), key_chars. value (). c_str (),
246
+ key_chars.value (). length () , body);
237
247
if (!HANDLE_RESULT (cx, status)) {
238
248
return ReturnPromiseRejectedWithPendingError (cx, args);
239
249
}
@@ -308,8 +318,8 @@ bool put(JSContext *cx, unsigned argc, JS::Value *vp) {
308
318
return ReturnPromiseRejectedWithPendingError (cx, args);
309
319
}
310
320
311
- int status = fastly_object_store_insert (object_store_handle (self), key_chars.value (), key_len ,
312
- body_handle);
321
+ int status = fastly_object_store_insert (object_store_handle (self), key_chars.value (). c_str () ,
322
+ key_chars. value (). length (), body_handle);
313
323
// Ensure that we throw an exception for all unexpected host errors.
314
324
if (!HANDLE_RESULT (cx, status)) {
315
325
return RejectPromiseWithPendingError (cx, result_promise);
0 commit comments