Skip to content

Commit 60af1a1

Browse files
committed
Secure cookies only when HTTPS expected. (Not with remote Selenium)
1 parent cf421c0 commit 60af1a1

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

app/assets/javascripts/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,16 @@ $(function () {
171171
map.getLayersCode(),
172172
map._object);
173173

174-
Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
174+
OSM.COOKIES.set("_osm_location", OSM.locationCookie(map), { expires: expiry });
175175
});
176176

177-
if (Cookies.get("_osm_welcome") !== "hide") {
177+
if (OSM.COOKIES.get("_osm_welcome") !== "hide") {
178178
$(".welcome").removeAttr("hidden");
179179
}
180180

181181
$(".welcome .btn-close").on("click", function () {
182182
$(".welcome").hide();
183-
Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
183+
OSM.COOKIES.set("_osm_welcome", "hide", { expires: expiry });
184184
});
185185

186186
const bannerExpiry = new Date();
@@ -191,7 +191,7 @@ $(function () {
191191
$("#banner").hide();
192192
e.preventDefault();
193193
if (cookieId) {
194-
Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
194+
OSM.COOKIES.set(cookieId, "hide", { expires: bannerExpiry });
195195
}
196196
});
197197

app/assets/javascripts/index/directions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,17 @@ OSM.Directions = function (map) {
137137
}
138138

139139
setEngine("fossgis_osrm_car");
140-
setEngine(Cookies.get("_osm_directions_engine"));
140+
setEngine(OSM.COOKIES.get("_osm_directions_engine"));
141141

142142
modeGroup.on("change", "input[name='modes']", function (e) {
143143
setEngine(chosenEngine.provider + "_" + e.target.value);
144-
Cookies.set("_osm_directions_engine", chosenEngine.id, { secure: true, expires: expiry, path: "/", samesite: "lax" });
144+
OSM.COOKIES.set("_osm_directions_engine", chosenEngine.id, { expires: expiry });
145145
getRoute(true, true);
146146
});
147147

148148
select.on("change", function (e) {
149149
setEngine(e.target.value + "_" + chosenEngine.mode);
150-
Cookies.set("_osm_directions_engine", chosenEngine.id, { secure: true, expires: expiry, path: "/", samesite: "lax" });
150+
OSM.COOKIES.set("_osm_directions_engine", chosenEngine.id, { expires: expiry });
151151
getRoute(true, true);
152152
});
153153

app/assets/javascripts/index/new_note.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ OSM.NewNote = function (map) {
146146

147147
createNote(location, text, (feature) => {
148148
if (typeof OSM.user === "undefined") {
149-
const anonymousNotesCount = Number(Cookies.get("_osm_anonymous_notes_count")) || 0;
150-
Cookies.set("_osm_anonymous_notes_count", anonymousNotesCount + 1, { secure: true, expires: 30, path: "/", samesite: "lax" });
149+
const anonymousNotesCount = Number(OSM.COOKIES.get("_osm_anonymous_notes_count")) || 0;
150+
OSM.COOKIES.set("_osm_anonymous_notes_count", anonymousNotesCount + 1, { expires: 30 });
151151
}
152152
content.find("textarea").val("");
153153
addCreatedNoteMarker(feature);

app/assets/javascripts/language_selector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $(document).on("click", "#select_language_dialog [data-language-code]", function
88
form.elements.language.value = code;
99
form.submit();
1010
} else {
11-
Cookies.set("_osm_locale", code, { path: "/", samesite: "lax" });
11+
OSM.COOKIES.set("_osm_locale", code);
1212
location.reload();
1313
}
1414
});

app/assets/javascripts/osm.js.erb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
//= depend_on layers.yml
44
//= depend_on legend.yml
55

6+
// Temporary variable to avoid a circular dependency.
7+
let _COOKIES = Cookies.withAttributes({ path: "/", samesite: "lax", secure: <%= (Settings.server_protocol == "https").to_json %> });
8+
69
OSM = {
710
...<%=
811
%i[
@@ -27,6 +30,8 @@ OSM = {
2730
end.to_json
2831
%>,
2932

33+
COOKIES: _COOKIES,
34+
3035
DEFAULT_LOCALE: <%= I18n.default_locale.to_json %>,
3136

3237
LAYER_DEFINITIONS: <%= MapLayers::full_definitions("config/layers.yml", :legends => "config/legend.yml").to_json %>,
@@ -73,7 +78,7 @@ OSM = {
7378

7479
const hash = OSM.parseHash();
7580

76-
const loc = Cookies.get("_osm_location")?.split("|");
81+
const loc = _COOKIES.get("_osm_location")?.split("|");
7782

7883
function bboxToLatLngBounds({ minlon, minlat, maxlon, maxlat }) {
7984
return L.latLngBounds([minlat, minlon], [maxlat, maxlon]);
@@ -228,3 +233,7 @@ OSM = {
228233
alertModal.show();
229234
}
230235
};
236+
237+
// This value now lives in its final location.
238+
// This reference can be deleted.
239+
_COOKIES = null;

0 commit comments

Comments
 (0)