e70528
From d5753ec513fa5a4bdcf59fa298642fd0d3a3c364 Mon Sep 17 00:00:00 2001
e70528
From: Yusuke Endoh <mame@ruby-lang.org>
e70528
Date: Fri, 12 Nov 2021 12:11:13 +0900
e70528
Subject: [PATCH 1/2] Add length limit option for methods that parses date
e70528
 strings
e70528
e70528
This patch fixes CVE-2021-41817 and created from the commit
e70528
<https://github.com/ruby/date/commit/4f9b8e946ba98f0a1774f8e677baa4a45637ebb3>.
e70528
We didn't merge the files included in the original commit below, as those are
e70528
for rebasing date gem version.
e70528
e70528
* ext/date/date.gemspec
e70528
* ext/date/lib/date.rb
e70528
e70528
== Original commit message ==
e70528
e70528
`Date.parse` now raises an ArgumentError when a given date string is
e70528
longer than 128. You can configure the limit by giving `limit` keyword
e70528
arguments like `Date.parse(str, limit: 1000)`. If you pass `limit: nil`,
e70528
the limit is disabled.
e70528
e70528
Not only `Date.parse` but also the following methods are changed.
e70528
e70528
* Date._parse
e70528
* Date.parse
e70528
* DateTime.parse
e70528
* Date._iso8601
e70528
* Date.iso8601
e70528
* DateTime.iso8601
e70528
* Date._rfc3339
e70528
* Date.rfc3339
e70528
* DateTime.rfc3339
e70528
* Date._xmlschema
e70528
* Date.xmlschema
e70528
* DateTime.xmlschema
e70528
* Date._rfc2822
e70528
* Date.rfc2822
e70528
* DateTime.rfc2822
e70528
* Date._rfc822
e70528
* Date.rfc822
e70528
* DateTime.rfc822
e70528
* Date._jisx0301
e70528
* Date.jisx0301
e70528
* DateTime.jisx0301
e70528
---
e70528
 ext/date/date_core.c         | 384 +++++++++++++++++++++++++++--------
e70528
 test/date/test_date_parse.rb |  29 +++
e70528
 2 files changed, 325 insertions(+), 88 deletions(-)
e70528
e70528
diff --git a/ext/date/date_core.c b/ext/date/date_core.c
e70528
index c250633426..177ea0f6c5 100644
e70528
--- a/ext/date/date_core.c
e70528
+++ b/ext/date/date_core.c
e70528
@@ -4290,12 +4290,37 @@ date_s_strptime(int argc, VALUE *argv, VALUE klass)
e70528
 
e70528
 VALUE date__parse(VALUE str, VALUE comp);
e70528
 
e70528
+static size_t
e70528
+get_limit(VALUE opt)
e70528
+{
e70528
+    if (!NIL_P(opt)) {
e70528
+        VALUE limit = rb_hash_aref(opt, ID2SYM(rb_intern("limit")));
e70528
+        if (NIL_P(limit)) return SIZE_MAX;
e70528
+        return NUM2SIZET(limit);
e70528
+    }
e70528
+    return 128;
e70528
+}
e70528
+
e70528
+static void
e70528
+check_limit(VALUE str, VALUE opt)
e70528
+{
e70528
+    StringValue(str);
e70528
+    size_t slen = RSTRING_LEN(str);
e70528
+    size_t limit = get_limit(opt);
e70528
+    if (slen > limit) {
e70528
+	rb_raise(rb_eArgError,
e70528
+		 "string length (%"PRI_SIZE_PREFIX"u) exceeds the limit %"PRI_SIZE_PREFIX"u", slen, limit);
e70528
+    }
e70528
+}
e70528
+
e70528
 static VALUE
