Skip to content

Commit fc36e5f

Browse files
Fix geo-lookup hostcall invocation (#46)
When changing Geolocation lookups to using the hostcall, I didn't correctly translate inputs to the required format—octet bytes instead of a string representation. This commit fixes that. Fixes #42
1 parent 25a21ab commit fc36e5f

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

c-dependencies/js-compute-runtime/js-compute-builtins.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3009,9 +3009,28 @@ static JSString* get_geo_info(JSContext* cx, HandleString address_str) {
30093009
UniqueChars address = encode(cx, address_str, &address_len);
30103010
if (!address) return nullptr;
30113011

3012+
int format = AF_INET;
3013+
size_t octets_len = 4;
3014+
const char* caddress = address.get();
3015+
for (size_t i = 0; i < address_len; i++) {
3016+
if (caddress[i] == ':') {
3017+
format = AF_INET6;
3018+
octets_len = 16;
3019+
break;
3020+
}
3021+
}
3022+
3023+
char octets[sizeof(struct in6_addr)];
3024+
if (inet_pton(format, caddress, octets) != 1) {
3025+
// While get_geo_info can be invoked through FetchEvent#client.geo, too, that path can't
3026+
// result in an invalid address here, so we can be more specific in the error message.
3027+
JS_ReportErrorLatin1(cx, "Invalid address passed to fastly.getGeolocationForIpAddress");
3028+
return nullptr;
3029+
}
3030+
30123031
OwnedHostCallBuffer buffer;
30133032
size_t nwritten = 0;
3014-
if (!HANDLE_RESULT(cx, xqd_geo_lookup(address.get(), address_len, buffer.get(),
3033+
if (!HANDLE_RESULT(cx, xqd_geo_lookup(octets, octets_len, buffer.get(),
30153034
HOSTCALL_BUFFER_LEN, &nwritten)))
30163035
{
30173036
return nullptr;

0 commit comments

Comments
 (0)