chore: refactor validateSshTerm into utils.js

This commit is contained in:
Bill Church 2024-08-17 13:10:23 +00:00
parent 72d747763c
commit 82c0da0ff7
No known key found for this signature in database
2 changed files with 17 additions and 14 deletions

View file

@ -6,7 +6,7 @@ const express = require("express")
const router = express.Router()
const handleConnection = require("./connectionHandler")
const basicAuth = require("basic-auth")
const { sanitizeObject } = require("./utils")
const { sanitizeObject, validateSshTerm } = require("./utils")
const validator = require("validator")
function auth(req, res, next) {
@ -80,17 +80,4 @@ router.post("/force-reconnect", function (req, res) {
res.status(401).send("Authentication required.")
})
/**
* Validates the SSH terminal name using validator functions.
* Allows alphanumeric characters, hyphens, and periods.
* @param {string} term - The terminal name to validate
* @returns {boolean} True if the terminal name is valid, false otherwise
*/
function validateSshTerm(term) {
return (
validator.isLength(term, { min: 1, max: 30 }) &&
validator.matches(term, /^[a-zA-Z0-9.-]+$/)
)
}
module.exports = router

View file

@ -1,5 +1,6 @@
// server
// /app/utils.js
const validator = require("validator");
/**
* Sanitizes an object by replacing sensitive properties with asterisks.
@ -33,4 +34,19 @@ function sanitizeObject(
return obj
}
/**
* Validates the SSH terminal name using validator functions.
* Allows alphanumeric characters, hyphens, and periods.
* @param {string} term - The terminal name to validate
* @returns {boolean} True if the terminal name is valid, false otherwise
*/
function validateSshTerm(term) {
return (
validator.isLength(term, { min: 1, max: 30 }) &&
validator.matches(term, /^[a-zA-Z0-9.-]+$/)
)
}
exports.sanitizeObject = sanitizeObject
exports.validateSshTerm = validateSshTerm;