Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check ip format #2262

Merged
merged 1 commit into from
Jan 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/neo/SmartContract/Native/NameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public sealed class NameService : NonfungibleToken<NameService.NameState>
private const uint OneYear = 365 * 24 * 3600;
private static readonly Regex rootRegex = new Regex("^[a-z][a-z0-9]{0,15}$", RegexOptions.Singleline);
private static readonly Regex nameRegex = new Regex("^(?=.{3,255}$)([a-z0-9]{1,62}\\.)+[a-z][a-z0-9]{0,15}$", RegexOptions.Singleline);
private static readonly Regex ipv4Regex = new Regex("^(2(5[0-5]|[0-4]\\d))|1?\\d{1,2}(\\.((2(5[0-5]|[0-4]\\d))|1?\\d{1,2})){3}$", RegexOptions.Singleline);
private static readonly Regex ipv6Regex = new Regex("^([a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$", RegexOptions.Singleline | RegexOptions.IgnoreCase);

internal NameService()
{
Expand Down Expand Up @@ -169,6 +171,7 @@ private void SetRecord(ApplicationEngine engine, string name, RecordType type, s
switch (type)
{
case RecordType.A:
if (!ipv4Regex.IsMatch(data)) throw new FormatException();
if (!IPAddress.TryParse(data, out IPAddress address)) throw new FormatException();
shargon marked this conversation as resolved.
Show resolved Hide resolved
if (address.AddressFamily != AddressFamily.InterNetwork) throw new FormatException();
break;
Expand All @@ -179,6 +182,7 @@ private void SetRecord(ApplicationEngine engine, string name, RecordType type, s
if (Utility.StrictUTF8.GetByteCount(data) > 255) throw new FormatException();
break;
case RecordType.AAAA:
if (!ipv6Regex.IsMatch(data)) throw new FormatException();
if (!IPAddress.TryParse(data, out address)) throw new FormatException();
if (address.AddressFamily != AddressFamily.InterNetworkV6) throw new FormatException();
break;
Expand Down