Skip to content

Commit 3945a0d

Browse files
adrian-niculescuAdrian Niculescu
authored andcommitted
fix: coerce append/set arguments to USVStrings
Completes the review follow-up: append() and set() now run both arguments through the same USVString coercion as the rest of the binding, instead of casting them to strings blindly. A number/boolean/object argument coerces via toString; a Symbol (or a throwing toString) raises a TypeError before anything is appended or replaced.
1 parent 1a0ae12 commit 3945a0d

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

test-app/app/src/main/assets/app/tests/testURLSearchParamsImpl.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,26 @@ describe("Test URLSearchParams ", function () {
443443
expect(params.get("a")).toBe("1");
444444
});
445445

446+
it("Test URLSearchParams coerces non-string arguments in append and set", function(){
447+
const params = new URLSearchParams();
448+
params.append(1, 2);
449+
expect(params.get("1")).toBe("2");
450+
params.set(true, { toString: function(){ return "x"; } });
451+
expect(params.get("true")).toBe("x");
452+
params.append("a", null);
453+
expect(params.get("a")).toBe("null");
454+
});
455+
456+
it("Test URLSearchParams append and set throw when an argument is a Symbol", function(){
457+
const params = new URLSearchParams("a=1");
458+
expect(function(){ params.append(Symbol("x"), "v"); }).toThrow();
459+
expect(function(){ params.append("k", Symbol("x")); }).toThrow();
460+
expect(function(){ params.set(Symbol("x"), "v"); }).toThrow();
461+
expect(function(){ params.set("k", Symbol("x")); }).toThrow();
462+
// Nothing was appended or replaced by the failed calls.
463+
expect(params.toString()).toBe("a=1");
464+
});
465+
446466
// --- Brand checks: iterators require a genuine receiver. ---
447467

448468
it("Test URLSearchParams entries/keys/values throw on a foreign receiver", function(){

test-app/runtime/src/main/cpp/URLSearchParamsImpl.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,16 @@ namespace tns {
543543
if (ptr == nullptr) {
544544
return;
545545
}
546-
auto key = ArgConverter::ConvertToString(args[0].As<v8::String>());
547-
auto value = ArgConverter::ConvertToString(args[1].As<v8::String>());
548-
ptr->GetURLSearchParams()->append(key.c_str(), value.c_str());
546+
// Both arguments are USVStrings (url.bs:3860); a Symbol or throwing
547+
// toString leaves the pending exception and aborts.
548+
auto context = args.GetIsolate()->GetCurrentContext();
549+
std::string key;
550+
std::string value;
551+
if (!ValueToString(context, args[0], key) ||
552+
!ValueToString(context, args[1], value)) {
553+
return;
554+
}
555+
ptr->GetURLSearchParams()->append(key, value);
549556
}
550557

551558
void URLSearchParamsImpl::Delete(const v8::FunctionCallbackInfo<v8::Value> &args) {
@@ -708,13 +715,16 @@ namespace tns {
708715
if (ptr == nullptr) {
709716
return;
710717
}
711-
auto key = args[0].As<v8::String>();
712-
auto value = args[1].As<v8::String>();
713-
714-
ptr->GetURLSearchParams()->set(
715-
ArgConverter::ConvertToString(key),
716-
ArgConverter::ConvertToString(value)
717-
);
718+
// Both arguments are USVStrings (url.bs:3865); a Symbol or throwing
719+
// toString leaves the pending exception and aborts.
720+
auto context = args.GetIsolate()->GetCurrentContext();
721+
std::string key;
722+
std::string value;
723+
if (!ValueToString(context, args[0], key) ||
724+
!ValueToString(context, args[1], value)) {
725+
return;
726+
}
727+
ptr->GetURLSearchParams()->set(key, value);
718728
}
719729

720730
void URLSearchParamsImpl::GetSize(v8::Local<v8::String> property,

0 commit comments

Comments
 (0)