Skip to content

[Datastore] Key::determineIdentifierType() misclassifies overflowing numeric and scientific-notation strings as TYPE_ID #9872

Backlog

Description

@pentium10

For humans

Summary

Google\Cloud\Datastore\Key::determineIdentifierType() uses PHP's is_numeric() to infer whether a key identifier should be treated as Key::TYPE_ID (int64) or Key::TYPE_NAME (string).

Because is_numeric() returns true for values that cannot be represented as a Datastore 64-bit signed integer—such as 32-digit numeric MD5 hashes (e.g. '13975337727204308302793171870910'), scientific notation hashes (e.g. md5('240610708') === '0e462097431906509019562988736854'), or floats—the SDK assigns id instead of name, and later crashes inside Protobuf when constructing PathElement:

Exception: Cannot convert '13975337727204308302793171870910' to integer
 at Google\Protobuf\Internal\Message::__construct(PathElement.php:64)
 at Google\Cloud\Datastore\V1\Key\PathElement::__construct(PathElement.php:64)
 at Google\Cloud\Datastore\Connection\Grpc::keysList(Grpc.php:387)
 at Google\Cloud\Datastore\Connection\Grpc::lookup(Grpc.php:203)

Environment

  • google/cloud-datastore: v1.34.2
  • google/protobuf: v5.36.2
  • PHP: 8.4-8.6

Steps to reproduce

<?php
use Google\Cloud\Datastore\Key;
use Google\Cloud\Datastore\V1\Key\PathElement;

$key = new Key('example-project');
// 32-digit numeric string (e.g. an all-digit MD5 hash) or scientific-notation MD5 like md5('240610708')
$key->pathElement('CacheEntry', '13975337727204308302793171870910');

// PathElement receives ['kind' => 'CacheEntry', 'id' => '13975337727204308302793171870910']
new PathElement($key->pathEnd()); // Throws: Exception: Cannot convert '13975337727204308302793171870910' to integer

Expected behavior

When identifierType is omitted, Key::determineIdentifierType() should only infer Key::TYPE_ID if the value is a valid signed 64-bit integer (not a float, scientific notation string, or integer overflowing int64). Values that cannot fit in int64 should either fall back to Key::TYPE_NAME or fail fast with a clear InvalidArgumentException when constructing the Key rather than crashing later during gRPC serialization.

Workaround

Explicitly pass ['identifierType' => Key::TYPE_NAME] when calling DatastoreClient::key() or Key::pathElement().


For agents

  • Repository / Package: googleapis/google-cloud-php (Datastore component, google/cloud-datastore)
  • Affected file & method: Datastore/src/Key.php -> Key::determineIdentifierType($identifier, $identifierType)
  • Current logic:
    if (is_numeric($identifier)) {
        return self::TYPE_ID;
    }
    return self::TYPE_NAME;
  • Failure mode: is_numeric() accepts floats ('12.34'), scientific notation ('0e462097431906509019562988736854'), and out-of-range integer strings ('13975337727204308302793171870910'). Google\Cloud\Datastore\V1\Key\PathElement defines id as protobuf int64, so Google\Protobuf\Internal\Message::__construct() throws Exception: Cannot convert '...' to integer at RPC time (Grpc::keysList() / Grpc::lookup()).
  • Suggested fix direction:
    1. Replace is_numeric($identifier) in Key::determineIdentifierType() with a strict signed 64-bit integer check (e.g., is_int($identifier) or a regex/range check matching a canonical base-10 integer within [-9223372036854775808, 9223372036854775807], excluding floats, scientific notation, and overflowing digit strings), or validate TYPE_ID bounds in Key::normalizeElement().
    2. Add unit tests in Datastore/tests/Unit/KeyTest.php covering:
      • 32-digit numeric strings exceeding PHP_INT_MAX ('13975337727204308302793171870910')
      • Scientific-notation strings ('0e462097431906509019562988736854')
      • Float strings ('12.34')
      • Valid 64-bit integer IDs as both int and string (preserving existing behavior).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions