2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-09 20:57:11 +00:00

32_withings: safer json decoding

git-svn-id: https://svn.fhem.de/fhem/trunk@14248 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
moises 2017-05-11 15:24:46 +00:00
parent c859a43b46
commit 97232b5448
2 changed files with 63 additions and 21 deletions

View File

@ -1,5 +1,6 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it.
- bugfix: 32_withings: safer json decoding
- feature: 38_netatmo: home notification settings
- feature: 98_alarmclock: New feature RepRoutine
- feature: 31_PLAYBULB: support for new Garden Model, move battery Reading to

View File

@ -362,7 +362,7 @@ sub withings_Define($$) {
my $username = withings_encrypt($user);
my $password = withings_encrypt($pass);
Log3 $name, 3, "$name: encrypt $user/$pass to $username/$password";
Log3 $name, 3, "$name: encrypt $user/$pass to $username/$password" if($user ne $username || $pass ne $password);
#$hash->{DEF} =~ s/$user/$username/g;
#$hash->{DEF} =~ s/$pass/$password/g;
@ -500,8 +500,13 @@ sub withings_getToken($) {
#my $response = $agent->request($request);
return undef if(!defined($data));
my $json = ();
$json = JSON->new->utf8(0)->decode($data) if( $data =~ m/^{.*}$/ );
my $json = eval { JSON->new->utf8(0)->decode($data) };
if($@)
{
Log3 "withings", 2, "withings: json evaluation error on getToken ".$@;
return undef;
}
my $once = $json->{body}{once};
$hash->{Once} = $once;
my $hashstring = withings_decrypt($hash->{helper}{username}).':'.md5_hex(withings_decrypt($hash->{helper}{password})).':'.$once;
@ -625,8 +630,12 @@ sub withings_getSessionKey($) {
if( $data =~ m/^{.*}$/ )
{
my $json = ();
$json = JSON->new->utf8(0)->decode($data);
my $json = eval { JSON->new->utf8(0)->decode($data) };
if($@)
{
Log3 $name, 2, "$name: json evaluation error on getSessionKey ".$@;
return undef;
}
foreach my $account (@{$json->{body}{account}}) {
next if( !defined($account->{id}) );
@ -929,8 +938,12 @@ sub withings_getUsers($) {
#my $response = $ua->request($request);
return undef if(!defined($data));
my $json = ();
$json = JSON->new->utf8(0)->decode($data) if( $data =~ m/^{.*}$/ );
my $json = eval { JSON->new->utf8(0)->decode($data) };
if($@)
{
Log3 $name, 2, "$name: json evaluation error on getUsers ".$@;
return undef;
}
my @users = ();
foreach my $user (@{$json->{body}{users}}) {
@ -965,8 +978,12 @@ sub withings_getDevices($) {
#my $response = $ua->request($request);
return undef if(!defined($data));
my $json = ();
$json = JSON->new->utf8(0)->decode($data) if( $data =~ m/^{.*}$/ );;
my $json = eval { JSON->new->utf8(0)->decode($data) };
if($@)
{
Log3 $name, 2, "$name: json evaluation error on getDevices ".$@;
return undef;
}
Log3 "withings", 5, "$name: getdevices ".Dumper($json);
my @devices = ();
@ -998,8 +1015,12 @@ sub withings_getDeviceDetail($) {
#Log3 "withings", 5, "$name: getdevicedetaildata ".Dumper($data);
return undef if(!defined($data));
my $json = ();
$json = JSON->new->utf8(0)->decode($data) if( $data =~ m/^{.*}$/ );
my $json = eval { JSON->new->utf8(0)->decode($data) };
if($@)
{
Log3 $name, 2, "$name: json evaluation error on getDeviceDetail ".$@;
return undef;
}
if($json)
{
@ -1045,8 +1066,12 @@ sub withings_getDeviceLink($) {
#my $response = $ua->request($request);
return undef if(!defined($data));
my $json = ();
$json = JSON->new->utf8(0)->decode($data) if( $data =~ m/^{.*}$/ );
my $json = eval { JSON->new->utf8(0)->decode($data) };
if($@)
{
Log3 $name, 2, "$name: json evaluation error on getDeviceLink ".$@;
return undef;
}
return $json->{body};
}
@ -1308,8 +1333,12 @@ sub withings_getVideoLink($) {
});
return undef if(!defined($data));
my $json = ();
$json = JSON->new->utf8(0)->decode($data) if( $data =~ m/^{.*}$/ );
my $json = eval { JSON->new->utf8(0)->decode($data) };
if($@)
{
Log3 $name, 2, "$name: json evaluation error on getVideoLink ".$@;
return undef;
}
if(defined($json->{body}{device}))
{
@ -1340,8 +1369,12 @@ sub withings_getS3Credentials($) {
});
return undef if(!defined($data));
my $json = ();
$json = JSON->new->utf8(0)->decode($data) if( $data =~ m/^{.*}$/ );
my $json = eval { JSON->new->utf8(0)->decode($data) };
if($@)
{
Log3 $name, 2, "$name: json evaluation error on getS3Credentials ".$@;
return undef;
}
if(defined($json->{body}{sts}))
{
@ -1397,8 +1430,12 @@ sub withings_getUserDetail($) {
});
return undef if(!defined($data));
my $json = ();
$json = JSON->new->utf8(0)->decode($data) if( $data =~ m/^{.*}$/ );
my $json = eval { JSON->new->utf8(0)->decode($data) };
if($@)
{
Log3 $name, 2, "$name: json evaluation error on getUserDetail ".$@;
return undef;
}
return $json->{body}{users}[0];
}
@ -3080,8 +3117,12 @@ sub withings_Dispatch($$$) {
return undef;
}
my $json;
$json = JSON->new->utf8(0)->decode($data);
my $json = eval { JSON->new->utf8(0)->decode($data) };
if($@)
{
Log3 $name, 2, "$name: json evaluation error on dispatch type ".$param->{type}." ".$@;
return undef;
}
Log3 $name, 4, "$name: json returned: ".Dumper($json);