luci-base: properly handle promise targets in Request.request()

Under some circumstances, ubus RPC requests may be initiated while LuCI is
still resolving the `rpcBaseURL` value. In this situation, the `target`
argument of the `request()` call will be a pending promise object which
results in an invalid URL when serialized by `expandURL()`, leading to an
`Failed to execute 'open' on 'XMLHttpRequest': Invalid URL` exception.

This commonly occured on the index status page which immediately initiates
ubus RPC calls on load to discover existing status page partials.

Solve the issue by filtering the given `target` argument through
`Promise.resolve()` before expanding the URL and initiating the actual
request.

Fixes: #3747
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(cherry picked from commit 5663fd596b)
This commit is contained in:
Jo-Philipp Wich 2022-02-21 14:59:16 +01:00
parent cc582ebfb3
commit 31a27f3087

View file

@ -695,7 +695,8 @@
* The resulting HTTP response.
*/
request: function(target, options) {
var state = { xhr: new XMLHttpRequest(), url: this.expandURL(target), start: Date.now() },
return Promise.resolve(target).then((function(url) {
var state = { xhr: new XMLHttpRequest(), url: this.expandURL(url), start: Date.now() },
opt = Object.assign({}, options, state),
content = null,
contenttype = null,
@ -804,6 +805,7 @@
rejectFn.call(opt, e);
}
});
}).bind(this));
},
handleReadyStateChange: function(resolveFn, rejectFn, ev) {