All files / lib/resources validator.ts

100% Statements 439/439
100% Branches 50/50
100% Functions 22/22
100% Lines 439/439

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 4401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24741x 24741x 24741x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1648x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 300x 193x 193x 107x 154x 106x 106x 106x 106x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 19x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 57x 57x 57x 57x 57x 57x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 68x 68x 68x 68x 68x 68x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 29x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 1x  
 
import * as Ajv from 'ajv';
import {ErrorStruct, ICLOUD_PHOTOS_ERR, VALIDATOR_ERR} from "../../app/error/error-codes.js";
import {iCPSError} from "../../app/error/error.js";
import {HttpResponse, ResponseValidator} from "./http-client.js";
import {AuthInformationResponse, CloudKitRecordsResponse, COOKIE_KEYS, EscrowCompleteResponse, EscrowInitResponse, HealthCheckPingResponse, LogoutResponse, MFASubmitResponse, PCSResponse, PhotosSetupResponse, ResendMFADeviceResponse, ResendMFAPhoneResponse, SetupResponse, SigninInitResponse, SigninResponse, TrustResponse} from "./network-types.js";
import {ResourceFile} from "./resource-types.js";
import {PushSubscription} from './web-server-types.js';
import PCSResponseSchema from "./schemas/pcs-response.json" with { type: "json" };
import PhotosSetupResponseSchema from "./schemas/photos-setup-response.json" with { type: "json" };
import AuthInformationResponseSchema from "./schemas/auth-information-response.json" with { type: "json" };
import ResendMFADeviceResponseSchema from "./schemas/resend-mfa-device-response.json" with { type: "json" };
import ResendMFAPhoneResponseSchema from "./schemas/resend-mfa-phone-response.json" with { type: "json" };
import ResourceFileSchema from "./schemas/resource-file.json" with { type: "json" };
import PushSubscriptionSchema from "./schemas/push-subscription.json" with { type: "json" };
import SetupResponseSchema from "./schemas/setup-response.json" with { type: "json" };
import SigninInitResponseSchema from "./schemas/signin-init-response.json" with { type: "json" };
import EscrowInitResponseSchema from "./schemas/escrow-init-response.json" with { type: "json" };
import SigninResponseSchema from "./schemas/signin-response.json" with { type: "json" };
import TrustResponseSchema from "./schemas/trust-response.json" with { type: "json" };
import MFASubmitResponseSchema from "./schemas/mfa-submit-response.json" with { type: "json" };
import EscrowCompleteResponseSchema from "./schemas/escrow-complete-response.json" with { type: "json" };
import LogoutResponseSchema from "./schemas/logout-response.json" with { type: "json" };
import CloudKitRecordsResponseSchema from "./schemas/cloud-kit-records-response.json" with { type: "json" };
import HealthCheckPingResponseSchema from "./schemas/health-check-ping-response.json" with { type: "json" };
 
/**
 * Common configuration for the schema validator
 */
const AJV_CONF = {
    verbose: true,
    // Logger: ResourceManager.logger(`AjvValidator`),
};
 
/**
 * Marks a function as response validator - only used within this module, so every response validator passed to a request is backed by a JSON schema
 * @param validate - The validation function, expected to validate the response (or the value derived from it) against a JSON schema
 * @returns The response validator
 */
function responseValidator<T>(validate: (response: HttpResponse) => T): ResponseValidator<T> {
    return validate as ResponseValidator<T>;
}
 
/**
 * This class is responsible for validating 3rd party provided JSON based resources using previously compiled JSON schemas
 */
export class Validator {
    /**
     * Validator for the resource file schema
     */
    _resourceFileValidator: Ajv.ValidateFunction<ResourceFile> = new Ajv.Ajv(AJV_CONF).compile<ResourceFile>(ResourceFileSchema);
 
    /**
     * Validator for the push subscription web server request
     */
    _pushSubscriptionValidator: Ajv.ValidateFunction<PushSubscription> = new Ajv.Ajv(AJV_CONF).compile<PushSubscription>(PushSubscriptionSchema);
 
    /**
     * Validator for the signin init response schema
     */
    _signinInitResponseValidator: Ajv.ValidateFunction<SigninInitResponse> = new Ajv.Ajv(AJV_CONF).compile<SigninInitResponse>(SigninInitResponseSchema);
 
    /**
     * Validator for the escrow init response schema
     */
    _escrowInitResponseValidator: Ajv.ValidateFunction<EscrowInitResponse> = new Ajv.Ajv(AJV_CONF).compile<EscrowInitResponse>(EscrowInitResponseSchema);
 
