Skip to content

Add more defensive bounds and input checks #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changes

## 2.0.0-beta.9

- **SECURITY**: Fixed integer overflow vulnerability in search tree size
calculation that could potentially allow malformed databases to trigger
security issues.
- **SECURITY**: Enhanced bounds checking in tree traversal functions to return
proper errors instead of silent failures when encountering malformed
databases.
- Added validation for invalid prefixes in `NetworksWithin` to prevent
unexpected behavior with malformed input.

## 2.0.0-beta.8 - 2025-07-15

- Fixed "no next offset available" error that occurred when using custom
Expand Down
4 changes: 4 additions & 0 deletions internal/decoder/data_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ func (d *DataDecoder) decodePointer(
pointerValueOffset = 526336
case 4:
pointerValueOffset = 0
default:
return 0, 0, mmdberrors.NewInvalidDatabaseError("invalid pointer size: %d", pointerSize)
}

pointer := unpacked + pointerValueOffset
Expand Down Expand Up @@ -477,6 +479,8 @@ func (d *DataDecoder) sizeFromCtrlByte(
size = 285 + uintFromBytes(0, sizeBytes)
case size > 30:
size = uintFromBytes(0, sizeBytes) + 65821
default:
// size < 30, no modification needed
}
return size, newOffset, nil
}
Expand Down
26 changes: 24 additions & 2 deletions internal/decoder/reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ func (d *ReflectionDecoder) unmarshalBool(
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
default:
// Fall through to error return
}
return newOffset, mmdberrors.NewUnmarshalTypeError(value, result.Type())
}
Expand All @@ -400,6 +402,8 @@ func (d *ReflectionDecoder) unmarshalBytes(
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
default:
// Fall through to error return
}
return newOffset, mmdberrors.NewUnmarshalTypeError(value, result.Type())
}
Expand All @@ -421,6 +425,8 @@ func (d *ReflectionDecoder) unmarshalFloat32(
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
default:
// Fall through to error return
}
return newOffset, mmdberrors.NewUnmarshalTypeError(value, result.Type())
}
Expand All @@ -445,6 +451,8 @@ func (d *ReflectionDecoder) unmarshalFloat64(
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
default:
// Fall through to error return
}
return newOffset, mmdberrors.NewUnmarshalTypeError(value, result.Type())
}
Expand Down Expand Up @@ -481,6 +489,8 @@ func (d *ReflectionDecoder) unmarshalInt32(
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
default:
// Fall through to error return
}
return newOffset, mmdberrors.NewUnmarshalTypeError(value, result.Type())
}
Expand All @@ -492,8 +502,6 @@ func (d *ReflectionDecoder) unmarshalMap(
depth int,
) (uint, error) {
switch result.Kind() {
default:
return 0, mmdberrors.NewUnmarshalTypeStrError("map", result.Type())
case reflect.Struct:
return d.decodeStruct(size, offset, result, depth)
case reflect.Map:
Expand All @@ -508,6 +516,8 @@ func (d *ReflectionDecoder) unmarshalMap(
return newOffset, err
}
return 0, mmdberrors.NewUnmarshalTypeStrError("map", result.Type())
default:
return 0, mmdberrors.NewUnmarshalTypeStrError("map", result.Type())
}
}

Expand Down Expand Up @@ -556,6 +566,8 @@ func (d *ReflectionDecoder) unmarshalSlice(
result.Set(rv.Value)
return newOffset, err
}
default:
// Fall through to error return
}
return 0, mmdberrors.NewUnmarshalTypeStrError("array", result.Type())
}
Expand All @@ -578,6 +590,8 @@ func (d *ReflectionDecoder) unmarshalString(
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
default:
// Fall through to error return
}
return newOffset, mmdberrors.NewUnmarshalTypeError(value, result.Type())
}
Expand Down Expand Up @@ -632,6 +646,8 @@ func (d *ReflectionDecoder) unmarshalUint(
result.SetUint(value)
return newOffset, nil
}
default:
// Fall through to general unmarshaling logic
}

switch result.Kind() {
Expand All @@ -656,6 +672,8 @@ func (d *ReflectionDecoder) unmarshalUint(
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
default:
// Fall through to error return
}
return newOffset, mmdberrors.NewUnmarshalTypeError(value, result.Type())
}
Expand Down Expand Up @@ -691,6 +709,8 @@ func (d *ReflectionDecoder) unmarshalUint128(
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
default:
// Fall through to error return
}
return newOffset, mmdberrors.NewUnmarshalTypeError(value, result.Type())
}
Expand Down Expand Up @@ -1210,6 +1230,8 @@ func (d *ReflectionDecoder) tryFastDecodeTyped(
addressableValue{result.Elem(), false},
expectedType.Elem(),
)
default:
// Type not supported for fast path
}

return 0, false
Expand Down
Loading
Loading