Skip to content

Commit 90c517f

Browse files
fix: better input sanitization [MTT-5894] (#821)
* using NetworkEndPoint.TryParse to validate IP address and port
1 parent fd5b1dd commit 90c517f

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

Assets/Prefabs/UI/IPPopup.prefab

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ MonoBehaviour:
173173
m_faceColor:
174174
serializedVersion: 2
175175
rgba: 4294967295
176-
m_fontSize: 20.3
176+
m_fontSize: 33
177177
m_fontSizeBase: 36
178178
m_fontWeight: 400
179179
m_enableAutoSizing: 1
@@ -785,7 +785,7 @@ MonoBehaviour:
785785
m_faceColor:
786786
serializedVersion: 2
787787
rgba: 4294967295
788-
m_fontSize: 32.2
788+
m_fontSize: 50
789789
m_fontSizeBase: 36
790790
m_fontWeight: 400
791791
m_enableAutoSizing: 1
@@ -935,6 +935,7 @@ MonoBehaviour:
935935
m_IPInputField: {fileID: 783666621484907260}
936936
m_PortInputField: {fileID: 3692047279709044436}
937937
m_CanvasGroup: {fileID: 3432270648822068983}
938+
m_HostButton: {fileID: 8503688101831781139}
938939
--- !u!1 &2513356161705610835
939940
GameObject:
940941
m_ObjectHideFlags: 0
@@ -2211,7 +2212,7 @@ MonoBehaviour:
22112212
m_faceColor:
22122213
serializedVersion: 2
22132214
rgba: 4294967295
2214-
m_fontSize: 32.2
2215+
m_fontSize: 50
22152216
m_fontSizeBase: 36
22162217
m_fontWeight: 400
22172218
m_enableAutoSizing: 1
@@ -3287,6 +3288,7 @@ MonoBehaviour:
32873288
m_CanvasGroup: {fileID: 6846323567751854231}
32883289
m_IPInputField: {fileID: 2677382141616317261}
32893290
m_PortInputField: {fileID: 7282211495594724544}
3291+
m_JoinButton: {fileID: 8754602378570439514}
32903292
--- !u!1 &5924530127146065184
32913293
GameObject:
32923294
m_ObjectHideFlags: 0
@@ -3577,7 +3579,7 @@ MonoBehaviour:
35773579
m_faceColor:
35783580
serializedVersion: 2
35793581
rgba: 4294967295
3580-
m_fontSize: 20.3
3582+
m_fontSize: 33
35813583
m_fontSizeBase: 36
35823584
m_fontWeight: 400
35833585
m_enableAutoSizing: 1

Assets/Scripts/Gameplay/UI/IPHostingUI.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class IPHostingUI : MonoBehaviour
1313
[SerializeField]
1414
CanvasGroup m_CanvasGroup;
1515

16+
[SerializeField]
17+
Button m_HostButton;
18+
1619
[Inject] IPUIMediator m_IPUIMediator;
1720

1821
void Awake()
@@ -43,16 +46,17 @@ public void OnCreateClick()
4346
/// </summary>
4447
public void SanitizeIPInputText()
4548
{
46-
m_IPInputField.text = IPUIMediator.Sanitize(m_IPInputField.text);
49+
m_IPInputField.text = IPUIMediator.SanitizeIP(m_IPInputField.text);
50+
m_HostButton.interactable = IPUIMediator.AreIpAddressAndPortValid(m_IPInputField.text, m_PortInputField.text);
4751
}
4852

4953
/// <summary>
5054
/// Added to the InputField component's OnValueChanged callback for the Port UI text.
5155
/// </summary>
5256
public void SanitizePortText()
5357
{
54-
var inputFieldText = IPUIMediator.Sanitize(m_PortInputField.text);
55-
m_PortInputField.text = inputFieldText;
58+
m_PortInputField.text = IPUIMediator.SanitizePort(m_PortInputField.text);
59+
m_HostButton.interactable = IPUIMediator.AreIpAddressAndPortValid(m_IPInputField.text, m_PortInputField.text);
5660
}
5761
}
5862
}

