Commit 4a1fe233 authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

FormData: Fix a crash by passing null or non-HTMLFormElement instances to a FormData constructor

Add back nullptr check to FormData::Create(HTMLFormElement*).

This CL fixes a regression caused by crrev.com/609210.
According to standards, |new FormData(nullptr)| and |new
FormData("string")| should throw TypeErrors. However this CL applies the
behavior before crrev.com/609210.

Bug: 906649
Change-Id: I78aa53559592ef0e14cc941175dc9f1b44342bb0
Reviewed-on: https://chromium-review.googlesource.com/c/1343414Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Commit-Queue: Kent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609614}
parent 69c7807c
This is a testharness.js-based test.
FAIL Constructors assert_throws: function "() => { new FormData(null); }" did not throw
PASS empty formdata
PASS formdata with string
PASS formdata with named string
PASS formdata from form
Harness: the test ran to completion.
<!doctype html> <!doctype html>
<html lang=en> <html lang=en>
<meta charset=utf-8> <meta charset=utf-8>
<title>XMLHttpRequest: upload formdata</title> <title>XMLHttpRequest: Construct and upload FormData</title>
<script src="/resources/testharness.js"></script> <script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script> <script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://xhr.spec.whatwg.org/#interface-formdata" data-tested-assertations="following::P[1]" /> <link rel="help" href="https://xhr.spec.whatwg.org/#interface-formdata" data-tested-assertations="following::P[1]" />
...@@ -35,6 +35,11 @@ ...@@ -35,6 +35,11 @@
return fd; return fd;
} }
test(() => {
assert_throws(new TypeError(), () => { new FormData(null); });
assert_throws(new TypeError(), () => { new FormData("string"); });
}, "Constructors");
do_test("empty formdata", new FormData(), '\n'); do_test("empty formdata", new FormData(), '\n');
do_test("formdata with string", create_formdata(['key', 'value']), 'key=value,\n'); do_test("formdata with string", create_formdata(['key', 'value']), 'key=value,\n');
do_test("formdata with named string", create_formdata(['key', new Blob(['value'], {type: 'text/plain'}), 'kv.txt']), '\nkey=kv.txt:text/plain:5,'); do_test("formdata with named string", create_formdata(['key', new Blob(['value'], {type: 'text/plain'}), 'kv.txt']), '\nkey=kv.txt:text/plain:5,');
......
...@@ -91,8 +91,11 @@ FormData::FormData() : encoding_(UTF8Encoding()) {} ...@@ -91,8 +91,11 @@ FormData::FormData() : encoding_(UTF8Encoding()) {}
FormData* FormData::Create(HTMLFormElement* form, FormData* FormData::Create(HTMLFormElement* form,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(form);
auto* form_data = new FormData(); auto* form_data = new FormData();
// TODO(tkent): Null check should be unnecessary. We should remove
// LegacyInterfaceTypeChecking from form_data.idl. crbug.com/561338
if (!form)
return form_data;
if (!form->ConstructEntryList(nullptr, *form_data)) { if (!form->ConstructEntryList(nullptr, *form_data)) {
DCHECK(RuntimeEnabledFeatures::FormDataEventEnabled()); DCHECK(RuntimeEnabledFeatures::FormDataEventEnabled());
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError, exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment