4096 ) { throw new \RuntimeException( 'Invalid server document root metadata.' ); } $documentRoot = sanitize_text_field( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) ); // An exclusion boundary must never be silently rewritten or dropped. // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- This is only the size/unchanged-byte guard around the immediately sanitized document-root exclusion boundary; changed metadata is refused, never accepted as a storage path. if ( $documentRoot !== wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) || str_contains( $documentRoot, "\0" ) ) { throw new \RuntimeException( 'Invalid server document root metadata.' ); } if ( '' !== $documentRoot ) { if ( '/' !== $documentRoot[0] || ! is_dir( $documentRoot ) || false === realpath( $documentRoot ) ) { throw new \RuntimeException( 'Server document root must identify an existing absolute directory.' ); } $roots[] = rtrim( $documentRoot, '/' ) ?: '/'; } } if ( function_exists( 'wp_upload_dir' ) ) { $uploads = wp_upload_dir( null, false ); if ( ! empty( $uploads['basedir'] ) ) { $roots[] = $uploads['basedir']; } } foreach ( $roots as $root ) { foreach ( array_unique( array( rtrim( (string) $root, '/' ), realpath( (string) $root ) ?: rtrim( (string) $root, '/' ) ) ) as $candidate ) { if ( PathGuard::beneath( $dir, $candidate ) || PathGuard::beneath( $candidate, $dir ) ) { throw new \RuntimeException( 'Early-filter storage must be separate from every served and WordPress directory.' ); } } } for ( $path = $dir; '/' !== $path; $path = dirname( $path ) ) { if ( 0 !== ( fileperms( $path ) & 022 ) ) { throw new \RuntimeException( 'Private storage ancestors must not be writable by other accounts.' ); } } return $dir; } }