handle invalid duration tags and change default to 24 hours (#89)

* handle invalid duration tags and change default to 24 hours
This commit is contained in:
Salah Al Saleh
2022-07-26 14:33:46 -07:00
committed by GitHub
parent 7b35b76304
commit a57748cbff
2 changed files with 25 additions and 13 deletions

View File

@@ -18,6 +18,10 @@ const client = redis.createClient({url: process.env["REDISCLOUD_URL"]});
const saddAsync = promisify(client.sadd).bind(client);
const hsetAsync = promisify(client.hset).bind(client);
const durationRegex = /^(?=\d+[ywdhms])(( ?\d+y)?(?!\d))?(( ?\d+w)?(?!\d))?(( ?\d+d)?(?!\d))?(( ?\d+h)?(?!\d))?(( ?\d+m)?(?!\d))?(( ?\d+s)?(?!\d))?( ?\d+ms)?$/;
const defaultDuration = 24 * 60 * 60 * 1000; // 24h
const maxDuration = 24 * 60 * 60 * 1000; // 24h
@Controller("/v1/hook")
export class HookAPI {
/**
@@ -50,24 +54,19 @@ export class HookAPI {
const imageWithTag = `${image}:${tag}`;
// default to 1h
let expireInSeconds = 60 * 60 * 1000;
console.log(`parsing tag ${tag}`);
const parsed = parseDuration(tag);
if (parsed > 0) {
expireInSeconds = parsed;
// enforce a max of 24 hours
if (expireInSeconds > 24 * 60 * 60 * 1000) {
expireInSeconds = 24 * 60 * 60 * 1000;
}
let expiresIn = durationfromTag(tag);
if (expiresIn <= 0) {
expiresIn = defaultDuration
}
if (expiresIn > maxDuration) {
expiresIn = maxDuration
}
await saddAsync("current.images", imageWithTag);
const now = new Date().getTime();
const then = now + expireInSeconds;
const then = now + expiresIn;
await hsetAsync(imageWithTag, "created", now, "expires", then);
}
@@ -76,3 +75,10 @@ export class HookAPI {
return {};
}
}
function durationfromTag(tag: string): number {
if (!durationRegex.test(tag)) {
return -1;
}
return parseDuration(tag);
}