Discussion:
Compare Hex to Decimal
Michael Kennedy
2018-04-13 15:42:29 UTC
Permalink
Hi,


I have a URL that contains a Unix hex timestamp (something like 5ace8c5d). The timestamp should be within X minutes of now, and is used as part of an access control mechanism. I want to do something like this:


if (std.time(req.http.x-hextime, now) < now - 1w) {
...
}

Is there an efficient way in VCL to either convert Hex to Dec so that this comparison can be done, or some other solution that does not involve changing the data in the URL.

Thanks,

Michael
j***@codepuppy.com
2018-04-14 14:06:26 UTC
Permalink
Hi Michael,

I did something similar to this several years back using inline C:
https://varnish-cache.org/docs/5.1/users-guide/vcl-inline-c.html <https://varnish-cache.org/docs/5.1/users-guide/vcl-inline-c.html>

I’m super-rusty on my C, so ask better minds, but I’d start by doing the if check in C and leaving the rest of the logic in VCL. I.e.
strtol() with a base of 16 (see http://pubs.opengroup.org/onlinepubs/7908799/xsh/strtol.html <http://pubs.opengroup.org/onlinepubs/7908799/xsh/strtol.html>) and then a direct check of that against gmtime() - delta (hopefully your unix hex timestamps in UTC?). Double-check the input to strtol is safe — this approach has untrusted inputs getting run through into C.

-Jeff
Post by Michael Kennedy
Hi,
if (std.time(req.http.x-hextime, now) < now - 1w) {
...
}
Is there an efficient way in VCL to either convert Hex to Dec so that this comparison can be done, or some other solution that does not involve changing the data in the URL.
Thanks,
Michael
_______________________________________________
varnish-misc mailing list
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc <https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc>
Loading...