Skip to content

Commit 3c3050a

Browse files
authored
Merge pull request #45 from iFixit/fix--restrict-google-maps-key
Fix: Use two different keys for Google Maps
2 parents 5a3fed6 + bc2da21 commit 3c3050a

10 files changed

Lines changed: 33 additions & 17 deletions

File tree

.env.base

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ SEND_COMMAND_LOGS_TO=tech@yoursite.org
161161
# MAPS INTEGRATION
162162
# =============================================================================
163163
MAPBOX_TOKEN=1234
164-
GOOGLE_API_CONSOLE_KEY=1234
164+
# Google Maps API Keys - SECURITY: Separate restricted keys for different purposes
165+
# Website-restricted key for Maps JavaScript API and Places API (frontend use)
166+
GOOGLE_MAPS_FRONTEND_KEY=1234
167+
# IP-restricted key for Geocoding API and Time Zone API (backend use)
168+
GOOGLE_MAPS_BACKEND_KEY=1234
165169

166170
# =============================================================================
167171
# MONITORING AND ANALYTICS

.env.template

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ SEND_COMMAND_LOGS_TO="$SEND_COMMAND_LOGS_TO"
161161
# MAPS INTEGRATION
162162
# =============================================================================
163163
MAPBOX_TOKEN="$MAPBOX_TOKEN"
164-
GOOGLE_API_CONSOLE_KEY="$GOOGLE_API_CONSOLE_KEY"
164+
# Google Maps API Keys - SECURITY: Separate restricted keys for different purposes
165+
# Website-restricted key for Maps JavaScript API and Places API (frontend use)
166+
GOOGLE_MAPS_FRONTEND_KEY="$GOOGLE_MAPS_FRONTEND_KEY"
167+
# IP-restricted key for Geocoding API and Time Zone API (backend use)
168+
GOOGLE_MAPS_BACKEND_KEY="$GOOGLE_MAPS_BACKEND_KEY"
165169

166170
# =============================================================================
167171
# MONITORING AND ANALYTICS

app/Helpers/Geocoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function __construct()
1111
private function googleKey()
1212
{
1313
// We have this so that we can change the key in testing.
14-
return config('GOOGLE_API_CONSOLE_KEY') ?? env('GOOGLE_API_CONSOLE_KEY');
14+
return config('GOOGLE_MAPS_BACKEND_KEY') ?? env('GOOGLE_MAPS_BACKEND_KEY');
1515
}
1616

1717
public function geocode($location)

app/Http/Controllers/API/TimeZoneController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function lookup(Request $request)
1818
return response()->json(['error' => 'Missing lat/lng'], 400);
1919
}
2020

21-
$apiKey = env('GOOGLE_API_CONSOLE_KEY');
21+
$apiKey = env('GOOGLE_MAPS_BACKEND_KEY');
2222
$url = "https://maps.googleapis.com/maps/api/timezone/json?location={$lat},{$lng}&timestamp={$timestamp}&key={$apiKey}";
2323

2424
$response = Http::get($url);

charts/restarters/templates/_helpers.tpl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,16 @@ Helper to generate environment variables from secrets
6060
secretKeyRef:
6161
name: {{ .Values.secrets.mapKeys.secretName }}
6262
key: {{ .Values.secrets.mapKeys.keys.mapboxToken }}
63-
- name: GOOGLE_API_CONSOLE_KEY
63+
- name: GOOGLE_MAPS_FRONTEND_KEY
6464
valueFrom:
6565
secretKeyRef:
6666
name: {{ .Values.secrets.mapKeys.secretName }}
67-
key: {{ .Values.secrets.mapKeys.keys.googleApiKey }}
67+
key: {{ .Values.secrets.mapKeys.keys.googleMapsFrontendKey }}
68+
- name: GOOGLE_MAPS_BACKEND_KEY
69+
valueFrom:
70+
secretKeyRef:
71+
name: {{ .Values.secrets.mapKeys.secretName }}
72+
key: {{ .Values.secrets.mapKeys.keys.googleMapsBackendKey }}
6873
{{- end }}
6974
- name: DB_HOST
7075
valueFrom:

