Skip to content

Commit 24cc6e2

Browse files
committed
fix: Stack overflow on empty $ref value
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent 9469072 commit 24cc6e2

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Stack overflow on empty `$ref` value. [#886](https://github.com/Stranger6667/jsonschema/issues/886)
8+
59
## [0.37.0] - 2025-11-19
610

711
### Added

crates/jsonschema-py/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- Stack overflow on empty `$ref` value. [#886](https://github.com/Stranger6667/jsonschema/issues/886)
8+
59
## [0.37.0] - 2025-11-19
610

711
### Added

crates/jsonschema/src/keywords/ref_.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ fn compile_reference_validator<'a>(
2121
Err(error) => return Some(Err(error)),
2222
};
2323

24-
if alias == current_location {
24+
if alias == current_location
25+
|| (reference.is_empty() && alias.strip_fragment() == current_location.strip_fragment())
26+
{
2527
// Direct self-reference would recurse indefinitely, treat it as an annotation-only schema.
28+
// Empty string $ref ("") is a same-document reference per RFC 3986, equivalent to "#"
2629
return None;
2730
}
2831

@@ -573,4 +576,15 @@ mod tests {
573576
"Resource './virtualNetwork.json' is not present in a registry and retrieving it failed: No base URI is available"
574577
);
575578
}
579+
580+
#[test]
581+
fn test_empty_ref_no_stack_overflow() {
582+
// Empty string is a same-document reference per RFC 3986, should behave like $ref: "#"
583+
let schema = json!({"$ref": ""});
584+
let instance = json!(-1);
585+
586+
// Should compile without error and validate without stack overflow
587+
let validator = crate::validator_for(&schema).expect("Should compile");
588+
assert!(validator.is_valid(&instance));
589+
}
576590
}

0 commit comments

Comments
 (0)