All files / lib/resources validator.ts

100% Statements 227/227
100% Branches 24/24
100% Functions 11/11
100% Lines 227/227

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 2271x 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 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 652x 75x 16x 16x 59x 75x 58x 58x 58x 58x 1x 1x 1x 1x 1x 652x 652x 652x 652x 652x 652x 652x 652x 5x 5x 5x 5x 5x 5x 652x 652x 652x 652x 652x 652x 652x 652x 11x 11x 11x 11x 11x 11x 652x 652x 652x 652x 652x 652x 652x 652x 6x 6x 6x 6x 6x 6x 6x 652x 652x 652x 652x 652x 652x 652x 652x 4x 4x 4x 4x 4x 4x 652x 652x 652x 652x 652x 652x 652x 652x 3x 3x 3x 3x 3x 3x 652x 652x 652x 652x 652x 652x 652x 652x 3x 3x 3x 3x 3x 3x 652x 652x 652x 652x 652x 652x 652x 652x 12x 12x 12x 12x 12x 12x 12x 652x 652x 652x 652x 652x 652x 652x 652x 13x 13x 13x 13x 13x 13x 13x 652x 652x 652x 652x 652x 652x 652x 652x 18x 18x 18x 18x 18x 18x 652x
 
import {default as Ajv} from 'ajv';
import ResourceFileSchema from "./schemas/resource-file.json" assert { type: "json" }; // eslint-disable-line
import SigninResponseSchema from "./schemas/signin-response.json" assert { type: "json" }; // eslint-disable-line
import SigninInitResponseSchema from "./schemas/signin-init-response.json" assert { type: "json" }; // eslint-disable-line
import TrustResponseSchema from "./schemas/trust-response.json" assert { type: "json" }; // eslint-disable-line
import SetupResponseSchema from "./schemas/setup-response.json" assert { type: "json" }; // eslint-disable-line
import PhotosSetupResponseSchema from "./schemas/photos-setup-response.json" assert { type: "json" }; // eslint-disable-line
import ResendMFADeviceResponseSchema from "./schemas/resend-mfa-device-response.json" assert { type: "json" }; // eslint-disable-line
import ResendMFAPhoneResponseSchema from "./schemas/resend-mfa-phone-response.json" assert { type: "json" }; // eslint-disable-line
import PCSResponseSchema from "./schemas/pcs-response.json" assert { type: "json" }; // eslint-disable-line
import {ResourceFile} from "./resource-types.js";
import {iCPSError} from "../../app/error/error.js";
import {ErrorStruct, VALIDATOR_ERR} from "../../app/error/error-codes.js";
import {COOKIE_KEYS, PCSResponse, PhotosSetupResponse, ResendMFADeviceResponse, ResendMFAPhoneResponse, SetupResponse, SigninInitResponse, SigninResponse, TrustResponse} from "./network-types.js";
 
/**
 * Common configuration for the schema validator
 */
const AJV_CONF = {
    verbose: true,
    // Logger: ResourceManager.logger(`AjvValidator`),
};
 
/**
 * 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.default(AJV_CONF).compile<ResourceFile>(ResourceFileSchema);
 
    /**
     * Validator for the signin init response schema
     */
    _signinInitResponseValidator: Ajv.ValidateFunction<SigninInitResponse> = new Ajv.default(AJV_CONF).compile<SigninInitResponse>(SigninInitResponseSchema);
 
    /**
     * Validator for the signin response schema
     */
    _signinResponseValidator: Ajv.ValidateFunction<SigninResponse> = new Ajv.default(AJV_CONF).compile<SigninResponse>(SigninResponseSchema);
 
    /**
     * Validator for MFA Device Response schema
     */
    _resendMFADeviceResponseValidator: Ajv.ValidateFunction<ResendMFADeviceResponse> = new Ajv.default(AJV_CONF).compile<ResendMFADeviceResponse>(ResendMFADeviceResponseSchema);
 
    /**
     * Validator for MFA Phone Response schema
     */
    _resendMFAPhoneResponseValidator: Ajv.ValidateFunction<ResendMFAPhoneResponse> = new Ajv.default(AJV_CONF).compile<ResendMFAPhoneResponse>(ResendMFAPhoneResponseSchema);
 
    /**
     * Validator for the trust response schema
     */
    _trustResponseValidator: Ajv.ValidateFunction<TrustResponse> = new Ajv.default(AJV_CONF).compile<TrustResponse>(TrustResponseSchema);
 
    /**
     * Validator for the iCloud setup response schema
     */
    _setupResponseValidator: Ajv.ValidateFunction<SetupResponse> = new Ajv.default(AJV_CONF).compile<SetupResponse>(SetupResponseSchema);
 
    /**
     * Validator for the PCS response schema
     */
    _pcsResponseValidator: Ajv.ValidateFunction<PCSResponse> = new Ajv.default(AJV_CONF).compile<PCSResponse>(PCSResponseSchema);
 
    /**
     * Validator for the iCloud photos setup response schema
     */
    _photosSetupResponseValidator: Ajv.ValidateFunction<PhotosSetupResponse> = new Ajv.default(AJV_CONF).compile<PhotosSetupResponse>(PhotosSetupResponseSchema);
 
    /**
     * 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 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 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 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,
            (data: PCSResponse) => data.headers[`set-cookie`].filter(cookieString => cookieString.startsWith(COOKIE_KEYS.PCS_PHOTOS) || cookieString.startsWith(COOKIE_KEYS.PCS_SHARING)).length === 2,
        );
    }
 
    /**
     * 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,
        );
    }
}