98_JsonMod.pm: #109413: reading names reduced to ascii and within allowed chars

git-svn-id: https://svn.fhem.de/fhem/trunk@21516 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
herrmannj 2020-03-25 22:33:45 +00:00
parent 144433fc91
commit a9969502e0

View File

@ -29,6 +29,7 @@ use warnings;
use utf8; use utf8;
use Time::Local qw( timelocal timegm ); use Time::Local qw( timelocal timegm );
use Text::Balanced qw ( extract_codeblock extract_delimited ); use Text::Balanced qw ( extract_codeblock extract_delimited );
use Unicode::Normalize qw( NFD );
use HttpUtils; use HttpUtils;
#use Memory::Usage; #use Memory::Usage;
@ -421,6 +422,23 @@ sub JsonMod_DoReadings {
}; };
}; };
# sanitize reading names to comply with the rules
# (allowed chars: A-Za-z/\d_\.-)
my sub sanitizedSetReading {
my ($r, $v) = @_;
# convert into valid reading
$r = Unicode::Normalize::NFD($r);
$r =~ s/([^A-Za-z0-9\/_\.-])//g;
# prevent a totally stripped reading name
# todo, log it?
$r = "MASKED_$_index" unless($r);
$v//='';
$newReadings->{$r} = $v;
$oldReadings->{$r} = 1;
};
my sub multi { my sub multi {
my ($value, @refs) = @_; my ($value, @refs) = @_;
die ('jsonPath result not a list') if (ref($value) ne 'ARRAY'); die ('jsonPath result not a list') if (ref($value) ne 'ARRAY');
@ -434,8 +452,9 @@ sub JsonMod_DoReadings {
push @reading, $ref->($element); push @reading, $ref->($element);
}; };
$_index++; $_index++;
$newReadings->{$reading[0]} = $reading[1]; sanitizedSetReading($reading[0], $reading[1]);
$oldReadings->{$reading[0]} = 1; # $newReadings->{$reading[0]} = $reading[1];
# $oldReadings->{$reading[0]} = 1;
}; };
}; };
@ -448,8 +467,9 @@ sub JsonMod_DoReadings {
$value = $value->[0] if (ref($value) eq 'ARRAY' and scalar(@{$value})); $value = $value->[0] if (ref($value) eq 'ARRAY' and scalar(@{$value}));
$value //= $default; $value //= $default;
$newReadings->{$reading} = $value; sanitizedSetReading($reading, $value);
$oldReadings->{$reading} = 1; # $newReadings->{$reading} = $value;
# $oldReadings->{$reading} = 1;
return; return;
}; };