charts/restarters/values.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,12 @@ secrets:
177177
secretName: "restarters-map-keys"
178178
keys:
179179
mapboxToken: "MAPBOX_TOKEN"
180-
googleApiKey: "GOOGLE_API_CONSOLE_KEY"
180+
googleMapsFrontendKey: "GOOGLE_MAPS_FRONTEND_KEY"
181+
googleMapsBackendKey: "GOOGLE_MAPS_BACKEND_KEY"
181182
data:
182183
mapboxToken: "your-mapbox-token-here"
183-
googleApiKey: "your-google-api-key-here"
184+
googleMapsFrontendKey: "your-google-frontend-key-here"
185+
googleMapsBackendKey: "your-google-backend-key-here"
184186
# External database credentials
185187
# IMPORTANT: For production deployments, create the secret externally and set createSecret: false
186188
# For development, you can set createSecret: true and provide values in the data section
@@ -341,7 +343,8 @@ envGroups:
341343
# Maps integration
342344
mapKeys:
343345
MAPBOX_TOKEN: ""
344-
GOOGLE_API_CONSOLE_KEY: ""
346+
GOOGLE_MAPS_FRONTEND_KEY: ""
347+
GOOGLE_MAPS_BACKEND_KEY: ""
345348

346349
# Monitoring and analytics
347350
monitoring:

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# (or via Docker Desktop's UI on Windows). Check for any obvious errors.
2323
#
2424
# Then:
25-
# - edit .env and set GOOGLE_API_CONSOLE_KEY to the dev key.
25+
# - edit .env and set GOOGLE_MAPS_FRONTEND_KEY and GOOGLE_MAPS_BACKEND_KEY to the appropriate restricted keys.
2626
#
2727
# If you want to remove everything to free up disk space or force a complete rebuild (e.g. as a sanity check
2828
# after changing this configuration):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<script src="https://maps.googleapis.com/maps/api/js?v=3&key={{ env('GOOGLE_API_CONSOLE_KEY') }}&libraries=places"></script>
1+
<script src="https://maps.googleapis.com/maps/api/js?v=3&key={{ env('GOOGLE_MAPS_FRONTEND_KEY') }}&libraries=places"></script>

tests/Feature/Users/EditProfileTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public function test_location_update(): void
110110
$this->assertEquals(51.507, round($user->latitude, 3));
111111
$this->assertEquals(-0.128, round($user->longitude, 3));
112112

113-
$good = Config::get('GOOGLE_API_CONSOLE_KEY');
114-
Config::set('GOOGLE_API_CONSOLE_KEY', 'zzz');
113+
$good = Config::get('GOOGLE_MAPS_BACKEND_KEY');
114+
Config::set('GOOGLE_MAPS_BACKEND_KEY', 'zzz');
115115

116116
// Supply the id.
117117
$this->post('/profile/edit-info', [
@@ -123,7 +123,7 @@ public function test_location_update(): void
123123
'townCity' => 'ZZZZ',
124124
]);
125125

126-
Config::set('GOOGLE_API_CONSOLE_KEY', $good);
126+
Config::set('GOOGLE_MAPS_BACKEND_KEY', $good);
127127

128128
$user = $user->fresh();
129129
$this->assertNull($user->latitude);

tests/Feature/Users/Registration/AccountCreationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public function testRegisterInvalidAddress(): void
5151
$userAttributes = $this->userAttributes();
5252

5353
// Specify an invalid city and force geocoding to fail by invalidating the Google key.
54-
$good = Config::get('GOOGLE_API_CONSOLE_KEY');
55-
Config::set('GOOGLE_API_CONSOLE_KEY', 'zzz');
54+
$good = Config::get('GOOGLE_MAPS_BACKEND_KEY');
55+
Config::set('GOOGLE_MAPS_BACKEND_KEY', 'zzz');
5656

5757
$userAttributes['city'] = 'zzzzzzz';
5858
$response = $this->post('/user/register/', $userAttributes);
5959

60-
Config::set('GOOGLE_API_CONSOLE_KEY', $good);
60+
Config::set('GOOGLE_MAPS_BACKEND_KEY', $good);
6161

6262
$response->assertStatus(302);
6363
$response->assertRedirect('dashboard');

0 commit comments

Comments
 (0)