1
0
mirror of https://github.com/cmur2/dyndnsd.git synced 2024-12-21 14:54:22 +01:00

Check for hostname validity

This commit is contained in:
cn 2013-04-27 14:59:25 +02:00
parent d46f226e32
commit bbf0c42141
2 changed files with 33 additions and 3 deletions

View File

@ -31,6 +31,7 @@ module Dyndnsd
class Daemon class Daemon
def initialize(config, db, updater, responder) def initialize(config, db, updater, responder)
@users = config['users'] @users = config['users']
@domain = config['domain']
@db = db @db = db
@updater = updater @updater = updater
@responder = responder @responder = responder
@ -45,6 +46,14 @@ module Dyndnsd
@updater.update(@db) @updater.update(@db)
end end
def is_fqdn_valid?(hostname)
return false if hostname.length < @domain.length + 2
return false if not hostname.end_with?(@domain)
name = hostname.chomp(@domain)
return false if not name.match(/^[a-zA-Z0-9_-]+\.$/)
true
end
def call(env) def call(env)
return @responder.response_for(:method_forbidden) if env["REQUEST_METHOD"] != "GET" return @responder.response_for(:method_forbidden) if env["REQUEST_METHOD"] != "GET"
return @responder.response_for(:not_found) if env["PATH_INFO"] != "/nic/update" return @responder.response_for(:not_found) if env["PATH_INFO"] != "/nic/update"
@ -55,8 +64,8 @@ module Dyndnsd
hostname = params["hostname"] hostname = params["hostname"]
# Check if hostname(s) match rules # Check if hostname match rules
#return @responder.response_for(:hostname_malformed) if XY return @responder.response_for(:hostname_malformed) if not is_fqdn_valid?(hostname)
user = env["REMOTE_USER"] user = env["REMOTE_USER"]

View File

@ -5,6 +5,7 @@ describe Dyndnsd::Daemon do
def app def app
config = { config = {
'domain' => 'example.org',
'users' => { 'users' => {
'test' => { 'test' => {
'password' => 'secret', 'password' => 'secret',
@ -79,7 +80,27 @@ describe Dyndnsd::Daemon do
end end
it 'forbids invalid hostnames' do it 'forbids invalid hostnames' do
pending authorize 'test', 'secret'
get '/nic/update?hostname=test'
last_response.should be_ok
last_response.body.should == 'notfqdn'
get '/nic/update?hostname=test.example.com'
last_response.should be_ok
last_response.body.should == 'notfqdn'
get '/nic/update?hostname=test.example.org.me'
last_response.should be_ok
last_response.body.should == 'notfqdn'
get '/nic/update?hostname=foo.test.example.org'
last_response.should be_ok
last_response.body.should == 'notfqdn'
get '/nic/update?hostname=in%20valid.example.org.me'
last_response.should be_ok
last_response.body.should == 'notfqdn'
end end
it 'outputs status per hostname' do it 'outputs status per hostname' do