|
| 1 | +/* |
| 2 | +Copyright 2023 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/** |
| 18 | + * Utilities to deal with untrusted values coming from Matrix events. |
| 19 | + * |
| 20 | + * e.g. to confirm that a value `foo` has type `{ bar: string[]}`, |
| 21 | + * run |
| 22 | + * ```ts |
| 23 | + * new SubTypeObjectContent({ |
| 24 | + * bar: STRING_CONTENT.array() |
| 25 | + * }).check_type(value) |
| 26 | + * ``` |
| 27 | + */ |
| 28 | + |
| 29 | +/** |
| 30 | + * The abstract root class for all content we wish to validate against. |
| 31 | + */ |
| 32 | +abstract class AbstractContent { |
| 33 | + /** |
| 34 | + * Validate the type of a value against `this`. |
| 35 | + * @param value |
| 36 | + */ |
| 37 | + abstract checkType(value: any): boolean; |
| 38 | + |
| 39 | + /** |
| 40 | + * If `value` has `this` type, return `value`, otherwise |
| 41 | + * return `defaults()`. |
| 42 | + */ |
| 43 | + fallback(value: any, defaults: () => any): any { |
| 44 | + if (this.checkType(value)) { |
| 45 | + return value; |
| 46 | + } |
| 47 | + return defaults(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Return an `AbstractContent` for values of type `this | null | undefined`. |
| 52 | + */ |
| 53 | + optional(): AbstractContent { |
| 54 | + return new OptionalContent(this); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Return a `AbstractContent` for values of type `this[]`. |
| 59 | + * |
| 60 | + * This is a shortcut for `new OptionalContent(this)` |
| 61 | + */ |
| 62 | + array(): AbstractContent { |
| 63 | + return new ArrayContent(this); |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * A content validator for numbers. |
| 69 | + */ |
| 70 | +class StringContent extends AbstractContent { |
| 71 | + /** |
| 72 | + * Check that `value` is a string. |
| 73 | + */ |
| 74 | + checkType(value: any): boolean { |
| 75 | + return typeof value === "string"; |
| 76 | + } |
| 77 | +}; |
| 78 | +/** |
| 79 | + * A content validator for strings (singleton). |
| 80 | + */ |
| 81 | +export const STRING_CONTENT = new StringContent(); |
| 82 | + |
| 83 | + |
| 84 | +/** |
| 85 | + * A content validator for numbers. |
| 86 | + */ |
| 87 | +class NumberContent extends AbstractContent { |
| 88 | + checkType(value: any): boolean { |
| 89 | + return typeof value === "number"; |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * A content validator for numbers (singleton). |
| 95 | + */ |
| 96 | +export const NUMBER_CONTENT = new NumberContent(); |
| 97 | + |
| 98 | +/** |
| 99 | + * A content validator for arrays. |
| 100 | + */ |
| 101 | +class ArrayContent extends AbstractContent { |
| 102 | + constructor(public readonly content: AbstractContent) { |
| 103 | + super() |
| 104 | + } |
| 105 | + /** |
| 106 | + * Check that `value` is an array and that each value it contains |
| 107 | + * has type `type.content`. |
| 108 | + */ |
| 109 | + checkType(value: any): boolean { |
| 110 | + if (!Array.isArray(value)) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + for (let item of value) { |
| 114 | + if (!this.content.checkType(item)) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + } |
| 118 | + return true; |
| 119 | + } |
| 120 | +} |
| 121 | +class OptionalContent extends AbstractContent { |
| 122 | + constructor(public readonly content: AbstractContent) { |
| 123 | + super() |
| 124 | + } |
| 125 | + optional(): AbstractContent { |
| 126 | + return this; |
| 127 | + } |
| 128 | + /** |
| 129 | + * Check that value either has type `this.content` or is `null` or `undefined`. |
| 130 | + */ |
| 131 | + checkType(value: any): boolean { |
| 132 | + if (typeof value === "undefined") { |
| 133 | + return true; |
| 134 | + } |
| 135 | + if (value === null) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + if (this.content.checkType(value)) { |
| 139 | + return true; |
| 140 | + } |
| 141 | + return false; |
| 142 | + } |
| 143 | +} |
| 144 | +export class SubTypeObjectContent extends AbstractContent { |
| 145 | + constructor(public readonly fields: Record<string, AbstractContent>) { |
| 146 | + super() |
| 147 | + } |
| 148 | + /** |
| 149 | + * Check that `value` contains **at least** the fields of `this.fields` |
| 150 | + * and that each field specified in `this.fields` holds a value that |
| 151 | + * matches the type specified in `this.fields`. |
| 152 | + */ |
| 153 | + checkType(value: any): boolean { |
| 154 | + if (typeof value !== "object") { |
| 155 | + return false; |
| 156 | + } |
| 157 | + if (value === null) { |
| 158 | + // Let's not forget that `typeof null === "object"` |
| 159 | + return false; |
| 160 | + } |
| 161 | + if (Array.isArray(value)) { |
| 162 | + // Let's not forget that `typeof [...] === "object"` |
| 163 | + return false; |
| 164 | + } |
| 165 | + for (let [k, expected] of Object.entries(this.fields)) { |
| 166 | + if (!expected.checkType(value[k])) { |
| 167 | + return false; |
| 168 | + } |
| 169 | + } |
| 170 | + return true; |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +export class ExactTypeObjectContent extends SubTypeObjectContent { |
| 175 | + constructor(public readonly fields: Record<string, AbstractContent>) { |
| 176 | + super(fields) |
| 177 | + } |
| 178 | + /** |
| 179 | + * Check that `value` contains **exactly** the fields of `this.fields` |
| 180 | + * and that each field specified in `this.fields` holds a value that |
| 181 | + * matches the type specified in `this.fields`. |
| 182 | + */ |
| 183 | + checkType(value: any): boolean { |
| 184 | + if (!super.checkType(value)) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + // Check that we don't have any field we're not expecting. |
| 188 | + for (let k of Object.keys(value)) { |
| 189 | + if (!(k in this.fields)) { |
| 190 | + return false; |
| 191 | + } |
| 192 | + } |
| 193 | + return true; |
| 194 | + } |
| 195 | +} |
0 commit comments