pangolin/server/lib/validators.test.ts

335 lines
9.3 KiB
TypeScript

import {
getResourceRuleValueValidationError,
isValidDomain,
isValidUrlGlobPattern,
parseHttpMethodList
} from "./validators";
import { test } from "node:test";
import assert from "node:assert/strict";
test("domain validation", () => {
assert.equal(
isValidDomain("example.com"),
true,
"Standard ASCII domain should be valid"
);
assert.equal(
isValidDomain("xn--e1afmkfd.xn--p1ai"),
true,
"Punycode IDN domain should be valid"
);
assert.equal(
isValidDomain("example.invalid-tld"),
false,
"Domain with unknown TLD should be invalid"
);
});
test("URL pattern validation", () => {
// Test valid patterns
assert.equal(
isValidUrlGlobPattern("simple"),
true,
"Simple path segment should be valid"
);
assert.equal(
isValidUrlGlobPattern("simple/path"),
true,
"Simple path with slash should be valid"
);
assert.equal(
isValidUrlGlobPattern("/leading/slash"),
true,
"Path with leading slash should be valid"
);
assert.equal(
isValidUrlGlobPattern("path/"),
true,
"Path with trailing slash should be valid"
);
assert.equal(
isValidUrlGlobPattern("path/*"),
true,
"Path with wildcard segment should be valid"
);
assert.equal(
isValidUrlGlobPattern("*"),
true,
"Single wildcard should be valid"
);
assert.equal(
isValidUrlGlobPattern("*/subpath"),
true,
"Wildcard with subpath should be valid"
);
assert.equal(
isValidUrlGlobPattern("path/*/more"),
true,
"Path with wildcard in the middle should be valid"
);
// Test with special characters
assert.equal(
isValidUrlGlobPattern("path-with-dash"),
true,
"Path with dash should be valid"
);
assert.equal(
isValidUrlGlobPattern("path_with_underscore"),
true,
"Path with underscore should be valid"
);
assert.equal(
isValidUrlGlobPattern("path.with.dots"),
true,
"Path with dots should be valid"
);
assert.equal(
isValidUrlGlobPattern("path~with~tilde"),
true,
"Path with tilde should be valid"
);
assert.equal(
isValidUrlGlobPattern("path!with!exclamation"),
true,
"Path with exclamation should be valid"
);
assert.equal(
isValidUrlGlobPattern("path$with$dollar"),
true,
"Path with dollar should be valid"
);
assert.equal(
isValidUrlGlobPattern("path&with&ampersand"),
true,
"Path with ampersand should be valid"
);
assert.equal(
isValidUrlGlobPattern("path'with'quote"),
true,
"Path with quote should be valid"
);
assert.equal(
isValidUrlGlobPattern("path(with)parentheses"),
true,
"Path with parentheses should be valid"
);
assert.equal(
isValidUrlGlobPattern("path+with+plus"),
true,
"Path with plus should be valid"
);
assert.equal(
isValidUrlGlobPattern("path,with,comma"),
true,
"Path with comma should be valid"
);
assert.equal(
isValidUrlGlobPattern("path;with;semicolon"),
true,
"Path with semicolon should be valid"
);
assert.equal(
isValidUrlGlobPattern("path=with=equals"),
true,
"Path with equals should be valid"
);
assert.equal(
isValidUrlGlobPattern("path:with:colon"),
true,
"Path with colon should be valid"
);
assert.equal(
isValidUrlGlobPattern("path@with@at"),
true,
"Path with at should be valid"
);
// Test with percent encoding
assert.equal(
isValidUrlGlobPattern("path%20with%20spaces"),
true,
"Path with percent-encoded spaces should be valid"
);
assert.equal(
isValidUrlGlobPattern("path%2Fwith%2Fencoded%2Fslashes"),
true,
"Path with percent-encoded slashes should be valid"
);
// Test with wildcards in segments (the fixed functionality)
assert.equal(
isValidUrlGlobPattern("padbootstrap*"),
true,
"Path with wildcard at the end of segment should be valid"
);
assert.equal(
isValidUrlGlobPattern("pad*bootstrap"),
true,
"Path with wildcard in the middle of segment should be valid"
);
assert.equal(
isValidUrlGlobPattern("*bootstrap"),
true,
"Path with wildcard at the start of segment should be valid"
);
assert.equal(
isValidUrlGlobPattern("multiple*wildcards*in*segment"),
true,
"Path with multiple wildcards in segment should be valid"
);
assert.equal(
isValidUrlGlobPattern("wild*/cards/in*/different/seg*ments"),
true,
"Path with wildcards in different segments should be valid"
);
// Test invalid patterns
assert.equal(
isValidUrlGlobPattern(""),
false,
"Empty string should be invalid"
);
assert.equal(
isValidUrlGlobPattern("//double/slash"),
false,
"Path with double slash should be invalid"
);
assert.equal(
isValidUrlGlobPattern("path//end"),
false,
"Path with double slash in the middle should be invalid"
);
assert.equal(
isValidUrlGlobPattern("invalid<char>"),
false,
"Path with invalid characters should be invalid"
);
assert.equal(
isValidUrlGlobPattern("invalid|char"),
false,
"Path with invalid pipe character should be invalid"
);
assert.equal(
isValidUrlGlobPattern('invalid"char'),
false,
"Path with invalid quote character should be invalid"
);
assert.equal(
isValidUrlGlobPattern("invalid`char"),
false,
"Path with invalid backtick character should be invalid"
);
assert.equal(
isValidUrlGlobPattern("invalid^char"),
false,
"Path with invalid caret character should be invalid"
);
assert.equal(
isValidUrlGlobPattern("invalid\\char"),
false,
"Path with invalid backslash character should be invalid"
);
assert.equal(
isValidUrlGlobPattern("invalid[char]"),
false,
"Path with invalid square brackets should be invalid"
);
assert.equal(
isValidUrlGlobPattern("invalid{char}"),
false,
"Path with invalid curly braces should be invalid"
);
// Test invalid percent encoding
assert.equal(
isValidUrlGlobPattern("invalid%2"),
false,
"Path with incomplete percent encoding should be invalid"
);
assert.equal(
isValidUrlGlobPattern("invalid%GZ"),
false,
"Path with invalid hex in percent encoding should be invalid"
);
assert.equal(
isValidUrlGlobPattern("invalid%"),
false,
"Path with isolated percent sign should be invalid"
);
// ASN validation tests
assert.equal(
getResourceRuleValueValidationError("ASN", "AS15169"),
null,
"Standard ASN should be valid"
);
assert.equal(
getResourceRuleValueValidationError("ASN", " As15169 "),
null,
"Standard ASN should be valid with mixed case and whitespace"
);
assert.equal(
getResourceRuleValueValidationError("ASN", "ALL"),
null,
"ALL ASN selector should be valid"
);
assert.equal(
getResourceRuleValueValidationError("ASN", " all "),
null,
"ALL ASN selector should be valid with mixed case and whitespace"
);
assert.equal(
getResourceRuleValueValidationError("ASN", "AS0"),
null,
"AS0 alias should be valid"
);
assert.equal(
getResourceRuleValueValidationError("ASN", " as0 "),
null,
"AS0 alias should be valid with mixed case and whitespace"
);
assert.equal(
getResourceRuleValueValidationError("ASN", "not-an-asn"),
"Invalid ASN provided",
"Invalid ASN should return an error"
);
// HTTP method validation tests
assert.equal(
getResourceRuleValueValidationError("METHOD", "POST"),
null,
"Single HTTP method should be valid"
);
assert.equal(
getResourceRuleValueValidationError("METHOD", " post , Put "),
null,
"Method list should be valid with mixed case and whitespace"
);
assert.equal(
getResourceRuleValueValidationError("METHOD", "PROPFIND"),
null,
"Extension methods such as the WebDAV verbs should be valid"
);
assert.equal(
getResourceRuleValueValidationError("METHOD", ""),
"Invalid HTTP method provided",
"Empty method list should return an error"
);
assert.equal(
getResourceRuleValueValidationError("METHOD", ",,"),
"Invalid HTTP method provided",
"Method list of only separators should return an error"
);
assert.equal(
getResourceRuleValueValidationError("METHOD", "GET POST"),
"Invalid HTTP method provided",
"Space separated methods should return an error"
);
assert.deepEqual(
parseHttpMethodList(" get ,post, "),
["GET", "POST"],
"Method list should be normalized to uppercase without empty entries"
);
});