URSH: Switch back to RegExp-based URL validation

This commit is contained in:
Ergys Dona 2018-09-04 15:16:15 +02:00 committed by Adrian Moennich
parent 6af54aca8a
commit 6e47dc6d04
2 changed files with 17 additions and 31 deletions

View File

@ -15,7 +15,6 @@
* along with Indico; if not, see <http://www.gnu.org/licenses/>.
*/
import 'url-polyfill';
import {handleAxiosError, indicoAxios} from 'indico/utils/axios';
@ -38,7 +37,7 @@ async function _makeUrshRequest(originalURL) {
function _validateAndFormatURL(url) {
const $t = $T.domain('ursh');
if (!url) {
throw $t.gettext('Please fill in a URL to shorten');
throw Error($t.gettext('Please fill in a URL to shorten'));
}
// if protocol is missing, prepend it
@ -46,18 +45,24 @@ function _validateAndFormatURL(url) {
url = `${location.protocol}//${url}`;
}
let parsedURL;
try {
parsedURL = new URL(url);
} catch (err) {
throw $t.gettext('This does not look like a valid URL');
// regular expression, because I.E. does not like the URL class
const re = RegExp(`^(.+:)//([^/]+)(/.*)?$`);
const urlTokens = url.match(re);
if (!urlTokens) {
throw Error($t.gettext('This does not look like a valid URL'));
}
parsedURL.protocol = location.protocol; // patch protocol to match server
if (parsedURL.hostname !== location.hostname) {
throw $t.gettext('Invalid host: only Indico URLs are allowed');
// extract tokens
let protocol = urlTokens[1];
const hostname = urlTokens[2];
const path = urlTokens[3] ? urlTokens[3] : '/';
protocol = location.protocol; // patch protocol to match server
if (hostname !== location.hostname) {
throw Error($t.gettext('Invalid host: only Indico URLs are allowed'));
}
return parsedURL.href;
return `${protocol}//${hostname}${path}`;
}
function _getUrshInput() {
@ -86,7 +91,7 @@ function _getUrshInput() {
input.val(formattedURL);
return formattedURL;
} catch (err) {
tip(err);
tip(err.message);
input.focus().select();
return null;
}

View File

@ -1,19 +0,0 @@
{
"name": "indico-plugin-ursh",
"version": "1.0.0",
"description": "URL shortening service provider for Indico",
"main": "indico_ursh/client/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/indico/indico-plugins.git"
},
"author": "Indico Team <indico.team@cern.ch>",
"license": "GPL-3.0",
"bugs": {
"url": "https://github.com/indico/indico-plugins/issues"
},
"homepage": "https://github.com/indico/indico-plugins#readme",
"dependencies": {
"url-polyfill": "^1.1.0"
}
}