    /**
     * Validator for the signin response schema
     */
    _signinResponseValidator: Ajv.ValidateFunction<SigninResponse> = new Ajv.Ajv(AJV_CONF).compile<SigninResponse>(SigninResponseSchema);
 
    /**
     * Validator for the auth information response schema
     */
    _authInformationResponseValidator: Ajv.ValidateFunction<AuthInformationResponse> = new Ajv.Ajv(AJV_CONF).compile<AuthInformationResponse>(AuthInformationResponseSchema);
 
    /**
     * Validator for MFA Device Response schema
     */
    _resendMFADeviceResponseValidator: Ajv.ValidateFunction<ResendMFADeviceResponse> = new Ajv.Ajv(AJV_CONF).compile<ResendMFADeviceResponse>(ResendMFADeviceResponseSchema);
 
    /**
     * Validator for MFA Phone Response schema
     */
    _resendMFAPhoneResponseValidator: Ajv.ValidateFunction<ResendMFAPhoneResponse> = new Ajv.Ajv(AJV_CONF).compile<ResendMFAPhoneResponse>(ResendMFAPhoneResponseSchema);
 
    /**
     * Validator for the trust response schema
     */
    _trustResponseValidator: Ajv.ValidateFunction<TrustResponse> = new Ajv.Ajv(AJV_CONF).compile<TrustResponse>(TrustResponseSchema);
 
    /**
     * Validator for the iCloud setup response schema
     */
    _setupResponseValidator: Ajv.ValidateFunction<SetupResponse> = new Ajv.Ajv(AJV_CONF).compile<SetupResponse>(SetupResponseSchema);
 
    /**
     * Validator for the PCS response schema
     */
    _pcsResponseValidator: Ajv.ValidateFunction<PCSResponse> = new Ajv.Ajv(AJV_CONF).compile<PCSResponse>(PCSResponseSchema);
 
    /**
     * Validator for the iCloud photos setup response schema
     */
    _photosSetupResponseValidator: Ajv.ValidateFunction<PhotosSetupResponse> = new Ajv.Ajv(AJV_CONF).compile<PhotosSetupResponse>(PhotosSetupResponseSchema);
 
    /**
     * Validator for the MFA submit response schema
     */
    _mfaSubmitResponseValidator: Ajv.ValidateFunction<MFASubmitResponse> = new Ajv.Ajv(AJV_CONF).compile<MFASubmitResponse>(MFASubmitResponseSchema);
 
    /**
     * Validator for the escrow complete response schema
     */
    _escrowCompleteResponseValidator: Ajv.ValidateFunction<EscrowCompleteResponse> = new Ajv.Ajv(AJV_CONF).compile<EscrowCompleteResponse>(EscrowCompleteResponseSchema);
 
    /**
     * Validator for the logout response schema
     */
    _logoutResponseValidator: Ajv.ValidateFunction<LogoutResponse> = new Ajv.Ajv(AJV_CONF).compile<LogoutResponse>(LogoutResponseSchema);
 
    /**
     * Validator for the CloudKit records response schema (queries and operations)
     */
    _cloudKitRecordsResponseValidator: Ajv.ValidateFunction<CloudKitRecordsResponse> = new Ajv.Ajv(AJV_CONF).compile<CloudKitRecordsResponse>(CloudKitRecordsResponseSchema);
 
    /**
     * Validator for the health check ping response schema
     */
    _healthCheckPingResponseValidator: Ajv.ValidateFunction<HealthCheckPingResponse> = new Ajv.Ajv(AJV_CONF).compile<HealthCheckPingResponse>(HealthCheckPingResponseSchema);
 
