insert( Install::table( 'tokens' ), [ 'proof_id' => $proof_id, 'token_hash' => hash( 'sha256', $raw ), 'scope' => $scope, 'expires_at' => gmdate( 'Y-m-d H:i:s', time() + $ttl_seconds ), 'created_at' => Util::now(), ], [ '%d', '%s', '%s', '%s', '%s' ] ); if ( false === $inserted ) { self::log( 'Could not store a proof token.', [ 'proof_id' => $proof_id ] ); return ''; } return $raw; } /** * Validate a raw token for a proof and scope. * * @param string $raw Raw token from the URL. * @param int $proof_id Expected proof id. * @param string $scope respond|view. * @param bool $consume Mark the token used on success. */ public static function validate( string $raw, int $proof_id, string $scope, bool $consume = false ): bool { global $wpdb; $proof_id = absint( $proof_id ); $scope = strtolower( $scope ); if ( $proof_id <= 0 || '' === $raw || strlen( $raw ) > 128 || ! in_array( $scope, self::SCOPES, true ) ) { return false; } $hash = hash( 'sha256', $raw ); $row = $wpdb->get_row( $wpdb->prepare( 'SELECT id, token_hash, expires_at, used_at FROM %i WHERE proof_id = %d AND token_hash = %s AND scope = %s LIMIT 1', Install::table( 'tokens' ), $proof_id, $hash, $scope ) ); if ( ! $row ) { return false; } if ( ! hash_equals( (string) $row->token_hash, $hash ) ) { return false; } if ( null !== $row->used_at && '' !== (string) $row->used_at ) { return false; } if ( '' === (string) $row->expires_at || Util::is_expired( (string) $row->expires_at ) ) { return false; } if ( ! $consume ) { return true; } $updated = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET used_at = %s WHERE id = %d AND used_at IS NULL', Install::table( 'tokens' ), Util::now(), (int) $row->id ) ); return 1 === $updated; } /** * Delete tokens for a proof, optionally limited to one scope. */ public static function revoke( int $proof_id, ?string $scope = null ): void { global $wpdb; $where = [ 'proof_id' => absint( $proof_id ) ]; $formats = [ '%d' ]; if ( null !== $scope && in_array( strtolower( $scope ), self::SCOPES, true ) ) { $where['scope'] = strtolower( $scope ); $formats[] = '%s'; } if ( $where['proof_id'] <= 0 ) { return; } $wpdb->delete( Install::table( 'tokens' ), $where, $formats ); } /** * Remove expired tokens and tokens used more than 30 days ago. */ public static function purge_expired(): int { global $wpdb; $used_cutoff = gmdate( 'Y-m-d H:i:s', time() - ( 30 * DAY_IN_SECONDS ) ); $deleted = $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE expires_at < %s OR ( used_at IS NOT NULL AND used_at < %s )', Install::table( 'tokens' ), Util::now(), $used_cutoff ) ); return is_int( $deleted ) ? $deleted : 0; } /** * Sign an upload descriptor for use in the cart and checkout. */ public static function sign_upload( array $descriptor ): string { $clean = []; foreach ( [ 'path', 'name', 'mime', 'size', 'sha256' ] as $key ) { if ( array_key_exists( $key, $descriptor ) ) { $clean[ $key ] = $descriptor[ $key ]; } } if ( isset( $clean['size'] ) && is_numeric( $clean['size'] ) ) { $clean['size'] = (int) $clean['size']; } $clean['exp'] = time() + self::UPLOAD_TTL; $json = json_encode( $clean, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); if ( false === $json ) { return ''; } $payload = self::base64url( $json ); $sig = self::base64url( hash_hmac( 'sha256', $payload, wp_salt( 'proofkit_upload' ), true ) ); return $payload . '.' . $sig; } /** * Verify a signed upload descriptor. Never throws. * * @return array|\WP_Error Descriptor with path, name, mime, size, sha256, exp. */ public static function verify_upload( string $token ): array|\WP_Error { try { return self::do_verify_upload( $token ); } catch ( \Throwable ) { return self::bad_upload(); } } /** * Internal verification path. * * @return array|\WP_Error */ private static function do_verify_upload( string $token ): array|\WP_Error { if ( '' === $token || strlen( $token ) > 8192 ) { return self::bad_upload(); } $parts = explode( '.', $token ); if ( 2 !== count( $parts ) || '' === $parts[0] || '' === $parts[1] ) { return self::bad_upload(); } $provided = self::base64url_decode( $parts[1] ); if ( false === $provided || strlen( $provided ) !== 32 ) { return self::bad_upload(); } $expected = hash_hmac( 'sha256', $parts[0], wp_salt( 'proofkit_upload' ), true ); if ( ! hash_equals( $expected, $provided ) ) { return self::bad_upload(); } $json = self::base64url_decode( $parts[0] ); if ( false === $json || '' === $json ) { return self::bad_upload(); } $data = json_decode( $json, true ); if ( ! is_array( $data ) ) { return self::bad_upload(); } foreach ( [ 'path', 'name', 'mime', 'size', 'sha256', 'exp' ] as $key ) { if ( ! array_key_exists( $key, $data ) ) { return self::bad_upload(); } } $path = is_string( $data['path'] ) ? $data['path'] : ''; if ( '' === $path || strlen( $path ) > 255 || str_contains( $path, "\0" ) || str_contains( $path, '\\' ) || str_contains( $path, '//' ) || str_starts_with( $path, '/' ) || ! preg_match( '#^[A-Za-z0-9][A-Za-z0-9/._-]*$#', $path ) || preg_match( '#(^|/)\.\.?(/|$)#', $path ) ) { return self::bad_upload(); } $name = is_string( $data['name'] ) ? $data['name'] : ''; if ( '' === $name || strlen( $name ) > 255 || str_contains( $name, "\0" ) || preg_match( '/[\r\n]/', $name ) ) { return self::bad_upload(); } $mime = is_string( $data['mime'] ) ? strtolower( $data['mime'] ) : ''; if ( '' === $mime || strlen( $mime ) > 100 || ! preg_match( '#^[a-z0-9][a-z0-9.+-]*/[a-z0-9][a-z0-9.+-]*$#', $mime ) ) { return self::bad_upload(); } $sha = is_string( $data['sha256'] ) ? strtolower( $data['sha256'] ) : ''; if ( ! preg_match( '/^[a-f0-9]{64}$/', $sha ) ) { return self::bad_upload(); } if ( ! is_numeric( $data['size'] ) || (int) $data['size'] <= 0 ) { return self::bad_upload(); } if ( ! is_numeric( $data['exp'] ) || (int) $data['exp'] <= time() ) { return self::bad_upload(); } return [ 'path' => $path, 'name' => $name, 'mime' => $mime, 'size' => (int) $data['size'], 'sha256' => $sha, 'exp' => (int) $data['exp'], ]; } /** * Encode bytes as unpadded base64url. */ private static function base64url( string $raw ): string { return rtrim( strtr( base64_encode( $raw ), '+/', '-_' ), '=' ); } /** * Decode unpadded base64url strictly. */ private static function base64url_decode( string $data ): string|false { $data = strtr( $data, '-_', '+/' ); $pad = strlen( $data ) % 4; if ( 0 !== $pad ) { $data .= str_repeat( '=', 4 - $pad ); } return base64_decode( $data, true ); } /** * Single opaque error, so callers cannot probe which check failed. */ private static function bad_upload(): \WP_Error { return new \WP_Error( 'proofkit_bad_upload', __( 'The artwork upload token is invalid or has expired. Please upload the file again.', 'proofkit' ) ); } /** * Log through Util without ever letting logging break token handling. */ private static function log( string $message, array $context = [] ): void { try { Util::log( $message, $context ); } catch ( \Throwable ) { // Logging is best-effort only. } } }