git.iko.soy/typeable/pandoc
on master help sign in
Shared: normalizeDate should reject illegal years.

We only allow years between 1601 and 9999, inclusive. The ISO 8601 actually says that years are supposed to start with 1583, but MS Word only allows 1601-9999. This should stop corrupted word files if the date is out of that range, or is parsed incorrectly.

commitbbfcd50fb14fa0754e564c99eec01763ba701400 authorJesse Rosenthal <jrosenthal@jhu.edu> dateSat 9 Jul 2016, 15:37 (10 years ago) parent4816fac
1 file changed+10−5
src/Text/Pandoc/Shared.hs+10−5
src/Text/Pandoc/Shared.hs+10−5
@@ -324,17 +324,22 @@ tabFilter tabStop =
324324-- Date/time
325325--
326326
327−-- | Parse a date and convert (if possible) to "YYYY-MM-DD" format.
327+-- | Parse a date and convert (if possible) to "YYYY-MM-DD" format. We
328+-- limit years to the range 1601-9999 (ISO 8601 accepts greater than
329+-- or equal to 1583, but MS Word only accepts dates starting 1601.
328330normalizeDate :: String -> Maybe String
329−normalizeDate s = fmap (formatTime defaultTimeLocale "%F")
330− (msum $ map (\fs -> parsetimeWith fs s) formats :: Maybe Day)
331− where parsetimeWith =
331+normalizeDate s = case toGregorian <$> day of
332+ Just (y, _, _) | y >= 1601 && y <= 9999 ->
333+ fmap (formatTime defaultTimeLocale "%F") day
334+ _ -> Nothing
335+ where day = (msum $ map (\fs -> parsetimeWith fs s) formats :: Maybe Day)
336+ parsetimeWith =
332337#if MIN_VERSION_time(1,5,0)
333338 parseTimeM True defaultTimeLocale
334339#else
335340 parseTime defaultTimeLocale
336341#endif
337− formats = ["%x","%m/%d/%Y", "%D","%F", "%d %b %Y",
342+ formats = ["%x","%m/%d/%Y", "%D","%F", "%d %b %Y",
338343 "%d %B %Y", "%b. %d, %Y", "%B %d, %Y",
339344 "%Y%m%d", "%Y%m", "%Y"]
340345