Assets/Scripts/Gameplay/UI/IPJoiningUI.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class IPJoiningUI : MonoBehaviour
1414

1515
[SerializeField] InputField m_PortInputField;
1616

17+
[SerializeField]
18+
Button m_JoinButton;
19+
1720
[Inject] IPUIMediator m_IPUIMediator;
1821

1922
void Awake()
@@ -44,16 +47,17 @@ public void OnJoinButtonPressed()
4447
/// </summary>
4548
public void SanitizeIPInputText()
4649
{
47-
m_IPInputField.text = IPUIMediator.Sanitize(m_IPInputField.text);
50+
m_IPInputField.text = IPUIMediator.SanitizeIP(m_IPInputField.text);
51+
m_JoinButton.interactable = IPUIMediator.AreIpAddressAndPortValid(m_IPInputField.text, m_PortInputField.text);
4852
}
4953

5054
/// <summary>
5155
/// Added to the InputField component's OnValueChanged callback for the Port UI text.
5256
/// </summary>
5357
public void SanitizePortText()
5458
{
55-
var inputFieldText = IPUIMediator.Sanitize(m_PortInputField.text);
56-
m_PortInputField.text = inputFieldText;
59+
m_PortInputField.text = IPUIMediator.SanitizePort(m_PortInputField.text);
60+
m_JoinButton.interactable = IPUIMediator.AreIpAddressAndPortValid(m_IPInputField.text, m_PortInputField.text);
5761
}
5862
}
5963
}

Assets/Scripts/Gameplay/UI/IPUIMediator.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using TMPro;
55
using Unity.BossRoom.ConnectionManagement;
66
using Unity.BossRoom.Infrastructure;
7+
using Unity.Networking.Transport;
78
using UnityEngine;
89
using VContainer;
910

@@ -171,13 +172,32 @@ public void CancelConnectingWindow()
171172
}
172173

173174
/// <summary>
174-
/// Sanitize user port InputField box allowing only alphanumerics and '.'
175+
/// Sanitize user IP address InputField box allowing only numbers and '.'. This also prevents undesirable
176+
/// invisible characters from being copy-pasted accidentally.
175177
/// </summary>
176178
/// <param name="dirtyString"> string to sanitize. </param>
177179
/// <returns> Sanitized text string. </returns>
178-
public static string Sanitize(string dirtyString)
180+
public static string SanitizeIP(string dirtyString)
179181
{
180-
return Regex.Replace(dirtyString, "[^A-Za-z0-9.]", "");
182+
return Regex.Replace(dirtyString, "[^0-9.]", "");
183+
}
184+
185+
/// <summary>
186+
/// Sanitize user port InputField box allowing only numbers. This also prevents undesirable invisible characters
187+
/// from being copy-pasted accidentally.
188+
/// </summary>
189+
/// <param name="dirtyString"> string to sanitize. </param>
190+
/// <returns> Sanitized text string. </returns>
191+
public static string SanitizePort(string dirtyString)
192+
{
193+
194+
return Regex.Replace(dirtyString, "[^0-9]", "");
195+
}
196+
197+
public static bool AreIpAddressAndPortValid(string ipAddress, string port)
198+
{
199+
var portValid = ushort.TryParse(port, out var portNum);
200+
return portValid && NetworkEndPoint.TryParse(ipAddress, portNum, out var networkEndPoint);
181201
}
182202
}
183203
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2323
* Elements inside the Tank's and Rogue's AnimatorTriggeredSpecialFX list have been revised to not loop AudioSource clips, ending the logging of multiple warnings to the console (#785)
2424
* ClientConnectedState now inherits from OnlineState instead of the base ConnectionState (#801)
2525
* UpdateRunner now sends the right value for deltaTime when updating its subscribers (#805)
26+
* Inputs are better sanitized when entering IP address and port (#821). Now all invalid characters are prevented, and UnityTransport's NetworkEndpoint.TryParse is used to verify the validity of the IP address and port that are entered before making the join/host button interactable.
2627

2728
## [2.0.4] - 2022-12-13
2829
### Changed

0 commit comments

Comments
 (0)