Skip to content

Commit

Permalink
more secure unshortening and fixed bug with wrong unshortened url sho…
Browse files Browse the repository at this point in the history
…wing up and saving to history
  • Loading branch information
Visual-Studio-Coder committed Apr 18, 2024
1 parent 0d88eb2 commit a5e1026
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
4 changes: 2 additions & 2 deletions QRSharePro/HistoryDetailInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ struct HistoryDetailInfo: View {
}
}

Text(URL(string: qrCode.text)!.host!.replacingOccurrences(of: "www.", with: ""))
Text(URL(string: qrCode.text)!.prettify().host!.replacingOccurrences(of: "www.", with: ""))
.font(.largeTitle)
.bold()
.lineLimit(1)
Expand Down Expand Up @@ -424,7 +424,7 @@ struct HistoryDetailInfo: View {
}
}
}
.navigationTitle(URL(string: qrCode.text)!.host!.replacingOccurrences(of: "www.", with: ""))
.navigationTitle(URL(string: qrCode.text)!.prettify().host!.replacingOccurrences(of: "www.", with: ""))
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Expand Down
26 changes: 10 additions & 16 deletions QRSharePro/ObservableObject/QRScannerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class QRScannerViewModel: ObservableObject, QRScannerControllerDelegate {
@Published var qrCodeImage: UIImage?
@Published var qrCode: QRCode

@AppStorage("playHaptics") private var playHaptics = PlayHaptics.playHaptics

var qrCodeStore: QRCodeStore

func save() throws {
Expand Down Expand Up @@ -64,14 +62,14 @@ class QRScannerViewModel: ObservableObject, QRScannerControllerDelegate {
guard url != URL(string: lastDetectedString!) else { return }
lastDetectedString = string
self.detectedString = string

if playHaptics {
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
}
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))

let sanitizedURL = url.absoluteString.removeTrackers()

URLSession.shared.dataTask(with: URL(string: sanitizedURL)!.prettify()) { (data, response, error) in
let configuration = URLSessionConfiguration.ephemeral
let session = URLSession(configuration: configuration)

session.dataTask(with: URL(string: sanitizedURL)!) { (data, response, error) in
// prevent maliciously crafted qr codes + actually check we visited the page
guard error == nil else { return }
guard let response = response else { return }
Expand All @@ -90,11 +88,11 @@ class QRScannerViewModel: ObservableObject, QRScannerControllerDelegate {
print("Could not get user location.")
}

let newCode = QRCode(text: finalURL.prettify().absoluteString, originalURL: url.absoluteString, qrCode: pngData, scanLocation: userLocation, wasScanned: true)
let newCode = QRCode(text: finalURL.absoluteString, originalURL: url.absoluteString, qrCode: pngData, scanLocation: userLocation, wasScanned: true)

self.qrCodeStore.history.append(newCode)

self.detectedString = finalURL.prettify().absoluteString
self.detectedString = finalURL.absoluteString

Task {
do {
Expand All @@ -106,15 +104,13 @@ class QRScannerViewModel: ObservableObject, QRScannerControllerDelegate {
}

userLocation = [] // re-write user's location in memory
self.unshortenedURL = finalURL.prettify()
self.unshortenedURL = finalURL
}
}.resume()
} else if UIApplication.shared.canOpenURL(URL(string: string)!){
guard string != lastDetectedString else { return }

if playHaptics {
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
}
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))

generateQRCode(from: string)

Expand Down Expand Up @@ -149,9 +145,7 @@ class QRScannerViewModel: ObservableObject, QRScannerControllerDelegate {
} else {
guard string != lastDetectedString else { return }

if playHaptics {
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
}
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))

generateQRCode(from: string)

Expand Down

0 comments on commit a5e1026

Please sign in to comment.