Skip to content

Commit d3b47fb

Browse files
Fix: profile links regex (#6137)
1 parent cae33b0 commit d3b47fb

File tree

9 files changed

+66
-4
lines changed

9 files changed

+66
-4
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace DCL.MyAccount
4+
{
5+
/// <summary>
6+
/// A helper class to validate urls. These must start either with http or https.
7+
/// </summary>
8+
public static class LinkValidator
9+
{
10+
private static readonly Regex httpRegex = new (@"^(?:https?):\/\/[^\s\/$.?#].[^\s]*$");
11+
12+
/// <summary>
13+
/// Validates a given url checking if it starts with http or https.
14+
/// </summary>
15+
/// <param name="url">The url to validate.</param>
16+
/// <returns>Whether the url is valid or not.</returns>
17+
public static bool IsValid(string url) => httpRegex.IsMatch(url);
18+
}
19+
}

unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/MyAccountHUD/LinkValidator.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/MyAccountHUD/MyProfileLinkListComponentView.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Text.RegularExpressions;
43
using TMPro;
54
using UnityEngine;
65
using UnityEngine.Networking;
@@ -17,7 +16,6 @@ public class MyProfileLinkListComponentView : MonoBehaviour
1716
[SerializeField] private RectTransform linksContainer;
1817

1918
private readonly List<MyProfileLinkComponentView> links = new ();
20-
private readonly Regex httpRegex = new ("^((https?:)?\\/\\/)?([\\da-z.-]+)\\.([a-z.]{2,6})([\\/\\w .%@()\\-]*)*\\/?$");
2119

2220
private bool isAddEnabled = true;
2321

@@ -77,7 +75,7 @@ public void EnableOrDisableAddNewLinkOption(bool enabled)
7775
private void EnableOrDisableAddButton()
7876
{
7977
addButton.interactable = newLinkTitle.text.Length > 0 && newLinkUrl.text.Length > 0
80-
&& httpRegex.IsMatch(newLinkUrl.text)
78+
&& LinkValidator.IsValid(newLinkUrl.text)
8179
&& isAddEnabled;
8280
}
8381
}

unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/MyAccountHUD/Tests/EditModeTests.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using NUnit.Framework;
2+
using UnityEngine;
3+
using UnityEngine.TestTools;
4+
5+
namespace DCL.MyAccount
6+
{
7+
public class LinkValidatorShould
8+
{
9+
[UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
10+
[TestCase("https://alink.com?aVar=aValue&anotherVar=anotherValue", true)]
11+
[TestCase("https://alink.com?someVar=some%20value", true)]
12+
[TestCase("https://alink.com", true)]
13+
[TestCase("http://alink.com'", true)]
14+
[TestCase("javascript:window%5b%22ale%22%2b%22rt%22%5d(document.domain)//.com", false)]
15+
public void IsValid(string url, bool expectedResult)
16+
{
17+
Assert.True(LinkValidator.IsValid(url) == expectedResult);
18+
}
19+
}
20+
}

unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/MyAccountHUD/Tests/EditModeTests/LinkValidatorShould.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"reference": "GUID:c4ac1fe5aa954bb4facfed6d10c73d3d"
3+
}

unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/MyAccountHUD/Tests/EditModeTests/LinkValidatorTests.asmref.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unity-renderer/Assets/Scripts/MainScripts/TestsAsmdef/EditModeTests/DCL.EditModeTests.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"GUID:6426fc92c49cbb342916620a1009be8d",
5656
"GUID:fd6a19eb8dfe417a84a27e537222d98d",
5757
"GUID:c34e38f41494f834abff029ddf82af43",
58-
"GUID:43315b150e1df4592868fc0fe58b3429"
58+
"GUID:43315b150e1df4592868fc0fe58b3429",
59+
"GUID:0f15c7a7fa0e4b94aaceddb51ad829ac"
5960
],
6061
"includePlatforms": [
6162
"Editor"

0 commit comments

Comments
 (0)