Skip to content

Commit adbae12

Browse files
authored
Merge pull request #847 from surelyruchi/resolve-url-parsing-ip-port
Fix: prevent malformed URL error when parsing IP:PORT by adding defau…
2 parents 2ebd0ee + fb1def1 commit adbae12

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

packages/apidash_core/lib/consts.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ final kSupportedUriSchemes =
3030
SupportedUriSchemes.values.map((i) => i.name).toList();
3131
const kDefaultUriScheme = SupportedUriSchemes.https;
3232
final kLocalhostRegex = RegExp(r'^localhost(:\d+)?(/.*)?$');
33+
final kIPHostRegex =
34+
RegExp(r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}(:\d+)?(/.*)?$');
3335

3436
const kMethodsWithBody = [
3537
HTTPVerb.post,

packages/apidash_core/lib/utils/uri_utils.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ String stripUrlParams(String url) {
3030
return (null, "URL is missing!");
3131
}
3232

33-
if (kLocalhostRegex.hasMatch(url)) {
33+
if (kLocalhostRegex.hasMatch(url) || kIPHostRegex.hasMatch(url)) {
3434
url = '${SupportedUriSchemes.http.name}://$url';
3535
}
36+
3637
Uri? uri = Uri.tryParse(url);
3738
if (uri == null) {
3839
return (null, "Check URL (malformed)");

packages/apidash_core/test/utils/uri_utils_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,37 @@ void main() {
6262
expect(getValidRequestUri(url1, []), (uri1Expected, null));
6363
});
6464

65+
test('Testing getValidRequestUri with IP URL without port or path', () {
66+
String url1 = "8.8.8.8";
67+
Uri uri1Expected = Uri(scheme: 'http', host: '8.8.8.8');
68+
expect(getValidRequestUri(url1, []), (uri1Expected, null));
69+
});
70+
71+
test('Testing getValidRequestUri with IP URL with port', () {
72+
String url1 = "8.8.8.8:8080";
73+
Uri uri1Expected = Uri(scheme: 'http', host: '8.8.8.8', port: 8080);
74+
expect(getValidRequestUri(url1, []), (uri1Expected, null));
75+
});
76+
77+
test('Testing getValidRequestUri with IP URL with port and path', () {
78+
String url1 = "8.8.8.8:8080/hello";
79+
Uri uri1Expected =
80+
Uri(scheme: 'http', host: '8.8.8.8', port: 8080, path: '/hello');
81+
expect(getValidRequestUri(url1, []), (uri1Expected, null));
82+
});
83+
84+
test('Testing getValidRequestUri with IP URL with http prefix', () {
85+
String url1 = "http://8.8.8.8:3080";
86+
Uri uri1Expected = Uri(scheme: 'http', host: '8.8.8.8', port: 3080);
87+
expect(getValidRequestUri(url1, []), (uri1Expected, null));
88+
});
89+
90+
test('Testing getValidRequestUri with IP URL with https prefix', () {
91+
String url1 = "https://8.8.8.8:8080";
92+
Uri uri1Expected = Uri(scheme: 'https', host: '8.8.8.8', port: 8080);
93+
expect(getValidRequestUri(url1, []), (uri1Expected, null));
94+
});
95+
6596
test('Testing getValidRequestUri for normal values', () {
6697
String url1 = "https://api.apidash.dev/country/data";
6798
const kvRow1 = NameValueModel(name: "code", value: "US");

0 commit comments

Comments
 (0)