diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-07-20 17:10:49 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-07-20 17:10:49 +0900 |
| commit | 5ef607e2afe7eaf7e2ccdf62f3ffacfede3c5fca (patch) | |
| tree | ca46c8f66cbe5af345ec2e08c9a12c843ffe81b2 /crates | |
| parent | c175989cfa8b9558373edd1bd0696fd2a68431c2 (diff) | |
| download | php-shirabe-5ef607e2afe7eaf7e2ccdf62f3ffacfede3c5fca.tar.gz php-shirabe-5ef607e2afe7eaf7e2ccdf62f3ffacfede3c5fca.tar.zst php-shirabe-5ef607e2afe7eaf7e2ccdf62f3ffacfede3c5fca.zip | |
fix(no-proxy-pattern): stop chr() corrupting IP bytes as lossy UTF-8
shirabe_php_shim::chr() returned a Rust String, which lossily
re-encodes bytes >= 0x80 as UTF-8 replacement characters. ip_get_mask,
ip_get_network, and ip_map_to_6 relied on chr() to build raw in_addr
and netmask byte arrays, corrupting IPv4-in-IPv6 mappings and CIDR
netmasks. Build the Vec<u8> byte arrays directly instead of
round-tripping through String, and un-ignore test_ip_address and
test_ip_range now that the underlying bug is fixed.
chr()'s only other caller (http_downloader.rs, an ASCII ESC byte in a
regex pattern) didn't need the indirection either, so remove the shim
function entirely.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/shirabe-php-shim/src/string.rs | 6 | ||||
| -rw-r--r-- | crates/shirabe/src/util/http_downloader.rs | 12 | ||||
| -rw-r--r-- | crates/shirabe/src/util/no_proxy_pattern.rs | 28 | ||||
| -rw-r--r-- | crates/shirabe/tests/util/no_proxy_pattern_test.rs | 2 |
4 files changed, 16 insertions, 32 deletions
diff --git a/crates/shirabe-php-shim/src/string.rs b/crates/shirabe-php-shim/src/string.rs index 97c66953..2617399f 100644 --- a/crates/shirabe-php-shim/src/string.rs +++ b/crates/shirabe-php-shim/src/string.rs @@ -1289,12 +1289,6 @@ pub fn ucfirst(s: &str) -> String { } } -pub fn chr(_value: u8) -> String { - // TODO(phase-d): PHP chr() yields a single raw byte; for values >= 0x80 that byte is not valid - // UTF-8, so storing it in a Rust String forces a lossy substitution. - String::from_utf8_lossy(&[_value]).into_owned() -} - // Port of PHP's addcslashes: every byte that falls in the (range-expanded) charlist is // backslash-escaped, with non-printable bytes rendered as the C escape or a three-digit octal. pub fn addcslashes(_string: &str, _charlist: &str) -> String { diff --git a/crates/shirabe/src/util/http_downloader.rs b/crates/shirabe/src/util/http_downloader.rs index b055a515..0c91484d 100644 --- a/crates/shirabe/src/util/http_downloader.rs +++ b/crates/shirabe/src/util/http_downloader.rs @@ -18,9 +18,9 @@ use crate::util::sync_executor; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ - InvalidArgumentException, LogicException, PhpMixed, array_replace_recursive, chr, - extension_loaded, file_get_contents, function_exists, implode, is_numeric, php_regex, - rawurldecode, stream_context_create, stripos, strpos, substr, ucfirst, + InvalidArgumentException, LogicException, PhpMixed, array_replace_recursive, extension_loaded, + file_get_contents, function_exists, implode, is_numeric, php_regex, rawurldecode, + stream_context_create, stripos, strpos, substr, ucfirst, }; use shirabe_semver::constraint::SimpleConstraint; @@ -381,11 +381,7 @@ impl HttpDownloader { ) -> anyhow::Result<()> { let clean_message = |msg: &str| -> anyhow::Result<String> { if !io.is_decorated() { - return Ok(Preg::replace( - format!("{{{}{}}}u", chr(27), "\\[[;\\d]*m"), - "", - msg, - )); + return Ok(Preg::replace("{\x1b\\[[;\\d]*m}u", "", msg)); } Ok(msg.to_string()) diff --git a/crates/shirabe/src/util/no_proxy_pattern.rs b/crates/shirabe/src/util/no_proxy_pattern.rs index dc91a256..a33f2451 100644 --- a/crates/shirabe/src/util/no_proxy_pattern.rs +++ b/crates/shirabe/src/util/no_proxy_pattern.rs @@ -3,9 +3,9 @@ use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_shim::{ - PHP_URL_HOST, PHP_URL_PORT, PHP_URL_SCHEME, PhpMixed, RuntimeException, array_key_exists, chr, + PHP_URL_HOST, PHP_URL_PORT, PHP_URL_SCHEME, PhpMixed, RuntimeException, array_key_exists, empty, explode, filter_var_int_with_range, filter_var_ip, inet_pton, ltrim, parse_url, - php_regex, str_pad, str_repeat, stripos, strlen, strpbrk, strpos, substr, substr_count, unpack, + php_regex, stripos, strlen, strpbrk, strpos, substr, substr_count, unpack, }; /// Tests URLs against NO_PROXY patterns @@ -315,26 +315,23 @@ impl NoProxyPattern { /// @param int $prefix CIDR prefix-length /// @param int $size Byte size of in_addr fn ip_get_mask(&self, prefix: i64, size: i64) -> Vec<u8> { - let mut mask = String::new(); + let mut mask: Vec<u8> = Vec::new(); let ones = (prefix as f64 / 8.0).floor() as i64; if ones != 0 { - mask = str_repeat(&chr(255), ones as usize); + mask.extend(vec![0xffu8; ones as usize]); } let remainder = prefix % 8; if remainder != 0 { - mask.push_str(&chr(0xff ^ (0xff >> remainder))); + mask.push(0xff ^ (0xff >> remainder)); } - let mask = str_pad( - &mask, - size as usize, - &chr(0), - shirabe_php_shim::STR_PAD_RIGHT, - ); + if (size as usize) > mask.len() { + mask.resize(size as usize, 0); + } - self.ip_map_to_6(mask.as_bytes(), size) + self.ip_map_to_6(&mask, size) } /// Calculates and returns the network and mask @@ -389,8 +386,7 @@ impl NoProxyPattern { .get(&i.to_string()) .and_then(|v| v.as_int()) .unwrap_or(0); - // PHP: $net .= chr($ip[$i] & $mask[$i]); - net.extend(chr((ip_byte & mask_byte) as u8).as_bytes()); + net.push((ip_byte & mask_byte) as u8); } Ok((net, netmask)) @@ -404,8 +400,8 @@ impl NoProxyPattern { /// @return string Mapped or existing in_addr fn ip_map_to_6(&self, binary: &[u8], size: i64) -> Vec<u8> { if size == 4 { - let mut prefix = str_repeat(&chr(0), 10).into_bytes(); - prefix.extend(str_repeat(&chr(255), 2).into_bytes()); + let mut prefix = vec![0u8; 10]; + prefix.extend(vec![0xffu8; 2]); prefix.extend_from_slice(binary); return prefix; } diff --git a/crates/shirabe/tests/util/no_proxy_pattern_test.rs b/crates/shirabe/tests/util/no_proxy_pattern_test.rs index e59e28a6..63f0d5f6 100644 --- a/crates/shirabe/tests/util/no_proxy_pattern_test.rs +++ b/crates/shirabe/tests/util/no_proxy_pattern_test.rs @@ -42,7 +42,6 @@ fn test_host_name() { } #[test] -#[ignore = "shirabe_php_shim::chr() lossily re-encodes bytes >= 0x80 as UTF-8 (TODO(phase-d) in its own doc comment); ip_map_to_6's chr(255) filler bytes become 3-byte U+FFFD replacements, corrupting the mapped IPv4-in-IPv6 byte arrays used for comparison"] fn test_ip_address() { let noproxy = "192.168.1.1, 2001:db8::52:0:1"; @@ -55,7 +54,6 @@ fn test_ip_address() { } #[test] -#[ignore = "shirabe_php_shim::chr() lossily re-encodes bytes >= 0x80 as UTF-8 (TODO(phase-d) in its own doc comment); ip_get_mask's chr(255)/chr(0xff^...) calls corrupt the CIDR netmask bytes used in match_range, breaking all prefix-based no_proxy matching"] fn test_ip_range() { let noproxy = "10.0.0.0/30, 2002:db8:a::45/121"; |
