'POST', 'callback' => [ self::class, 'upload' ], 'permission_callback' => [ self::class, 'permission_upload' ], ] ); register_rest_route( 'proofkit/v1', '/file', [ 'methods' => 'GET', 'callback' => [ self::class, 'file' ], 'permission_callback' => '__return_true', ] ); register_rest_route( 'proofkit/v1', '/respond', [ 'methods' => 'POST', 'callback' => [ self::class, 'respond' ], 'permission_callback' => '__return_true', ] ); } ); } /** * Permission callback for uploads: rate limit, then capability or valid nonce. * Guests are allowed by design, but only with a valid wp_rest nonce. * * @param \WP_REST_Request $request Request. * @return true|\WP_Error */ public static function permission_upload( \WP_REST_Request $request ) { if ( self::is_rate_limited() ) { if ( ! headers_sent() ) { header( 'Retry-After: 3600' ); } return new \WP_Error( 'proofkit_rate_limited', __( 'Too many uploads. Please try again in an hour.', 'proofkit' ), [ 'status' => 429 ] ); } if ( current_user_can( 'upload_files' ) ) { return true; } $nonce = (string) $request->get_header( 'X-WP-Nonce' ); if ( '' === $nonce && isset( $_REQUEST['_wpnonce'] ) ) { $nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ); } if ( '' !== $nonce && false !== wp_verify_nonce( $nonce, 'wp_rest' ) ) { return true; } return new \WP_Error( 'proofkit_forbidden', __( 'A valid security token is required to upload artwork.', 'proofkit' ), [ 'status' => 403 ] ); } /** * Accept an artwork file and return a signed, short-lived descriptor token. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public static function upload( \WP_REST_Request $request ): \WP_REST_Response|\WP_Error { $files = $request->get_file_params(); if ( ! isset( $files['file'] ) || ! is_array( $files['file'] ) ) { return new \WP_Error( 'proofkit_upload_missing', __( 'Please choose a file to upload.', 'proofkit' ), [ 'status' => 400 ] ); } $product_id = absint( $request->get_param( 'product_id' ) ); if ( $product_id <= 0 || ! Product::is_enabled( $product_id ) ) { return new \WP_Error( 'proofkit_upload_product', __( 'Artwork upload is not available for this product.', 'proofkit' ), [ 'status' => 400 ] ); } $valid = Upload::validate( $files['file'], $product_id ); if ( is_wp_error( $valid ) ) { return self::upload_error( $valid ); } $stored = Upload::store( $files['file'], $product_id ); if ( is_wp_error( $stored ) ) { return self::upload_error( $stored ); } $descriptor = self::descriptor( $stored ); if ( [] === $descriptor ) { return new \WP_Error( 'proofkit_upload_invalid', __( 'The uploaded file could not be saved. Please try again.', 'proofkit' ), [ 'status' => 500 ] ); } $token = Token::sign_upload( $descriptor ); if ( ! is_string( $token ) || '' === $token ) { return new \WP_Error( 'proofkit_upload_token', __( 'The uploaded file could not be attached. Please try again.', 'proofkit' ), [ 'status' => 500 ] ); } $response = rest_ensure_response( [ 'token' => $token, 'name' => $descriptor['name'], 'size' => (int) $descriptor['size'], 'sha256' => $descriptor['sha256'], // Must match Token::sign_upload() (72 hours). 'expires_at' => gmdate( 'c', time() + 72 * HOUR_IN_SECONDS ), ] ); $response->set_status( 201 ); $response->header( 'Cache-Control', 'no-store' ); return $response; } /** * Stream a stored artwork file. Authorised by a proof token or a shop manager. * * @param \WP_REST_Request $request Request. */ public static function file( \WP_REST_Request $request ) { $proof_id = absint( $request->get_param( 'proof_id' ) ); $order_id = absint( $request->get_param( 'order_id' ) ); $raw_token = $request->get_param( 'token' ); $token = is_string( $raw_token ) ? sanitize_text_field( $raw_token ) : ''; if ( $proof_id <= 0 || $order_id <= 0 ) { return self::forbidden(); } $proof = Proof::get( $proof_id ); if ( ! $proof || (int) $proof->order_id !== $order_id ) { return self::forbidden(); } $authorised = false; $actor = 'customer'; if ( '' !== $token ) { $authorised = Token::validate( $token, $proof_id, 'respond', false ) || Token::validate( $token, $proof_id, 'view', false ); } elseif ( current_user_can( 'manage_woocommerce' ) ) { $authorised = true; $actor = 'admin'; } if ( ! $authorised ) { return self::forbidden(); } $raw_download = $request->get_param( 'download' ); $download = is_scalar( $raw_download ) ? (bool) rest_sanitize_boolean( (string) $raw_download ) : false; $path = (string) $proof->file_path; $hash = (string) $proof->sha256; if ( ! Upload::exists( $path ) || ! Upload::verify( $path, $hash ) ) { self::log( sprintf( 'ProofKit: stored file failed verification for proof %d.', $proof_id ) ); return new \WP_Error( 'proofkit_file_unverified', __( 'This file could not be verified. Please contact the shop.', 'proofkit' ), [ 'status' => 409 ] ); } if ( $download ) { Proof::record_event( $proof_id, $order_id, 'downloaded', $actor, [ 'ip_hash' => Util::hash_ip( Util::client_ip() ) ] ); Upload::send_file( $path, (string) $proof->file_name, (string) $proof->mime, 'attachment' ); exit; } Proof::mark_viewed( $proof_id, hash( 'sha256', $token ) ); // Inline is only ever honoured for raster images and PDFs; send_file() // forces attachment for SVG/AI/EPS and every other type. Upload::send_file( $path, (string) $proof->file_name, (string) $proof->mime, 'inline' ); exit; } /** * Approve a proof or request changes using a respond token. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public static function respond( \WP_REST_Request $request ): \WP_REST_Response|\WP_Error { $proof_id = absint( $request->get_param( 'proof_id' ) ); $order_id = absint( $request->get_param( 'order_id' ) ); $raw_token = $request->get_param( 'token' ); $token = is_string( $raw_token ) ? sanitize_text_field( $raw_token ) : ''; $raw_action = $request->get_param( 'action' ); $action = is_string( $raw_action ) ? sanitize_key( $raw_action ) : ''; $raw_note = $request->get_param( 'note' ); $note = is_string( $raw_note ) ? wp_kses_post( self::truncate( $raw_note, 2000 ) ) : ''; if ( $proof_id <= 0 || $order_id <= 0 || '' === $token || ! in_array( $action, [ 'approve', 'changes' ], true ) ) { return new \WP_Error( 'proofkit_bad_request', __( 'This request is not valid.', 'proofkit' ), [ 'status' => 400 ] ); } if ( self::respond_rate_limited( $proof_id ) ) { if ( ! headers_sent() ) { header( 'Retry-After: 3600' ); } return new \WP_Error( 'proofkit_rate_limited', __( 'Too many attempts. Please try again in an hour.', 'proofkit' ), [ 'status' => 429 ] ); } $proof = Proof::get( $proof_id ); if ( ! $proof || (int) $proof->order_id !== $order_id ) { return self::forbidden(); } // Do not consume here: Proof::approve()/request_changes() revoke tokens. if ( ! Token::validate( $token, $proof_id, 'respond', false ) ) { return self::forbidden(); } if ( 'approve' === $action ) { $result = Proof::approve( $proof_id, $note ); $message = __( 'Thank you — your proof is approved.', 'proofkit' ); } else { if ( '' === trim( wp_strip_all_tags( $note ) ) ) { return new \WP_Error( 'proofkit_note_required', __( 'Please describe the changes you need.', 'proofkit' ), [ 'status' => 400 ] ); } $result = Proof::request_changes( $proof_id, $note ); $message = __( 'Your change request has been sent.', 'proofkit' ); } if ( is_wp_error( $result ) ) { return new \WP_Error( $result->get_error_code(), $result->get_error_message(), [ 'status' => self::error_status( $result ) ] ); } $updated = Proof::get( $proof_id ); return rest_ensure_response( [ 'status' => $updated ? (string) $updated->status : '', 'message' => $message, ] ); } /** * Sliding one-hour upload counter. * * Shop managers are never counted. Logged-in users are bucketed by user id * so a shared NAT does not throttle everyone together; guests are bucketed * by salted IP hash. This read/modify/write is best-effort: on hosts * without a persistent object cache concurrent requests can race and * slightly exceed the limit. That is acceptable for a defence-in-depth cap. */ private static function is_rate_limited(): bool { if ( current_user_can( 'manage_woocommerce' ) ) { return false; } if ( is_user_logged_in() ) { $key = 'proofkit_rl_user_' . get_current_user_id(); } else { $key = 'proofkit_rl_ip_' . md5( Util::hash_ip( Util::client_ip() ) ); } $count = (int) get_transient( $key ); if ( $count >= 20 ) { return true; } set_transient( $key, $count + 1, HOUR_IN_SECONDS ); return false; } /** * Per-IP, per-proof response counter (20 per hour). * * @param int $proof_id Proof id. */ private static function respond_rate_limited( int $proof_id ): bool { $key = 'proofkit_rl_respond_' . md5( Util::hash_ip( Util::client_ip() ) . '|' . $proof_id ); $count = (int) get_transient( $key ); if ( $count >= 20 ) { return true; } set_transient( $key, $count + 1, HOUR_IN_SECONDS ); return false; } /** * Normalise a stored-file descriptor. * * @param array $stored Descriptor from Upload::store(). */ private static function descriptor( array $stored ): array { $path = isset( $stored['path'] ) ? (string) $stored['path'] : ''; $sha256 = isset( $stored['sha256'] ) ? (string) $stored['sha256'] : ''; if ( '' === $path || '' === $sha256 ) { return []; } return [ 'path' => $path, 'name' => isset( $stored['name'] ) ? (string) $stored['name'] : '', 'mime' => isset( $stored['mime'] ) ? (string) $stored['mime'] : '', 'size' => isset( $stored['size'] ) ? (int) $stored['size'] : 0, 'sha256' => $sha256, ]; } /** * Map an upload error to a REST status, promoting size failures to 413. * * @param \WP_Error $error Upload error. */ private static function upload_error( \WP_Error $error ): \WP_Error { $code = (string) $error->get_error_code(); $status = 400; if ( false !== strpos( $code, 'size' ) || false !== strpos( $code, 'large' ) ) { $status = 413; } $data = $error->get_error_data(); if ( is_array( $data ) && ! empty( $data['status'] ) ) { $status = (int) $data['status']; } return new \WP_Error( $code, $error->get_error_message(), [ 'status' => $status ] ); } /** * Map a proof state error to 400/403/409. * * @param \WP_Error $error Proof error. */ private static function error_status( \WP_Error $error ): int { $data = $error->get_error_data(); if ( is_array( $data ) && ! empty( $data['status'] ) ) { return (int) $data['status']; } $code = (string) $error->get_error_code(); if ( false !== strpos( $code, 'forbidden' ) ) { return 403; } if ( false !== strpos( $code, 'state' ) || false !== strpos( $code, 'expired' ) || false !== strpos( $code, 'used' ) || false !== strpos( $code, 'wrong_status' ) || false !== strpos( $code, 'duplicate_version' ) || false !== strpos( $code, 'order_closed' ) ) { return 409; } return 400; } /** * Cap a string without breaking multibyte characters. * * @param string $text Text. * @param int $limit Character limit. */ private static function truncate( string $text, int $limit ): string { if ( function_exists( 'mb_substr' ) ) { return mb_substr( $text, 0, $limit ); } return substr( $text, 0, $limit ); } /** * Uniform 403 with no detail about which check failed. */ private static function forbidden(): \WP_Error { return new \WP_Error( 'proofkit_forbidden', __( 'This proof link is not valid.', 'proofkit' ), [ 'status' => 403 ] ); } /** * Log a system message without ever breaking the request. * * @param string $message Message. */ private static function log( string $message ): void { if ( ! is_callable( [ Util::class, 'log' ] ) ) { return; } try { Util::log( $message ); } catch ( \Throwable $e ) { // Logging must never break file delivery. return; } } }