Further record validation cleanup, support for CNAME records

This commit is contained in:
Al Beano 2017-08-20 00:27:47 +01:00
parent d2499d551f
commit 57a0c8b1e9
2 changed files with 18 additions and 26 deletions

View File

@ -51,29 +51,17 @@ get '/domains/:name/records/add' => sub {
}; };
}; };
my %patterns = (
"ipv4" => qr/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}$/,
"ipv6" => qr/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/,
"name" => qr/^([a-zA-Z0-9]([a-zA-Z0-9-_]*[a-zA-Z0-9])?\.)+$/,
);
my %tests = ( IN => { my %tests = ( IN => {
# Tests return 1 on invalid value, 0 on valid "A" => "ipv4",
A => sub { "AAAA" => "ipv6",
my $val_l = shift; "NS" => "name",
if ($val_l !~ m/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}$/) { "CNAME" => "name",
return 1;
}
return 0;
},
AAAA => sub {
my $val_l = shift;
if ($val_l !~ m/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/) {
return 1;
}
return 0;
},
NS => sub {
my $val_l = shift;
if ($val_l !~ m/^([a-zA-Z0-9]([a-zA-Z0-9-_]*[a-zA-Z0-9])?\.)+$/) {
return 1;
}
return 0;
},
}); });
post '/domains/:name/records/add' => sub { post '/domains/:name/records/add' => sub {
@ -92,8 +80,10 @@ post '/domains/:name/records/add' => sub {
my %errs; my %errs;
if ( ref($tests{param("type")}) == "CODE" ) { if ( exists($tests{"IN"}->{param("type")}) ) {
$errs{"e_bad_value"} = 1 if &{$tests{IN}{param("type")}}(param("value")); if (param("value") !~ m/$patterns{ $tests{"IN"}->{param("type")} }/) {
$errs{"e_bad_value"} = 1;
}
} else { } else {
$errs{"e_bad_type"} = 1; $errs{"e_bad_type"} = 1;
} }

View File

@ -12,7 +12,7 @@
<ul> <ul>
<% IF e_bad_value %> <% IF e_bad_value %>
<li>The value you entered was invalid. <li>The value you entered was invalid.
<% IF params.type == 'NS' %>(Did you forget the trailing '.'?)<% END %></li> <% IF params.type == 'NS' || params.type == 'CNAME' %>(Did you forget the trailing '.'?)<% END %></li>
<% END %> <% END %>
<% IF e_bad_type %> <% IF e_bad_type %>
<li>You must choose a type for the record.</li> <li>You must choose a type for the record.</li>
@ -39,7 +39,7 @@
</td> </td>
<td> <td>
<input type="text" style="width: 100px" name="rname" id="rname" placeholder="Use @ for root" <input type="text" style="width: 100px" name="rname" id="rname" placeholder="Use @ for root"
value="<% params.rname | html_entity %>" />.<% domain.name | html_entity %>.<% vars.config.tld %> value="<% params.rname | html_entity %>" />.<% domain.name | html_entity %>.<% vars.config.tld %>
</td> </td>
</tr> </tr>
<tr> <tr>
@ -50,9 +50,11 @@
</td> </td>
<td> <td>
<select name="type" id="type"> <select name="type" id="type">
<%# TODO: machine generate this %>
<option value="">- Select -</option> <option value="">- Select -</option>
<option value="A">A</option> <option value="A">A</option>
<option value="AAAA">AAAA</option> <option value="AAAA">AAAA</option>
<option value="CNAME">CNAME</option>
<option value="NS">NS</option> <option value="NS">NS</option>
</select> </select>
</td> </td>