-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinput_validator.dart
More file actions
71 lines (59 loc) · 1.95 KB
/
input_validator.dart
File metadata and controls
71 lines (59 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:threebotlogin/helpers/logger.dart';
class InputValidator {
static const int maxUrlLength = 2048;
static const int maxContentLength = 100000;
static const int maxScopeLength = 10000;
static const Duration httpTimeout = Duration(seconds: 30);
static Uri? validateUrl(String url, {int? maxLength}) {
try {
final trimmedUrl = url.trim();
if (trimmedUrl.length > (maxLength ?? maxUrlLength)) return null;
final uri = Uri.parse(trimmedUrl);
if (!uri.isScheme('http') && !uri.isScheme('https')) return null;
if (!uri.hasAuthority) return null;
return uri;
} catch (e) {
logger.e('Invalid URL: $e');
return null;
}
}
static Map<String, dynamic>? decodeJson(String jsonString, {int? maxLength}) {
try {
if (jsonString.length > (maxLength ?? maxContentLength)) return null;
final decoded = json.decode(jsonString);
return decoded is Map<String, dynamic> ? decoded : null;
} catch (e) {
logger.e('Invalid JSON: $e');
return null;
}
}
static bool isValidLength(String? value, int maxLength) {
return value != null && value.length <= maxLength;
}
static Future<String?> fetchValidatedContent(Uri uri,
{int? maxLength}) async {
try {
final response = await http.get(uri).timeout(httpTimeout);
if (response.statusCode != 200) {
logger.e('Failed to fetch: HTTP ${response.statusCode}');
return null;
}
if (response.body.isEmpty) {
logger.e('Empty response body');
return null;
}
final maxLen = maxLength ?? maxContentLength;
if (response.body.length > maxLen) {
logger.e(
'Response too large: ${response.body.length} bytes (max: $maxLen)');
return null;
}
return response.body;
} catch (e) {
logger.e('Error fetching content: $e');
return null;
}
}
}