From cdb2c5e46b12c4bd0bf9d9800d8967c77f42ec30 Mon Sep 17 00:00:00 2001 From: Marko Oldenburg Date: Wed, 4 Jan 2017 13:44:06 +0100 Subject: [PATCH] added JSON String check, set and get cmd specific by bridge type --- 73_NUKIBridge.pm | 19 ++++++++++++++++--- 74_NUKIDevice.pm | 27 ++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/73_NUKIBridge.pm b/73_NUKIBridge.pm index 566e8ca..4a100f5 100644 --- a/73_NUKIBridge.pm +++ b/73_NUKIBridge.pm @@ -46,7 +46,7 @@ use JSON; use HttpUtils; -my $version = "0.4.0"; +my $version = "0.4.3"; @@ -234,7 +234,9 @@ sub NUKIBridge_Set($@) { } } else { - my $list = "info:noArg autocreate:noArg clearLog:noArg fwUpdate:noArg reboot:noArg callbackRemove:0,1,2"; + my $list = ""; + $list .= "info:noArg autocreate:noArg "; + $list .= "clearLog:noArg fwUpdate:noArg reboot:noArg callbackRemove:0,1,2" if( ReadingsVal($name,'bridgeType','Software') eq 'Hardware' ); return "Unknown argument $cmd, choose one of $list"; } @@ -256,7 +258,7 @@ sub NUKIBridge_Get($@) { NUKIBridge_getCallbackList($hash) if( !IsDisabled($name) ); } else { - my $list = "logFile:noArg callbackList:noArg"; + my $list = "logFile:noArg callbackList:noArg" if( ReadingsVal($name,'bridgeType','Software') eq 'Hardware' ); return "Unknown argument $cmd, choose one of $list"; } @@ -424,6 +426,17 @@ sub NUKIBridge_ResponseProcessing($$$) { my $decode_json; + if( !$json ) { + Log3 $name, 3, "NUKIBridge ($name) - empty answer received"; + return undef; + } elsif( $json =~ m'HTTP/1.1 200 OK' ) { + Log3 $name, 4, "NUKIBridge ($name) - empty answer received"; + return undef; + } elsif( $json !~ m/^[\[{].*[}\]]$/ ) { + Log3 $name, 3, "NUKIBridge ($name) - invalid json detected: $json"; + return "NUKIBridge ($name) - invalid json detected: $json"; + } + $decode_json = decode_json($json); if( ref($decode_json) eq "ARRAY" and scalar(@{$decode_json}) > 0 and $path eq "list" ) { diff --git a/74_NUKIDevice.pm b/74_NUKIDevice.pm index a9701ce..7246876 100644 --- a/74_NUKIDevice.pm +++ b/74_NUKIDevice.pm @@ -33,7 +33,7 @@ use warnings; use JSON; #use Time::HiRes qw(gettimeofday); -my $version = "0.4.1"; +my $version = "0.4.3"; @@ -192,6 +192,8 @@ sub NUKIDevice_Attr(@) { ###################### #### webhook ######### + return "$attrName can only use with hardware bridge" if( ($attrName eq "webhookHttpHostname" or $attrName eq "webhookFWinstance") and ReadingsVal($hash->{IODev}->{NAME},'bridgeType','Software') eq 'Software' ); + return "Invalid value for attribute $attrName: can only by FQDN or IPv4 or IPv6 address" if ( $attrVal && $attrName eq "webhookHttpHostname" && $attrVal !~ /^([A-Za-z_.0-9]+\.[A-Za-z_.0-9]+)|[0-9:]+$/ ); return "Invalid value for attribute $attrName: needs to be different from the defined name/address of your Smartlock, we need to know how Smartlock can connect back to FHEM here!" if ( $attrVal && $attrName eq "webhookHttpHostname" && $attrVal eq $hash->{DeviceName} ); @@ -358,6 +360,17 @@ sub NUKIDevice_Parse($$) { ######################################### ####### Errorhandling ############# + if( !$result ) { + Log3 $name, 3, "NUKIDevice ($name) - empty answer received"; + return undef; + } elsif( $result =~ m'HTTP/1.1 200 OK' ) { + Log3 $name, 4, "NUKIDevice ($name) - empty answer received"; + return undef; + } elsif( $result !~ m/^[\[{].*[}\]]$/ ) { + Log3 $name, 3, "NUKIDevice ($name) - invalid json detected: $result"; + return "NUKIDevice ($name) - invalid json detected: $result"; + } + if( $result =~ /\d{3}/ ) { if( $result eq 400 ) { readingsSingleUpdate( $hash, "state", "action is undefined", 1 ); @@ -460,6 +473,18 @@ sub NUKIDevice_CGI() { my $header = join("\n", @FW_httpheader); my ($first,$json) = split("&",$request,2); + + if( !$json ) { + Log3 $name, 3, "NUKIDevice ($name) - empty answer received"; + return undef; + } elsif( $json =~ m'HTTP/1.1 200 OK' ) { + Log3 $name, 4, "NUKIDevice ($name) - empty answer received"; + return undef; + } elsif( $json !~ m/^[\[{].*[}\]]$/ ) { + Log3 $name, 3, "NUKIDevice ($name) - invalid json detected: $json"; + return "NUKIDevice ($name) - invalid json detected: $json"; + } + my $decode_json = decode_json($json);