From 93a7f7d1495795b731242e270b6dc76b1ad6b0dc Mon Sep 17 00:00:00 2001 From: Naoki Kambe Date: Fri, 5 Aug 2011 14:22:28 +0900 Subject: [PATCH] [trac929] add more strict check for date and time format (add reverse check) --- src/lib/config/module_spec.cc | 9 ++++++++- src/lib/config/tests/module_spec_unittests.cc | 2 +- src/lib/python/isc/config/module_spec.py | 6 ++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/lib/config/module_spec.cc b/src/lib/config/module_spec.cc index eed6b7243..27cf99390 100644 --- a/src/lib/config/module_spec.cc +++ b/src/lib/config/module_spec.cc @@ -103,8 +103,15 @@ check_format(ConstElementPtr value, ConstElementPtr format_name) { BOOST_FOREACH (const format_types::value_type& f, time_formats) { if (format_name->stringValue() == f.first) { struct tm tm; + char buf[255] = ""; + memset(&tm, 0, sizeof(tm)); + // reverse check return (strptime(value->stringValue().c_str(), - f.second.c_str(), &tm) != NULL); + f.second.c_str(), &tm) != NULL + && strftime(buf, sizeof(buf), + f.second.c_str(), &tm) != 0 + && strcmp(value->stringValue().c_str(), + buf) == 0); } } return (false); diff --git a/src/lib/config/tests/module_spec_unittests.cc b/src/lib/config/tests/module_spec_unittests.cc index 315a78df0..cfd0ff521 100644 --- a/src/lib/config/tests/module_spec_unittests.cc +++ b/src/lib/config/tests/module_spec_unittests.cc @@ -287,7 +287,7 @@ TEST(ModuleSpec, CheckFormat) { item_default = "\"item_default\": \"2011-05-27\","; item_format = "\"item_format\": \"date\""; specs.push_back("," + item_default + item_format); - item_default = "\"item_default\": \"19:42:57Z\","; + item_default = "\"item_default\": \"19:42:57\","; item_format = "\"item_format\": \"time\""; specs.push_back("," + item_default + item_format); diff --git a/src/lib/python/isc/config/module_spec.py b/src/lib/python/isc/config/module_spec.py index d12008009..b79f92823 100644 --- a/src/lib/python/isc/config/module_spec.py +++ b/src/lib/python/isc/config/module_spec.py @@ -330,8 +330,10 @@ def _check_format(value, format_name): for fmt in time_formats: if format_name == fmt: try: - time.strptime(value, time_formats[fmt]) - return True + # reverse check + return value == time.strftime( + time_formats[fmt], + time.strptime(value, time_formats[fmt])) except (ValueError, TypeError): break return False -- GitLab