Skip to content

Commit 5e420fc

Browse files
committed
chore: update string validation
1 parent fb27b61 commit 5e420fc

3 files changed

Lines changed: 50 additions & 20 deletions

File tree

src/types/strings/string16.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,27 @@ impl String16 {
2323
* Reads an unsigned 16-bit ( 2 bytes ) utf-8 string from the stream. ( 0 to 65535 )
2424
*/
2525
pub fn read(stream: &mut BinaryStream, endian: Option<Endianness>) -> Result<String> {
26+
// Read the length of the string.
2627
let length = match Uint16::read(stream, endian) {
27-
Ok(value) => value as u32,
28+
Ok(value) => value,
2829
Err(err) => return Err(err)
2930
};
3031

31-
let buffer = match stream.read(length) {
32-
Ok(bytes) => bytes,
33-
Err(err) => return Err(err)
34-
};
32+
// Length validation
33+
let start = stream.offset as usize;
34+
let end = start + length as usize;
35+
if end > stream.binary.len() {
36+
return Err(
37+
napi::Error::new(
38+
napi::Status::GenericFailure,
39+
"String length exceeds available bytes in the stream.".to_string()
40+
)
41+
);
42+
}
3543

36-
let value = String::from_utf8_lossy(&buffer).to_string();
44+
// Read the string from the binary stream.
45+
let value = String::from_utf8_lossy(&stream.binary[start..end]).to_string();
46+
stream.offset += length as u32;
3747

3848
Ok(value)
3949
}

src/types/strings/string32.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,27 @@ impl String32 {
2323
* Reads a signed 32-bit ( 4 bytes ) utf-8 string from the stream. ( 0 to 4294967295 )
2424
*/
2525
pub fn read(stream: &mut BinaryStream, endian: Option<Endianness>) -> Result<String> {
26+
// Read the length of the string.
2627
let length = match Uint32::read(stream, endian) {
27-
Ok(value) => value,
28+
Ok(value) => value as usize,
2829
Err(err) => return Err(err)
2930
};
3031

31-
let buffer = match stream.read(length) {
32-
Ok(bytes) => bytes,
33-
Err(err) => return Err(err)
34-
};
32+
// Length validation
33+
let start = stream.offset as usize;
34+
let end = start + length as usize;
35+
if end > stream.binary.len() {
36+
return Err(
37+
napi::Error::new(
38+
napi::Status::GenericFailure,
39+
"String length exceeds available bytes in the stream.".to_string()
40+
)
41+
);
42+
}
3543

36-
let value = String::from_utf8_lossy(&buffer).to_string();
44+
// Read the string from the binary stream.
45+
let value = String::from_utf8_lossy(&stream.binary[start..end]).to_string();
46+
stream.offset += length as u32;
3747

3848
Ok(value)
3949
}
@@ -55,4 +65,4 @@ impl FromNapiValue for String32 {
5565
unsafe fn from_napi_value(_: napi::sys::napi_env, _: napi::sys::napi_value) -> Result<Self> {
5666
Ok(String32 {})
5767
}
58-
}
68+
}

src/types/strings/varstring.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,27 @@ impl VarString {
2222
* Reads a signed 32-bit ( 4 bytes ) utf-8 string from the stream. ( 0 to 4294967295 )
2323
*/
2424
pub fn read(stream: &mut BinaryStream) -> Result<String> {
25+
// Read the length of the string.
2526
let length = match VarInt::read(stream) {
26-
Ok(value) => value,
27+
Ok(value) => value as usize,
2728
Err(err) => return Err(err)
2829
};
2930

30-
let buffer = match stream.read(length) {
31-
Ok(bytes) => bytes,
32-
Err(err) => return Err(err)
33-
};
31+
// Length validation
32+
let start = stream.offset as usize;
33+
let end = start + length as usize;
34+
if end > stream.binary.len() {
35+
return Err(
36+
napi::Error::new(
37+
napi::Status::GenericFailure,
38+
"String length exceeds available bytes in the stream.".to_string()
39+
)
40+
);
41+
}
3442

35-
let value = String::from_utf8_lossy(&buffer).to_string();
43+
// Read the string from the binary stream.
44+
let value = String::from_utf8_lossy(&stream.binary[start..end]).to_string();
45+
stream.offset += length as u32;
3646

3747
Ok(value)
3848
}
@@ -55,4 +65,4 @@ impl FromNapiValue for VarString {
5565
unsafe fn from_napi_value(_: napi::sys::napi_env, _: napi::sys::napi_value) -> Result<Self> {
5666
Ok(VarString {})
5767
}
58-
}
68+
}

0 commit comments

Comments
 (0)