    /**
     * Response validators, to be passed to network requests - every request needs to validate its response against a JSON schema
     * The validators delegate to the respective validate function at the time of validation
     */
    response = {
        signinInit: responseValidator(response => this.validateSigninInitResponse(response)),
        signin: responseValidator(response => this.validateSigninResponse(response)),
        escrowInit: responseValidator(response => this.validateEscrowInitResponse(response)),
        escrowComplete: responseValidator(response => this.validateEscrowCompleteResponse(response)),
        /**
         * The auth information is either provided as JSON or embedded in HTML, therefore it needs to be extracted before validation
         * @param extract - Extracts the auth information from the response
         * @returns The response validator
         */
        authInformation: (extract: (response: HttpResponse) => unknown) => responseValidator(response => this.validateAuthInformationResponse(extract(response))),
        resendMFADevice: responseValidator(response => this.validateResendMFADeviceResponse(response)),
        resendMFAPhone: responseValidator(response => this.validateResendMFAPhoneResponse(response)),
        mfaSubmit: responseValidator(response => this.validateMFASubmitResponse(response)),
        trust: responseValidator(response => this.validateTrustResponse(response)),
        setup: responseValidator(response => this.validateSetupResponse(response)),
        pcs: responseValidator(response => this.validatePCSResponse(response)),
        logout: responseValidator(response => this.validateLogoutResponse(response)),
        photosSetup: responseValidator(response => this.validatePhotosSetupResponse(response)),
        query: responseValidator(response => this.validateQueryResponse(response)),
        operation: responseValidator(response => this.validateOperationResponse(response)),
        healthCheckPing: responseValidator(response => this.validateHealthCheckPingResponse(response)),
    };
 
    /**
     * Generic validation function
     * @param validator - Uses the pre-configured ajv validator to validate the data
     * @param errorStruct - The error struct to throw, in case validation fails
     * @param data - The data to validate
     * @param additionalValidations - Optional additional validation functions
     * @returns The validated data
     * @throws The error struct with context and message if validation fails
     */
    validate<T>(validator: Ajv.ValidateFunction<T>, errorStruct: ErrorStruct, data: unknown, ...additionalValidations: ((arg0: T) => boolean)[]): T {
        if (validator(data) && additionalValidations.every(validation => validation(data))) {
            return data;
        }
 
        if (validator.errors) {
            throw new iCPSError(errorStruct)
                .addMessage(`${validator.errors![0].message} (${validator.errors![0].instancePath})`)
                .addContext(`data`, data);
        }
 
        throw new iCPSError(errorStruct)
            .addMessage(`Additional validations failed`)
            .addContext(`data`, data);
    }
 
    /**
     * Validates the provided data string against the resource file schema
     * @param data - The data to validate
     * @throws An error if the data cannot be parsed
     * @returns The parsed ResourceFile data
     */
    validateResourceFile(data: unknown): ResourceFile {
        return this.validate(
            this._resourceFileValidator,
            VALIDATOR_ERR.RESOURCE_FILE,
            data,
        );
    }
 
    /**
     * Validates the provided data string against the push subscription schema
     * @param data - The data to validate
     * @throws An error if the data cannot be parsed
     * @returns The parsed ResourceFile data
     */
    validatePushSubscription(data: unknown): PushSubscription {
        return this.validate(
            this._pushSubscriptionValidator,
            VALIDATOR_ERR.PUSH_SUBSCRIPTION,
            data,
        );
    }
 
