Skip to content

Commit dd6af43

Browse files
authored
Merge pull request #286 from loopandlearn/url-sanitization
Sanitize and validate URL input to remove unwanted characters.
2 parents ac00c18 + 678b061 commit dd6af43

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

LoopFollow/ViewControllers/SettingsViewController.swift

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,29 @@ class SettingsViewController: FormViewController {
128128
guard let value = row.value else {
129129
UserDefaultsRepository.url.value = ""
130130
self.showHideNSDetails()
131-
return }
132-
// check the format of the URL entered by the user and trim away any spaces or "/" at the end
133-
var urlNSInput = value.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression)
134-
if urlNSInput.last == "/" {
135-
urlNSInput = String(urlNSInput.dropLast())
131+
return
136132
}
137-
UserDefaultsRepository.url.value = urlNSInput.lowercased()
138-
// set the row value back to the correctly formatted URL so that the user immediately sees how it should have been written
139-
row.value = UserDefaultsRepository.url.value
133+
134+
// Normalize input: remove unwanted characters and lowercase
135+
let filtered = value.replacingOccurrences(of: "[^A-Za-z0-9:/._-]", with: "", options: .regularExpression).lowercased()
136+
137+
// Further clean-up: Remove trailing slashes
138+
var cleanURL = filtered
139+
while cleanURL.last == "/" {
140+
cleanURL = String(cleanURL.dropLast())
141+
}
142+
143+
// Set the cleaned URL
144+
UserDefaultsRepository.url.value = cleanURL
145+
row.value = cleanURL
146+
140147
self.showHideNSDetails()
141148
globalVariables.nsVerifiedAlert = 0
142149

143150
// Verify Nightscout URL and token
144151
self.checkNightscoutStatus()
145152
}
153+
146154
<<< TextRow() { row in
147155
row.title = "NS Token"
148156
row.placeholder = "Leave blank if not using tokens"
@@ -174,15 +182,15 @@ class SettingsViewController: FormViewController {
174182
row.tag = "loopUser"
175183
row.value = UserDefaultsRepository.loopUser.value
176184
row.hidden = "$showNS == false"
177-
}.onChange { [weak self] row in
185+
}.onChange { row in
178186
guard let value = row.value else { return }
179187
UserDefaultsRepository.loopUser.value = value
180188
}
181189

182190
<<< SwitchRow("showDex"){ row in
183191
row.title = "Show Dexcom Settings"
184192
row.value = UserDefaultsRepository.showDex.value
185-
}.onChange { [weak self] row in
193+
}.onChange { row in
186194
guard let value = row.value else { return }
187195
UserDefaultsRepository.showDex.value = value
188196
}

0 commit comments

Comments
 (0)