Files
threshold-ui/ui/desktop/electron-app/src/utils/sanitizer.js
T
hashicorp-copywrite[bot]andCameron Perera 3d610427d0 [COMPLIANCE] Add Copyright and License Headers (#1617)
* [COMPLIANCE] Add Copyright and License Headers

* chore: 🤖 update Copyright headers to satisfy our lint rules

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Cameron Perera <cameron.perera@hashicorp.com>
2023-03-01 09:39:41 -06:00

36 lines
844 B
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
const { URL } = require('url');
module.exports = {
/**
* Super paranoid shell quote/escape and validation. Input must be base62.
* @param {string} str
*/
base62EscapeAndValidate: (str) => {
const candidate = str.toString();
if (candidate.match(/^[A-Za-z0-9_]*$/)) return candidate;
throw new Error(`
Could not invoke command:
input contained unsafe characters.
`);
},
/**
* Return a quoted string suitable for using in shell commands.
* @param {string} str
* @returns {string}
*/
urlValidate: (str) => {
try {
const url = new URL(str);
return `${url.protocol}//${url.host}`;
} catch (e) {
// Parse error
throw new Error(`URL ${str} could not be validated.`);
}
},
};