    /**
     * Validates the response from the signin init request
     * @param data - The data to validate
     * @returns A validated SigninInitResponse object
     * @throws An error if the data cannot be validated
     */
    validateSigninInitResponse(data: unknown): SigninInitResponse {
        return this.validate(
            this._signinInitResponseValidator,
            VALIDATOR_ERR.SIGNIN_INIT_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from the escrow init request
     * @param data - The data to validate
     * @returns A validated EscrowInitResponse object
     * @throws An error if the data cannot be validated
     */
    validateEscrowInitResponse(data: unknown): EscrowInitResponse {
        return this.validate(
            this._escrowInitResponseValidator,
            VALIDATOR_ERR.ESCROW_INIT_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from the signin request
     * @param data - The data to validate
     * @returns A validated SigninResponse object
     * @throws An error if the data cannot be validated
     */
    validateSigninResponse(data: unknown): SigninResponse {
        return this.validate(
            this._signinResponseValidator,
            VALIDATOR_ERR.SIGNIN_RESPONSE,
            data,
            (data: SigninResponse) => data.headers[`set-cookie`].filter(cookieString => cookieString.startsWith(COOKIE_KEYS.AASP)).length <= 1, // AASP cookie is optional
        );
    }
 
    /**
     * Validates the response from the auth information request
     * @param data - The data to validate
     * @returns A validated SigninResponse object
     * @throws An error if the data cannot be validated
     */
    validateAuthInformationResponse(data: unknown): AuthInformationResponse {
        return this.validate(
            this._authInformationResponseValidator,
            VALIDATOR_ERR.AUTH_INFORMATION_RESPONSE,
            data
        );
    }
 
    /**
     * Validates the response from resending the MFA code via a device
     * @param data - The data to validate
     * @returns A validated ResendMFADeviceResponse object
     * @throws An error if the data cannot be validated
     */
    validateResendMFADeviceResponse(data: unknown): ResendMFADeviceResponse {
        return this.validate(
            this._resendMFADeviceResponseValidator,
            VALIDATOR_ERR.RESEND_MFA_DEVICE_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from resending the MFA code via a phone
     * @param data - The data to validate
     * @returns A validated ResendMFAPhoneResponse object
     * @throws An error if the data cannot be validated
     */
    validateResendMFAPhoneResponse(data: unknown): ResendMFAPhoneResponse {
        return this.validate(
            this._resendMFAPhoneResponseValidator,
            VALIDATOR_ERR.RESEND_MFA_PHONE_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from the trust request
     * @param data - The data to validate
     * @returns A validated TrustResponse object
     * @throws An error if the data cannot be validated
     */
    validateTrustResponse(data: unknown): TrustResponse {
        return this.validate(
            this._trustResponseValidator,
            VALIDATOR_ERR.TRUST_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from the setup request
     * @param data - The data to validate
     * @returns A validated SetupResponse object
     * @throws An error if the data cannot be validated
     */
    validateSetupResponse(data: unknown): SetupResponse {
        return this.validate(
            this._setupResponseValidator,
            VALIDATOR_ERR.SETUP_RESPONSE,
            data,
            (data: SetupResponse) => data.headers[`set-cookie`].filter(cookieString => cookieString.startsWith(COOKIE_KEYS.X_APPLE)).length > 1, // Making sure there are authentication cookies
        );
    }
 
    /**
     * Validates the response from the PCS acquisition request
     * @param data - The data to validate
     * @returns A validated PCSResponse object
     * @throws An error if the data cannot be validated
     */
    validatePCSResponse(data: unknown): PCSResponse {
        return this.validate(
            this._pcsResponseValidator,
            VALIDATOR_ERR.PCS_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from the photos setup request
     * @param data - The data to validate
     * @returns A validated PhotosSetupResponse object
     * @throws An error if the data cannot be validated
     */
    validatePhotosSetupResponse(data: unknown): PhotosSetupResponse {
        return this.validate(
            this._photosSetupResponseValidator,
            VALIDATOR_ERR.PHOTOS_SETUP_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from the MFA code submission
     * @param data - The data to validate
     * @returns A validated MFASubmitResponse object
     * @throws An error if the data cannot be validated
     */
    validateMFASubmitResponse(data: unknown): MFASubmitResponse {
        return this.validate(
            this._mfaSubmitResponseValidator,
            VALIDATOR_ERR.MFA_SUBMIT_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from the escrow completion request
     * @param data - The data to validate
     * @returns A validated EscrowCompleteResponse object
     * @throws An error if the data cannot be validated
     */
    validateEscrowCompleteResponse(data: unknown): EscrowCompleteResponse {
        return this.validate(
            this._escrowCompleteResponseValidator,
            VALIDATOR_ERR.ESCROW_COMPLETE_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from the logout request
     * @param data - The data to validate
     * @returns A validated LogoutResponse object
     * @throws An error if the data cannot be validated
     */
    validateLogoutResponse(data: unknown): LogoutResponse {
        return this.validate(
            this._logoutResponseValidator,
            VALIDATOR_ERR.LOGOUT_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from a CloudKit query - the records themselves are parsed defensively by the query parser
     * @param data - The data to validate
     * @returns A validated CloudKitRecordsResponse object
     * @throws An error if the data cannot be validated
     */
    validateQueryResponse(data: unknown): CloudKitRecordsResponse {
        return this.validate(
            this._cloudKitRecordsResponseValidator,
            ICLOUD_PHOTOS_ERR.UNEXPECTED_QUERY_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from a CloudKit operation - the records themselves are parsed defensively by the query parser
     * @param data - The data to validate
     * @returns A validated CloudKitRecordsResponse object
     * @throws An error if the data cannot be validated
     */
    validateOperationResponse(data: unknown): CloudKitRecordsResponse {
        return this.validate(
            this._cloudKitRecordsResponseValidator,
            ICLOUD_PHOTOS_ERR.UNEXPECTED_OPERATIONS_RESPONSE,
            data,
        );
    }
 
    /**
     * Validates the response from a health check ping
     * @param data - The data to validate
     * @returns A validated HealthCheckPingResponse object
     * @throws An error if the data cannot be validated
     */
    validateHealthCheckPingResponse(data: unknown): HealthCheckPingResponse {
        return this.validate(
            this._healthCheckPingResponseValidator,
            VALIDATOR_ERR.HEALTH_CHECK_PING_RESPONSE,
            data,
        );
    }
}