Re: [PATCH] BUG/MINOR: tools: fix parsing "us" unit for timers

2021-04-05 Thread Christopher Faulet

Le 02/04/2021 à 22:12, Thayne McCombs a écrit :

Commit c20ad0d8dbd1bb5707bbfe23632415c3062e046c (BUG/MINOR: tools:  make
parse_time_err() more strict on the timer validity) broke parsing the "us"
unit in timers. It caused `parse_time_err()` to return the string "s",
which indicates an error.

Now if the "u" is followed by an "s" we properly continue processing the
time instead of immediately failing.



Thanks, now merged !

--
Christopher Faulet



Re: [PATCH] JWT payloads break b64dec convertor

2021-04-05 Thread Moemen MHEDHBI
Thanks Willy and Tim for your feedback.

You can find attached the updated patches with fixed coding style (now
set correctly in my editor), updated commit message, entry doc in sorted
order, size_t instead of int in both enc/dec  and corresponding reg-test.

Only part unclear:
On 02/04/2021 15:04, Tim Düsterhus wrote:
>> +int base64urldec(const char *in, size_t ilen, char *out, size_t olen) {
>> +char conv[ilen+2];
>
> This looks like a remotely triggerable stack overflow.

You mean in case ilen is too big? in such case should we rather use
dynamic allocation ?

-- 
Moemen MHEDHBI
>From bae8d3890be6d2f5a58697bf7b8e9f01f4589d3b Mon Sep 17 00:00:00 2001
From: Moemen MHEDHBI 
Date: Thu, 1 Apr 2021 20:53:59 +0200
Subject: [PATCH 1/2] MINOR: sample: add ub64dec and ub64enc converters

ub64dec and ub64enc are the base64url equivalent of b64dec and base64
converters. base64url encoding is the "URL and Filename Safe Alphabet"
variant of base64 encoding. It is also used in in JWT (JSON Web Token)
standard.
RFC1421 mention in base64.c file is deprecated so it was replaced with
RFC4648 to which existing converters, base64/b64dec, still apply.

Example:
  HAProxy:
http-request return content-type text/plain lf-string %[req.hdr(Authorization),word(2,.),ub64dec]
  Client:
Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZm9vIiwia2V5IjoiY2hhZTZBaFhhaTZlIn0.5VsVj7mdxVvo1wP5c0dVHnr-S_khnIdFkThqvwukmdg
$ curl -H "Authorization: Bearer ${TOKEN}" http://haproxy.local
{"user":"foo","key":"chae6AhXai6e"}
---
 doc/configuration.txt| 12 ++
 include/haproxy/base64.h |  2 +
 reg-tests/sample_fetches/ubase64.vtc | 24 +++
 src/base64.c | 59 +++-
 src/sample.c | 38 ++
 5 files changed, 134 insertions(+), 1 deletion(-)
 create mode 100644 reg-tests/sample_fetches/ubase64.vtc

diff --git a/doc/configuration.txt b/doc/configuration.txt
index 7048fb63e..c7fe416e5 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -16393,6 +16393,18 @@ table_trackers()
   connections there are from a given address for example. See also the
   sc_trackers sample fetch keyword.
 
+ub64dec
+  This converter is the base64url variant of b64dec converter. base64url
+	encoding is the "URL and Filename Safe Alphabet" variant of base64 encoding.
+	It is also the encoding used in JWT (JSON Web Token) standard.
+
+	Example:
+	  # Decoding a JWT payload:
+	  http-request set-var(txn.token_payload) req.hdr(Authorization),word(2,.),ub64dec
+
+ub64enc
+  This converter is the base64url variant of base64 converter.
+
 upper
   Convert a string sample to upper case. This can only be placed after a string
   sample fetch function or after a transformation keyword returning a string
diff --git a/include/haproxy/base64.h b/include/haproxy/base64.h
index 1756bc058..532c46a44 100644
--- a/include/haproxy/base64.h
+++ b/include/haproxy/base64.h
@@ -17,7 +17,9 @@
 #include 
 
 int a2base64(char *in, int ilen, char *out, int olen);
+int a2base64url(char *in, size_t ilen, char *out, size_t olen);
 int base64dec(const char *in, size_t ilen, char *out, size_t olen);
+int base64urldec(const char *in, size_t ilen, char *out, size_t olen);
 const char *s30tob64(int in, char *out);
 int b64tos30(const char *in);
 
diff --git a/reg-tests/sample_fetches/ubase64.vtc b/reg-tests/sample_fetches/ubase64.vtc
new file mode 100644
index 0..a273321d2
--- /dev/null
+++ b/reg-tests/sample_fetches/ubase64.vtc
@@ -0,0 +1,24 @@
+varnishtest "ub64dec sample fetche Test"
+
+#REQUIRE_VERSION=2.4
+
+feature ignore_unknown_macro
+
+haproxy h1 -conf {
+defaults
+mode http
+timeout connect 1s
+timeout client  1s
+timeout server  1s
+
+frontend fe
+bind "fd@${fe}"
+http-request return content-type text/plain hdr encode %[hdr(input),ub64enc] lf-string %[req.hdr(Authorization),word(2,.),ub64dec]
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+txreq -url "/" -hdr "input: biduule" -hdr "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZm9vIiwia2V5IjoiY2hhZTZBaFhhaTZlIn0.5VsVj7mdxVvo1wP5c0dVHnr-S_khnIdFkThqvwukmdg"
+rxresp
+expect resp.http.encode == "YmlkdXVsZQ"
+expect resp.body == "{\"user\":\"foo\",\"key\":\"chae6AhXai6e\"}"
+} -run
diff --git a/src/base64.c b/src/base64.c
index 53e4d65b2..c53c8b076 100644
--- a/src/base64.c
+++ b/src/base64.c
@@ -1,5 +1,5 @@
 /*
- * ASCII <-> Base64 conversion as described in RFC1421.
+ * ASCII <-> Base64 conversion as described in RFC4648.
  *
  * Copyright 2006-2010 Willy Tarreau 
  * Copyright 2009-2010 Krzysztof Piotr Oledzki 
@@ -138,6 +138,63 @@ int base64dec(const char *in, size_t ilen, char *out, size_t olen) {
 	return convlen;
 }
 
+/* url variant of a2base64 */
+int a2base64url(char *in, size_t ilen, char *out, size_t olen)
+{
+	int convlen, i;
+
+	convlen = a2base64(in, ilen, out, olen);
+	while 

[PATCH 0/3] Additional ist functions

2021-04-05 Thread Tim Duesterhus
Willy,

some more `ist` helper functions that allows consumers to avoid directly
operating on the raw pointer, instead using safe high level functions.

These will be used in a future series of mine. I'm sending them for early
review and integration, because I believe their existence is useful on its
own.

Best regards

Tim Düsterhus (3):
  MINOR: ist: Add `istappend(struct ist, char)`
  MINOR: ist: Add `istshift(struct ist*)`
  MINOR: ist: Add `istsplit(struct ist*, char)`

 include/import/ist.h | 39 +++
 1 file changed, 39 insertions(+)

-- 
2.31.1




[PATCH 1/3] MINOR: ist: Add `istappend(struct ist, char)`

2021-04-05 Thread Tim Duesterhus
This function appends the given char to the given `ist` and returns
the resulting `ist`.
---
 include/import/ist.h | 10 ++
 1 file changed, 10 insertions(+)

diff --git a/include/import/ist.h b/include/import/ist.h
index 1262e8f59..3f63ed2dd 100644
--- a/include/import/ist.h
+++ b/include/import/ist.h
@@ -407,6 +407,16 @@ static inline int istneq(const struct ist ist1, const 
struct ist ist2, size_t co
return isteq(l, r);
 }
 
+/* appends  after . The caller must ensure that the underlying buffer
+ * is large enough to fit the character.
+ */
+static inline struct ist istappend(struct ist dst, const char src)
+{
+   dst.ptr[dst.len++] = src;
+
+   return dst;
+}
+
 /* copies  over  for a maximum of  bytes. Returns the number
  * of characters copied (src.len), or -1 if it does not fit. In all cases, the
  * contents are copied prior to reporting an error, so that the destination
-- 
2.31.1




[PATCH 2/3] MINOR: ist: Add `istshift(struct ist*)`

2021-04-05 Thread Tim Duesterhus
istshift() returns the first character and advances the ist by 1.
---
 include/import/ist.h | 15 +++
 1 file changed, 15 insertions(+)

diff --git a/include/import/ist.h b/include/import/ist.h
index 3f63ed2dd..6ece7cdf9 100644
--- a/include/import/ist.h
+++ b/include/import/ist.h
@@ -240,6 +240,21 @@ static inline struct ist istnext(const struct ist ist)
return ret;
 }
 
+/* Returns the first character of the  and advances the  by 1.
+ * If the  is empty the result is undefined.
+ */
+static inline char istshift(struct ist *ist)
+{
+   if (ist->len) {
+   char c = *ist->ptr;
+   *ist = istnext(*ist);
+
+   return c;
+   }
+
+   return 0;
+}
+
 /* copies the contents from string  to buffer  and adds a trailing
  * zero. The caller must ensure  is large enough.
  */
-- 
2.31.1




[PATCH 3/3] MINOR: ist: Add `istsplit(struct ist*, char)`

2021-04-05 Thread Tim Duesterhus
istsplit is a combination of iststop + istadv.
---
 include/import/ist.h | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/include/import/ist.h b/include/import/ist.h
index 6ece7cdf9..daf23d552 100644
--- a/include/import/ist.h
+++ b/include/import/ist.h
@@ -801,6 +801,20 @@ static inline struct ist istadv(const struct ist ist, 
const size_t nb)
return ist2(ist.ptr + nb, ist.len - nb);
 }
 
+/* Splits the given  at the given character. The returned ist is
+ * equivalent to iststop(ist, delim). The passed  will contain the
+ * remainder of the string, not including the delimiter. In other words
+ * it will be advanced by the length of the returned string plus 1.
+ */
+static inline struct ist istsplit(struct ist *ist, char delim)
+{
+   const struct ist result = iststop(*ist, delim);
+
+   *ist = istadv(*ist, result.len + 1);
+
+   return result;
+}
+
 /*
  * compare 2 ists and return non-zero if they are the same
  */
-- 
2.31.1