Skip to content

Commit

Permalink
Fix to deny invalid field names. (#2414)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarutak authored Aug 4, 2023
1 parent e16a081 commit db359f5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lang/rust/avro/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ pub enum Error {
#[error("Invalid enum symbol name {0}")]
EnumSymbolName(String),

#[error("Invalid field name {0}")]
FieldName(String),

#[error("Invalid schema name {0}. It must match the regex '{1}'")]
InvalidSchemaName(String, &'static str),

Expand Down
38 changes: 38 additions & 0 deletions lang/rust/avro/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ lazy_static! {
// An optional namespace (with optional dots) followed by a name without any dots in it.
static ref SCHEMA_NAME_R: Regex =
Regex::new(r"^((?P<namespace>[A-Za-z_][A-Za-z0-9_\.]*)*\.)?(?P<name>[A-Za-z_][A-Za-z0-9_]*)$").unwrap();

static ref FIELD_NAME_R: Regex = Regex::new(r"^[A-Za-z_][A-Za-z0-9_]*$").unwrap();
}

/// Represents an Avro schema fingerprint
Expand Down Expand Up @@ -622,6 +624,10 @@ impl RecordField {
) -> AvroResult<Self> {
let name = field.name().ok_or(Error::GetNameFieldFromRecord)?;

if !FIELD_NAME_R.is_match(&name) {
return Err(Error::FieldName(name));
}

// TODO: "type" = "<record name>"
let schema = parser.parse_complex(field, enclosing_namespace)?;

Expand Down Expand Up @@ -5026,4 +5032,36 @@ mod tests {

Ok(())
}

#[test]
fn test_avro_3820_deny_invalid_field_names() -> TestResult {
let schema_str = r#"
{
"name": "my_record",
"type": "record",
"fields": [
{
"name": "f1.x",
"type": {
"name": "my_enum",
"type": "enum",
"symbols": ["a"]
}
}, {
"name": "f2",
"type": {
"name": "my_fixed",
"type": "fixed",
"size": 1
}
}
]
}
"#;

match Schema::parse_str(schema_str) {
Err(Error::FieldName(x)) if x == "f1.x" => Ok(()),
other => Err(format!("Expected Error::FieldName, got {other:?}").into()),
}
}
}

0 comments on commit db359f5

Please sign in to comment.