Fix bug in editing code with username in non-ascii format (#188)

This commit is contained in:
Neeraj Gupta 2023-08-10 12:33:13 +05:30 committed by GitHub
commit 683d44a83c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 11 deletions

View file

@ -23,12 +23,12 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
void initState() {
_issuerController = TextEditingController(
text: widget.code != null
? Uri.decodeFull(widget.code!.issuer).trim()
? safeDecode(widget.code!.issuer).trim()
: null,
);
_accountController = TextEditingController(
text: widget.code != null
? Uri.decodeFull(widget.code!.account).trim()
? safeDecode(widget.code!.account).trim()
: null,
);
_secretController = TextEditingController(

View file

@ -306,15 +306,6 @@ class _CodeWidgetState extends State<CodeWidget> {
);
}
String safeDecode(String value) {
try {
return Uri.decodeComponent(value);
} catch (e) {
// note: don't log the value, it might contain sensitive information
logger.severe("Failed to decode", e);
return value;
}
}
String _getCurrentOTP() {
try {

View file

@ -1,4 +1,5 @@
import 'package:ente_auth/models/code.dart';
import 'package:flutter/foundation.dart';
import 'package:otp/otp.dart' as otp;
String getOTP(Code code) {
@ -50,3 +51,13 @@ otp.Algorithm _getAlgorithm(Code code) {
String getSanitizedSecret(String secret) {
return secret.toUpperCase().trim().replaceAll(' ', '');
}
String safeDecode(String value) {
try {
return Uri.decodeComponent(value);
} catch (e) {
// note: don't log the value, it might contain sensitive information
debugPrint("Failed to decode $e");
return value;
}
}

View file

@ -39,4 +39,16 @@ void main() {
expect(code.account, "Acc !@#444", reason: "accountMismatch");
expect(code.secret, "NI4CTTFEV4G2JFE6");
});
test("parseAndUpdateInChinese", () {
const String rubberDuckQr =
'otpauth://totp/%E6%A9%A1%E7%9A%AE%E9%B8%AD?secret=2CWDCK4EOIN5DJDRMYUMYBBO4MKSR5AX&issuer=ente.io';
final code = Code.fromRawData(rubberDuckQr);
expect(code.account, '橡皮鸭');
final String updatedRawCode =
code.copyWith(account: '伍迪', issuer: '鸭子').rawData;
final updateCode = Code.fromRawData(updatedRawCode);
expect(updateCode.account, '伍迪', reason: 'updated accountMismatch');
expect(updateCode.issuer, '鸭子', reason: 'updated issuerMismatch');
});
}