2222#include "jwt.h"
2323#include "timing.h"
2424
25- #include <ts/ts.h>
2625#include <ts/remap.h>
2726
2827#include <stdio.h>
28+ #include <stdlib.h>
2929#include <string.h>
3030#include <inttypes.h>
3131
@@ -157,6 +157,8 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
157157 int cpi = 0 ;
158158 int url_ct = 0 ;
159159 const char * url = NULL ;
160+ char * strip_uri = NULL ;
161+ TSRemapStatus status = TSREMAP_NO_REMAP ;
160162
161163 const char * package = "URISigningPackage" ;
162164
@@ -176,16 +178,22 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
176178 checkpoints [cpi ++ ] = mark_timer (& t );
177179 }
178180
179- char strip_uri [2000 ] = {0 };
181+ int strip_size = url_ct + 1 ;
182+ strip_uri = (char * )TSmalloc (strip_size );
183+ memset (strip_uri , 0 , strip_size );
184+
180185 size_t strip_ct ;
181- cjose_jws_t * jws = get_jws_from_uri (url , url_ct , package , strip_uri , 2000 , & strip_ct );
186+ cjose_jws_t * jws = get_jws_from_uri (url , url_ct , package , strip_uri , strip_size , & strip_ct );
182187
183188 if (cpi < max_cpi ) {
184189 checkpoints [cpi ++ ] = mark_timer (& t );
185190 }
186191 int checked_cookies = 0 ;
187192 if (!jws ) {
188193 check_cookies :
194+ /* There is no valid token in the url */
195+ strncpy (strip_uri , url , url_ct );
196+ strip_ct = url_ct ;
189197 ++ checked_cookies ;
190198
191199 TSMLoc field ;
@@ -218,6 +226,55 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
218226 checkpoints [cpi ++ ] = mark_timer (& t );
219227 }
220228 jws = get_jws_from_cookie (& client_cookie , & client_cookie_sz_ct , package );
229+ } else {
230+ /* There has been a JWS found in the url */
231+ /* Strip the token from the URL for upstream if configured to do so */
232+ if (config_strip_token ((struct config * )ih )) {
233+ if ((int )strip_ct != url_ct ) {
234+ int map_url_ct = 0 ;
235+ char * map_url = NULL ;
236+ char * map_strip_uri = NULL ;
237+ map_url = TSUrlStringGet (rri -> requestBufp , rri -> requestUrl , & map_url_ct );
238+
239+ PluginDebug ("Stripping Token from requestUrl: %s" , map_url );
240+
241+ int map_strip_size = map_url_ct + 1 ;
242+ map_strip_uri = (char * )TSmalloc (map_strip_size );
243+ memset (map_strip_uri , 0 , map_strip_size );
244+ size_t map_strip_ct = 0 ;
245+
246+ cjose_jws_t * map_jws = get_jws_from_uri (map_url , map_url_ct , package , map_strip_uri , map_strip_size , & map_strip_ct );
247+ cjose_jws_release (map_jws );
248+
249+ char * strip_uri_start = & map_strip_uri [0 ];
250+ char * strip_uri_end = & map_strip_uri [map_strip_ct ];
251+ PluginDebug ("Stripping token from upstream url to: %s" , strip_uri_start );
252+
253+ TSParseResult parse_rc = TSUrlParse (rri -> requestBufp , rri -> requestUrl , (const char * * )& strip_uri_start , strip_uri_end );
254+ if (map_url != NULL ) {
255+ TSfree (map_url );
256+ }
257+ if (map_strip_uri != NULL ) {
258+ TSfree (map_strip_uri );
259+ }
260+
261+ if (parse_rc != TS_PARSE_DONE ) {
262+ PluginDebug ("Error in TSUrlParse" );
263+ goto fail ;
264+ }
265+ status = TSREMAP_DID_REMAP ;
266+ }
267+ }
268+ }
269+ /* Check auth_dir and pass through if configured */
270+ if (uri_matches_auth_directive ((struct config * )ih , url , url_ct )) {
271+ if (url != NULL ) {
272+ TSfree ((void * )url );
273+ }
274+ if (strip_uri != NULL ) {
275+ TSfree (strip_uri );
276+ }
277+ return TSREMAP_NO_REMAP ;
221278 }
222279 if (!jws ) {
223280 goto fail ;
@@ -226,8 +283,10 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
226283 if (cpi < max_cpi ) {
227284 checkpoints [cpi ++ ] = mark_timer (& t );
228285 }
286+
229287 struct jwt * jwt = validate_jws (jws , (struct config * )ih , strip_uri , strip_ct );
230288 cjose_jws_release (jws );
289+
231290 if (cpi < max_cpi ) {
232291 checkpoints [cpi ++ ] = mark_timer (& t );
233292 }
@@ -239,6 +298,8 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
239298 }
240299 }
241300
301+ /* There has been a validated JWT found in either the cookie or url */
302+
242303 struct signer * signer = config_signer ((struct config * )ih );
243304 char * cookie = renew (jwt , signer -> issuer , signer -> jwk , signer -> alg , package );
244305 jwt_delete (jwt );
@@ -260,23 +321,23 @@ TSRemapDoRemap(void *ih, TSHttpTxn txnp, TSRemapRequestInfo *rri)
260321 last_mark = checkpoints [i ];
261322 }
262323 PluginDebug ("Spent %" PRId64 " ns uri_signing verification of %.*s." , mark_timer (& t ), url_ct , url );
324+
263325 TSfree ((void * )url );
264- return TSREMAP_NO_REMAP ;
265- fail :
266- if (uri_matches_auth_directive ((struct config * )ih , url , url_ct )) {
267- if (url != NULL ) {
268- TSfree ((void * )url );
269- }
270- return TSREMAP_NO_REMAP ;
326+ if (strip_uri != NULL ) {
327+ TSfree (strip_uri );
271328 }
272-
329+ return status ;
330+ fail :
273331 PluginDebug ("Invalid JWT for %.*s" , url_ct , url );
274332 TSHttpTxnStatusSet (txnp , TS_HTTP_STATUS_FORBIDDEN );
275333 PluginDebug ("Spent %" PRId64 " ns uri_signing verification of %.*s." , mark_timer (& t ), url_ct , url );
276334
277335 if (url != NULL ) {
278336 TSfree ((void * )url );
279337 }
338+ if (strip_uri != NULL ) {
339+ TSfree (strip_uri );
340+ }
280341
281342 return TSREMAP_DID_REMAP ;
282343}
0 commit comments