e70528
 date_s__parse_internal(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE vstr, vcomp, hash;
e70528
+    VALUE vstr, vcomp, hash, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "11", &vstr, &vcomp);
e70528
+    rb_scan_args(argc, argv, "11:", &vstr, &vcomp, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
+    check_limit(vstr, opt);
e70528
     StringValue(vstr);
e70528
     if (!rb_enc_str_asciicompat_p(vstr))
e70528
 	rb_raise(rb_eArgError,
e70528
@@ -4320,7 +4345,7 @@ date_s__parse_internal(int argc, VALUE *argv, VALUE klass)
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date._parse(string[, comp=true])  ->  hash
e70528
+ *    Date._parse(string[, comp=true], limit: 128)  ->  hash
e70528
  *
e70528
  * Parses the given representation of date and time, and returns a
e70528
  * hash of parsed elements.  This method does not function as a
e70528
@@ -4331,6 +4356,10 @@ date_s__parse_internal(int argc, VALUE *argv, VALUE klass)
e70528
  * it full.
e70528
  *
e70528
  *    Date._parse('2001-02-03')	#=> {:year=>2001, :mon=>2, :mday=>3}
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 date_s__parse(int argc, VALUE *argv, VALUE klass)
e70528
@@ -4340,7 +4369,7 @@ date_s__parse(int argc, VALUE *argv, VALUE klass)
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date.parse(string='-4712-01-01'[, comp=true[, start=Date::ITALY]])  ->  date
e70528
+ *    Date.parse(string='-4712-01-01'[, comp=true[, start=Date::ITALY]], limit: 128)  ->  date
e70528
  *
e70528
  * Parses the given representation of date and time, and creates a
e70528
  * date object.  This method does not function as a validator.
e70528
@@ -4352,13 +4381,18 @@ date_s__parse(int argc, VALUE *argv, VALUE klass)
e70528
  *    Date.parse('2001-02-03')		#=> #<Date: 2001-02-03 ...>
e70528
  *    Date.parse('20010203')		#=> #<Date: 2001-02-03 ...>
e70528
  *    Date.parse('3rd Feb 2001')	#=> #<Date: 2001-02-03 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 date_s_parse(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, comp, sg;
e70528
+    VALUE str, comp, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "03", &str, &comp, &sg;;
e70528
+    rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -4370,11 +4404,12 @@ date_s_parse(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE argv2[2], hash;
e70528
-
e70528
-	argv2[0] = str;
e70528
-	argv2[1] = comp;
e70528
-	hash = date_s__parse(2, argv2, klass);
e70528
+        int argc2 = 2;
e70528
+	VALUE argv2[3];
e70528
+        argv2[0] = str;
e70528
+        argv2[1] = comp;
e70528
+        if (!NIL_P(opt)) argv2[argc2++] = opt;
e70528
+	VALUE hash = date_s__parse(argc2, argv2, klass);
e70528
 	return d_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
@@ -4388,19 +4423,28 @@ VALUE date__jisx0301(VALUE);
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date._iso8601(string)  ->  hash
e70528
+ *    Date._iso8601(string, limit: 128)  ->  hash
e70528
  *
e70528
  * Returns a hash of parsed elements.
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
-date_s__iso8601(VALUE klass, VALUE str)
e70528
+date_s__iso8601(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
+    VALUE str, opt;
e70528
+
e70528
+    rb_scan_args(argc, argv, "1:", &str, &opt;;
e70528
+    check_limit(str, opt);
e70528
+
e70528
     return date__iso8601(str);
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date.iso8601(string='-4712-01-01'[, start=Date::ITALY])  ->  date
e70528
+ *    Date.iso8601(string='-4712-01-01'[, start=Date::ITALY], limit: 128)  ->  date
e70528
  *
e70528
  * Creates a new Date object by parsing from a string according to
e70528
  * some typical ISO 8601 formats.
e70528
@@ -4408,13 +4452,18 @@ date_s__iso8601(VALUE klass, VALUE str)
e70528
  *    Date.iso8601('2001-02-03')	#=> #<Date: 2001-02-03 ...>
e70528
  *    Date.iso8601('20010203')		#=> #<Date: 2001-02-03 ...>
e70528
  *    Date.iso8601('2001-W05-6')	#=> #<Date: 2001-02-03 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 date_s_iso8601(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -4424,38 +4473,56 @@ date_s_iso8601(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__iso8601(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        if (!NIL_P(opt)) argv2[argc2++] = opt;
e70528
+	VALUE hash = date_s__iso8601(argc2, argv2, klass);
e70528
 	return d_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date._rfc3339(string)  ->  hash
e70528
+ *    Date._rfc3339(string, limit: 128)  ->  hash
e70528
  *
e70528
  * Returns a hash of parsed elements.
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
-date_s__rfc3339(VALUE klass, VALUE str)
e70528
+date_s__rfc3339(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
+    VALUE str, opt;
e70528
+
e70528
+    rb_scan_args(argc, argv, "1:", &str, &opt;;
e70528
+    check_limit(str, opt);
e70528
+
e70528
     return date__rfc3339(str);
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date.rfc3339(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY])  ->  date
e70528
+ *    Date.rfc3339(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128)  ->  date
e70528
  *
e70528
  * Creates a new Date object by parsing from a string according to
e70528
  * some typical RFC 3339 formats.
e70528
  *
e70528
  *    Date.rfc3339('2001-02-03T04:05:06+07:00')	#=> #<Date: 2001-02-03 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 date_s_rfc3339(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -4465,38 +4532,56 @@ date_s_rfc3339(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__rfc3339(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        if (!NIL_P(opt)) argv2[argc2++] = opt;
e70528
+	VALUE hash = date_s__rfc3339(argc2, argv2, klass);
e70528
 	return d_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date._xmlschema(string)  ->  hash
e70528
+ *    Date._xmlschema(string, limit: 128)  ->  hash
e70528
  *
e70528
  * Returns a hash of parsed elements.
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
-date_s__xmlschema(VALUE klass, VALUE str)
e70528
+date_s__xmlschema(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
+    VALUE str, opt;
e70528
+
e70528
+    rb_scan_args(argc, argv, "1:", &str, &opt;;
e70528
+    check_limit(str, opt);
e70528
+
e70528
     return date__xmlschema(str);
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date.xmlschema(string='-4712-01-01'[, start=Date::ITALY])  ->  date
e70528
+ *    Date.xmlschema(string='-4712-01-01'[, start=Date::ITALY], limit: 128)  ->  date
e70528
  *
e70528
  * Creates a new Date object by parsing from a string according to
e70528
  * some typical XML Schema formats.
e70528
  *
e70528
  *    Date.xmlschema('2001-02-03')	#=> #<Date: 2001-02-03 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 date_s_xmlschema(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -4506,41 +4591,58 @@ date_s_xmlschema(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__xmlschema(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        if (!NIL_P(opt)) argv2[argc2++] = opt;
e70528
+	VALUE hash = date_s__xmlschema(argc2, argv2, klass);
e70528
 	return d_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date._rfc2822(string)  ->  hash
e70528
- *    Date._rfc822(string)   ->  hash
e70528
+ *    Date._rfc2822(string, limit: 128)  ->  hash
e70528
+ *    Date._rfc822(string, limit: 128)   ->  hash
e70528
  *
e70528
  * Returns a hash of parsed elements.
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
-date_s__rfc2822(VALUE klass, VALUE str)
e70528
+date_s__rfc2822(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
+    VALUE str, opt;
e70528
+
e70528
+    rb_scan_args(argc, argv, "1:", &str, &opt;;
e70528
+    check_limit(str, opt);
e70528
+
e70528
     return date__rfc2822(str);
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date.rfc2822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY])  ->  date
e70528
- *    Date.rfc822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY])   ->  date
e70528
+ *    Date.rfc2822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128)  ->  date
e70528
+ *    Date.rfc822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128)   ->  date
e70528
  *
e70528
  * Creates a new Date object by parsing from a string according to
e70528
  * some typical RFC 2822 formats.
e70528
  *
e70528
  *    Date.rfc2822('Sat, 3 Feb 2001 00:00:00 +0000')
e70528
  *						#=> #<Date: 2001-02-03 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 date_s_rfc2822(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -4550,39 +4652,56 @@ date_s_rfc2822(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__rfc2822(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        if (!NIL_P(opt)) argv2[argc2++] = opt;
e70528
+	VALUE hash = date_s__rfc2822(argc2, argv2, klass);
e70528
 	return d_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date._httpdate(string)  ->  hash
e70528
+ *    Date._httpdate(string, limit: 128)  ->  hash
e70528
  *
e70528
  * Returns a hash of parsed elements.
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
-date_s__httpdate(VALUE klass, VALUE str)
e70528
+date_s__httpdate(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
+    VALUE str, opt;
e70528
+
e70528
+    rb_scan_args(argc, argv, "1:", &str, &opt;;
e70528
+    check_limit(str, opt);
e70528
+
e70528
     return date__httpdate(str);
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date.httpdate(string='Mon, 01 Jan -4712 00:00:00 GMT'[, start=Date::ITALY])  ->  date
e70528
+ *    Date.httpdate(string='Mon, 01 Jan -4712 00:00:00 GMT'[, start=Date::ITALY], limit: 128)  ->  date
e70528
  *
e70528
  * Creates a new Date object by parsing from a string according to
e70528
  * some RFC 2616 format.
e70528
  *
e70528
  *    Date.httpdate('Sat, 03 Feb 2001 00:00:00 GMT')
e70528
  *						#=> #<Date: 2001-02-03 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 date_s_httpdate(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -4592,38 +4711,56 @@ date_s_httpdate(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__httpdate(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        if (!NIL_P(opt)) argv2[argc2++] = opt;
e70528
+	VALUE hash = date_s__httpdate(argc2, argv2, klass);
e70528
 	return d_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date._jisx0301(string)  ->  hash
e70528
+ *    Date._jisx0301(string, limit: 128)  ->  hash
e70528
  *
e70528
  * Returns a hash of parsed elements.
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
-date_s__jisx0301(VALUE klass, VALUE str)
e70528
+date_s__jisx0301(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
+    VALUE str, opt;
e70528
+
e70528
+    rb_scan_args(argc, argv, "1:", &str, &opt;;
e70528
+    check_limit(str, opt);
e70528
+
e70528
     return date__jisx0301(str);
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    Date.jisx0301(string='-4712-01-01'[, start=Date::ITALY])  ->  date
e70528
+ *    Date.jisx0301(string='-4712-01-01'[, start=Date::ITALY], limit: 128)  ->  date
e70528
  *
e70528
  * Creates a new Date object by parsing from a string according to
e70528
  * some typical JIS X 0301 formats.
e70528
  *
e70528
  *    Date.jisx0301('H13.02.03')		#=> #<Date: 2001-02-03 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 date_s_jisx0301(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -4633,7 +4770,11 @@ date_s_jisx0301(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__jisx0301(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        if (!NIL_P(opt)) argv2[argc2++] = opt;
e70528
+	VALUE hash = date_s__jisx0301(argc2, argv2, klass);
e70528
 	return d_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
@@ -7925,7 +8066,7 @@ datetime_s_strptime(int argc, VALUE *argv, VALUE klass)
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    DateTime.parse(string='-4712-01-01T00:00:00+00:00'[, comp=true[, start=Date::ITALY]])  ->  datetime
e70528
+ *    DateTime.parse(string='-4712-01-01T00:00:00+00:00'[, comp=true[, start=Date::ITALY]], limit: 128)  ->  datetime
e70528
  *
e70528
  * Parses the given representation of date and time, and creates a
e70528
  * DateTime object.  This method does not function as a validator.
e70528
@@ -7939,13 +8080,18 @@ datetime_s_strptime(int argc, VALUE *argv, VALUE klass)
e70528
  *				#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
e70528
  *    DateTime.parse('3rd Feb 2001 04:05:06 PM')
e70528
  *				#=> #<DateTime: 2001-02-03T16:05:06+00:00 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 datetime_s_parse(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, comp, sg;
e70528
+    VALUE str, comp, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "03", &str, &comp, &sg;;
e70528
+    rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -7957,18 +8103,20 @@ datetime_s_parse(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE argv2[2], hash;
e70528
-
e70528
-	argv2[0] = str;
e70528
-	argv2[1] = comp;
e70528
-	hash = date_s__parse(2, argv2, klass);
e70528
+        int argc2 = 2;
e70528
+        VALUE argv2[3];
e70528
+        argv2[0] = str;
e70528
+        argv2[1] = comp;
e70528
+        argv2[2] = opt;
e70528
+        if (!NIL_P(opt)) argc2++;
e70528
+	VALUE hash = date_s__parse(argc2, argv2, klass);
e70528
 	return dt_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    DateTime.iso8601(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY])  ->  datetime
e70528
+ *    DateTime.iso8601(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128)  ->  datetime
e70528
  *
e70528
  * Creates a new DateTime object by parsing from a string according to
e70528
  * some typical ISO 8601 formats.
e70528
@@ -7979,13 +8127,18 @@ datetime_s_parse(int argc, VALUE *argv, VALUE klass)
e70528
  *				#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
e70528
  *    DateTime.iso8601('2001-W05-6T04:05:06+07:00')
e70528
  *				#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 datetime_s_iso8601(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -7995,27 +8148,37 @@ datetime_s_iso8601(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__iso8601(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        argv2[1] = opt;
e70528
+        if (!NIL_P(opt)) argc2--;
e70528
+	VALUE hash = date_s__iso8601(argc2, argv2, klass);
e70528
 	return dt_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    DateTime.rfc3339(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY])  ->  datetime
e70528
+ *    DateTime.rfc3339(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128)  ->  datetime
e70528
  *
e70528
  * Creates a new DateTime object by parsing from a string according to
e70528
  * some typical RFC 3339 formats.
e70528
  *
e70528
  *    DateTime.rfc3339('2001-02-03T04:05:06+07:00')
e70528
  *				#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 datetime_s_rfc3339(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -8025,27 +8188,37 @@ datetime_s_rfc3339(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__rfc3339(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        argv2[1] = opt;
e70528
+        if (!NIL_P(opt)) argc2++;
e70528
+	VALUE hash = date_s__rfc3339(argc2, argv2, klass);
e70528
 	return dt_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    DateTime.xmlschema(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY])  ->  datetime
e70528
+ *    DateTime.xmlschema(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128)  ->  datetime
e70528
  *
e70528
  * Creates a new DateTime object by parsing from a string according to
e70528
  * some typical XML Schema formats.
e70528
  *
e70528
  *    DateTime.xmlschema('2001-02-03T04:05:06+07:00')
e70528
  *				#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 datetime_s_xmlschema(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -8055,28 +8228,38 @@ datetime_s_xmlschema(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__xmlschema(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        argv2[1] = opt;
e70528
+        if (!NIL_P(opt)) argc2++;
e70528
+	VALUE hash = date_s__xmlschema(argc2, argv2, klass);
e70528
 	return dt_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    DateTime.rfc2822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY])  ->  datetime
e70528
- *    DateTime.rfc822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY])   ->  datetime
e70528
+ *    DateTime.rfc2822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128)  ->  datetime
e70528
+ *    DateTime.rfc822(string='Mon, 1 Jan -4712 00:00:00 +0000'[, start=Date::ITALY], limit: 128)   ->  datetime
e70528
  *
e70528
  * Creates a new DateTime object by parsing from a string according to
e70528
  * some typical RFC 2822 formats.
e70528
  *
e70528
  *     DateTime.rfc2822('Sat, 3 Feb 2001 04:05:06 +0700')
e70528
  *				#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 datetime_s_rfc2822(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -8086,7 +8269,12 @@ datetime_s_rfc2822(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__rfc2822(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        argv2[1] = opt;
e70528
+        if (!NIL_P(opt)) argc2++;
e70528
+	VALUE hash = date_s__rfc2822(argc2, argv2, klass);
e70528
 	return dt_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
@@ -8100,13 +8288,18 @@ datetime_s_rfc2822(int argc, VALUE *argv, VALUE klass)
e70528
  *
e70528
  *    DateTime.httpdate('Sat, 03 Feb 2001 04:05:06 GMT')
e70528
  *				#=> #<DateTime: 2001-02-03T04:05:06+00:00 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 datetime_s_httpdate(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -8116,27 +8309,37 @@ datetime_s_httpdate(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__httpdate(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        argv2[1] = opt;
e70528
+        if (!NIL_P(opt)) argc2++;
e70528
+	VALUE hash = date_s__httpdate(argc2, argv2, klass);
e70528
 	return dt_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
 
e70528
 /*
e70528
  * call-seq:
e70528
- *    DateTime.jisx0301(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY])  ->  datetime
e70528
+ *    DateTime.jisx0301(string='-4712-01-01T00:00:00+00:00'[, start=Date::ITALY], limit: 128)  ->  datetime
e70528
  *
e70528
  * Creates a new DateTime object by parsing from a string according to
e70528
  * some typical JIS X 0301 formats.
e70528
  *
e70528
  *    DateTime.jisx0301('H13.02.03T04:05:06+07:00')
e70528
  *				#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
e70528
+ *
e70528
+ * Raise an ArgumentError when the string length is longer than _limit_.
e70528
+ * You can stop this check by passing `limit: nil`, but note that
e70528
+ * it may take a long time to parse.
e70528
  */
e70528
 static VALUE
e70528
 datetime_s_jisx0301(int argc, VALUE *argv, VALUE klass)
e70528
 {
e70528
-    VALUE str, sg;
e70528
+    VALUE str, sg, opt;
e70528
 
e70528
-    rb_scan_args(argc, argv, "02", &str, &sg;;
e70528
+    rb_scan_args(argc, argv, "02:", &str, &sg, &opt;;
e70528
+    if (!NIL_P(opt)) argc--;
e70528
 
e70528
     switch (argc) {
e70528
       case 0:
e70528
@@ -8146,7 +8349,12 @@ datetime_s_jisx0301(int argc, VALUE *argv, VALUE klass)
e70528
     }
e70528
 
e70528
     {
e70528
-	VALUE hash = date_s__jisx0301(klass, str);
e70528
+        int argc2 = 1;
e70528
+        VALUE argv2[2];
e70528
+        argv2[0] = str;
e70528
+        argv2[1] = opt;
e70528
+        if (!NIL_P(opt)) argc2++;
e70528
+	VALUE hash = date_s__jisx0301(argc2, argv2, klass);
e70528
 	return dt_new_by_frags(klass, hash, sg);
e70528
     }
e70528
 }
e70528
@@ -9297,19 +9505,19 @@ Init_date_core(void)
e70528
     rb_define_singleton_method(cDate, "strptime", date_s_strptime, -1);
e70528
     rb_define_singleton_method(cDate, "_parse", date_s__parse, -1);
e70528
     rb_define_singleton_method(cDate, "parse", date_s_parse, -1);
e70528
-    rb_define_singleton_method(cDate, "_iso8601", date_s__iso8601, 1);
e70528
+    rb_define_singleton_method(cDate, "_iso8601", date_s__iso8601, -1);
e70528
     rb_define_singleton_method(cDate, "iso8601", date_s_iso8601, -1);
e70528
-    rb_define_singleton_method(cDate, "_rfc3339", date_s__rfc3339, 1);
e70528
+    rb_define_singleton_method(cDate, "_rfc3339", date_s__rfc3339, -1);
e70528
     rb_define_singleton_method(cDate, "rfc3339", date_s_rfc3339, -1);
e70528
-    rb_define_singleton_method(cDate, "_xmlschema", date_s__xmlschema, 1);
e70528
+    rb_define_singleton_method(cDate, "_xmlschema", date_s__xmlschema, -1);
e70528
     rb_define_singleton_method(cDate, "xmlschema", date_s_xmlschema, -1);
e70528
-    rb_define_singleton_method(cDate, "_rfc2822", date_s__rfc2822, 1);
e70528
-    rb_define_singleton_method(cDate, "_rfc822", date_s__rfc2822, 1);
e70528
+    rb_define_singleton_method(cDate, "_rfc2822", date_s__rfc2822, -1);
e70528
+    rb_define_singleton_method(cDate, "_rfc822", date_s__rfc2822, -1);
e70528
     rb_define_singleton_method(cDate, "rfc2822", date_s_rfc2822, -1);
e70528
     rb_define_singleton_method(cDate, "rfc822", date_s_rfc2822, -1);
e70528
-    rb_define_singleton_method(cDate, "_httpdate", date_s__httpdate, 1);
e70528
+    rb_define_singleton_method(cDate, "_httpdate", date_s__httpdate, -1);
e70528
     rb_define_singleton_method(cDate, "httpdate", date_s_httpdate, -1);
e70528
-    rb_define_singleton_method(cDate, "_jisx0301", date_s__jisx0301, 1);
e70528
+    rb_define_singleton_method(cDate, "_jisx0301", date_s__jisx0301, -1);
e70528
     rb_define_singleton_method(cDate, "jisx0301", date_s_jisx0301, -1);
e70528
 
e70528
 #ifndef NDEBUG
e70528
diff --git a/test/date/test_date_parse.rb b/test/date/test_date_parse.rb
e70528
index ac0eb85ca7..f9b160ee8c 100644
e70528
--- a/test/date/test_date_parse.rb
e70528
+++ b/test/date/test_date_parse.rb
e70528
@@ -1,6 +1,7 @@
e70528
 # frozen_string_literal: true
e70528
 require 'test/unit'
e70528
 require 'date'
e70528
+require 'timeout'
e70528
 
e70528
 class TestDateParse < Test::Unit::TestCase
e70528
 
e70528
@@ -1122,4 +1123,32 @@ def test_given_string
e70528
     assert_equal(s0, s)
e70528
   end
e70528
 
e70528
+  def test_length_limit
e70528
+    assert_raise(ArgumentError) { Date._parse("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date._iso8601("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date._rfc3339("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date._xmlschema("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date._rfc2822("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date._rfc822("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date._jisx0301("1" * 1000) }
e70528
+
e70528
+    assert_raise(ArgumentError) { Date.parse("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date.iso8601("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date.rfc3339("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date.xmlschema("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date.rfc2822("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date.rfc822("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { Date.jisx0301("1" * 1000) }
e70528
+
e70528
+    assert_raise(ArgumentError) { DateTime.parse("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { DateTime.iso8601("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { DateTime.rfc3339("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { DateTime.xmlschema("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { DateTime.rfc2822("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { DateTime.rfc822("1" * 1000) }
e70528
+    assert_raise(ArgumentError) { DateTime.jisx0301("1" * 1000) }
e70528
+
e70528
+    assert_raise(ArgumentError) { Date._parse("Jan " + "9" * 1000000) }
e70528
+    assert_raise(Timeout::Error) { Timeout.timeout(1) { Date._parse("Jan " + "9" * 1000000, limit: nil) } }
e70528
+  end
e70528
 end
e70528
-- 
e70528
2.36.1
e70528