self::TYPE_FILE, 'n' => $name, 's' => $size, 'm' => $mtime, ), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); if ( false === $json ) { throw new \RuntimeException( 'Cannot encode an archive entry header.' ); } return pack( 'N', strlen( $json ) ) . $json; } public static function eof( int $entries ): string { $json = wp_json_encode( array( 't' => self::TYPE_EOF, 'n' => '', 's' => 0, 'm' => time(), 'c' => $entries, ) ); return pack( 'N', strlen( (string) $json ) ) . $json; } /** * Whether an entry name is safe to join onto an extraction directory. * * Rejects anything that could escape it: parent segments, absolute paths, drive * letters, backslashes (which are separators on Windows), stream wrappers and NUL. * Names inside a trusted archive are still treated as hostile input. */ public static function isSafeName( string $name ): bool { if ( '' === $name || strlen( $name ) > 4096 ) { return false; } if ( false !== strpos( $name, "\0" ) || false !== strpos( $name, '\\' ) || '/' === $name[0] ) { return false; } if ( false !== strpos( $name, ':' ) ) { return false; } foreach ( explode( '/', $name ) as $segment ) { if ( '' === $segment || '.' === $segment || '..' === $segment || strlen( $segment ) > 255 ) { return false; } } return true; } /** * Only these top-level names are ever extracted. */ public static function isAllowedName( string $name ): bool { if ( self::PACKAGE === $name || self::DATABASE === $name ) { return true; } return 0 === strpos( $name, self::CONTENT_DIR . '/' ) && strlen( $name ) > strlen( self::CONTENT_DIR ) + 1; } }