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

Add validation for records create/edit forms #416

Merged
merged 3 commits into from
Mar 24, 2023

Conversation

Myrfion
Copy link
Contributor

@Myrfion Myrfion commented Mar 22, 2023

Changes

  • add more in-depth validation for forms, which includes validation of record value
  • extraction of validation schemas and reusing them
  • showing errors for all fields in record form

closes #277

@Genne23v
Copy link
Contributor

I think the issue number is not right @Myrfion

app/lib/dns.server.ts Show resolved Hide resolved
app/routes/__index/dns-records/$dnsRecordId.tsx Outdated Show resolved Hide resolved
SerpentBytes
SerpentBytes previously approved these changes Mar 23, 2023
Copy link
Contributor

@humphd humphd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really slick, nice work!

Testing locally, here is what I notice:

An invalid name and an invalid A record (e.g., not an IP) are caught correctly:

Screenshot 2023-03-23 at 1 49 49 PM

However, a CNAME shouldn't be an IP address (it has to be a domain name):

Screenshot 2023-03-23 at 1 51 52 PM

I'm not sure where this bug is coming from, might be in the DNS verification code?

app/lib/dns.server.ts Show resolved Hide resolved
@Myrfion
Copy link
Contributor Author

Myrfion commented Mar 23, 2023

@humphd because I wanted to reuse the same scheme for both record creation and editing, by just adding an extra field to edit scheme (id). But if I want to validate a subdomain using existing validation functions I also need to pass the username in the validation function and I have access to the user object only in the action function.

@Myrfion
Copy link
Contributor Author

Myrfion commented Mar 23, 2023

@humphd I think the problem of validation comes from the validation function which was created before and it looks like this:

export const isValueValid = (type: DnsRecordType, value: string) => {
  if (type === 'A') {
    return isIP(value, 4);
  }

  if (type === 'AAAA') {
    return isIP(value, 6);
  }

  // CNAME can be any non-empty string. Let AWS validate it.
  return value.length >= 1;
};

I believe this is not the way the value really should be validated

@Myrfion
Copy link
Contributor Author

Myrfion commented Mar 23, 2023

The ChatGPT is actually pretty good to write this kinda things that don't require a lot of context, here is what it did, looks good to me but need to write some tests too:

const isValueValid = (type: DnsRecordType, value: string) => {
  switch (type) {
    case 'A':
      // IPv4 address (e.g. "192.168.0.1")
      return /^(\d{1,3}\.){3}\d{1,3}$/.test(value);
    case 'AAAA':
      // IPv6 address (e.g. "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
      return /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/.test(value);
    case 'CNAME':
      // domain name (e.g. "www.example.com")
      return /^([a-zA-Z0-9_-]+\.)*[a-zA-Z0-9_-]+\.[a-zA-Z]{2,}$/.test(value);
    case 'TXT':
      // free-form text data (e.g. "v=spf1 a mx ~all")
      return /^"[^"]*"$/.test(value);
  }
};

@humphd
Copy link
Contributor

humphd commented Mar 23, 2023

Let's try to fix our validation and do it all in one place vs. duplicating it. I'm hesitant to write our own regex for these, as there are so many corner cases with domains. Those functions should work, and if they don't, we should replace them with ones that do.

cc @Genne23v

@Myrfion
Copy link
Contributor Author

Myrfion commented Mar 23, 2023

Let's try to fix our validation and do it all in one place vs. duplicating it. I'm hesitant to write our own regex for these, as there are so many corner cases with domains. Those functions should work, and if they don't, we should replace them with ones that do.

cc @Genne23v

I wasn't actually thinking about duplicating it, but just replacing the existing one. If you wanna fix the current function I think we need at least to validate the CNAME value it in it somehow, because right now it is not being validated in it

humphd
humphd previously approved these changes Mar 23, 2023
Copy link
Contributor

@humphd humphd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's take this now, and please file a follow-up to fix our validation (@Genne23v) and add E2E tests (@Eakam1007) for all these validation cases.

@Genne23v Genne23v added this to the Milestone 0.7 milestone Mar 23, 2023
Copy link
Contributor

@humphd humphd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please file an issue to fix our validation on CNAME, and add e2e tests, and this is good.

@Myrfion Myrfion merged commit e602dee into main Mar 24, 2023
@Myrfion Myrfion deleted the feature/227/dns-record-forms-validation branch March 24, 2023 15:57
@humphd
Copy link
Contributor

humphd commented Mar 24, 2023

Please file an issue to fix our validation on CNAME, and add e2e tests, and this is good.

@Myrfion can you do this too please?

@Myrfion
Copy link
Contributor Author

Myrfion commented Mar 24, 2023

Please file an issue to fix our validation on CNAME, and add e2e tests, and this is good.

@Myrfion can you do this too please?

Done #435 #436

@humphd
Copy link
Contributor

humphd commented Mar 24, 2023

Awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add validation for new DNS Record form
5 participants