tj / node-cookie-signature

cookie signing
MIT License
180 stars 34 forks source link

Improve some variable naming (internal enhancement) #26

Closed natevw closed 2 years ago

natevw commented 6 years ago

This bit of code is a potential maintenance issue:

var str = val.slice(0, val.lastIndexOf('.'))
    , mac = exports.sign(str, secret);

We have three variables val, str, mac involved in some important logic and their names are confusing.

Maybe rename these something like:

Not urgent.

dschnare commented 4 years ago

@natevw I agree. Only I would perhaps suggest the following changes to improve understandability and maintainability:

  var decodedVal = decode(val)
    , expectedToken = exports.sign(decodedVal, secret)
    , expectedTokenBuffer = Buffer.from(expectedToken)
    , valBuffer = Buffer.from(val);

  return crypto.timingSafeEqual(expectedTokenBuffer, valBuffer) ? decodedVal : false;

Like you said though this is not urgent, otherwise I would have created a PR for this myself. I'll let the maintainers decide whether this is beneficial.

natevw commented 4 years ago

Thanks, yeah I like your "expected" terminology since its meaning is pretty clear. No worries about the PR since I don't plan on changing this just for its own sake. Rather, my thinking is to wait until (if…) the code were changing anyway…and so far there's been no reason for other maintenance.