Fix edit-meta API logic

Signed-off-by: Joe Ma <rikkaneko23@gmail.com>
This commit is contained in:
Joe Ma 2025-08-04 13:58:28 +08:00
parent a2347f9f94
commit bc0704f49d
No known key found for this signature in database
GPG key ID: 7A0ECF5F5EDC587F
2 changed files with 21 additions and 5 deletions

View file

@ -171,7 +171,8 @@ export function get_auth(headers: Headers, required_scheme: string): string | [s
return [decoded.slice(0, index), decoded.slice(index + 1)];
} else if (scheme == 'Bearer') {
return encoded;
if (encoded.length > 0) return encoded;
else null;
}
}
return null;

View file

@ -80,7 +80,7 @@ router.post('/info/:uuid', async (req, env, ctx) => {
);
}
// Check password and username should be empty
if (cert.length != 0 || descriptor.password !== sha256(cert).slice(0, 16)) {
if (descriptor.password !== sha256(cert as string).slice(0, 16)) {
return PasteAPIRepsonse.build(403, 'Invalid access credentials.');
}
}
@ -91,13 +91,23 @@ router.post('/info/:uuid', async (req, env, ctx) => {
// Change paste info logic
// Explict assign the fields
const updated_descriptor = {
...descriptor,
const update: PasteInfoUpdateParams = {
password: params.password ? sha256(params.password).slice(0, 16) : undefined,
max_access_n: params.max_access_n,
title: params.title,
mime_type: params.mime_type,
expired_at: params.expired_at ? params.expired_at : descriptor.expired_at,
expired_at: params.expired_at,
};
// Remove redundant fields
Object.keys(update).forEach(
(key) =>
update[key as keyof PasteInfoUpdateParams] === undefined && delete update[key as keyof PasteInfoUpdateParams]
);
const updated_descriptor: PasteIndexEntry = {
...descriptor,
...update,
};
ctx.waitUntil(env.PASTE_INDEX.put(uuid, JSON.stringify(updated_descriptor), { expirationTtl: 2419200 }));
@ -291,4 +301,9 @@ router.post('/upload', async (req, env, ctx) => {
return PasteAPIRepsonse.build(200, 'This endpoint is not ready.');
});
// Fallback route
router.all('*', async () => {
return PasteAPIRepsonse.build(403, 'Invalid endpoint.');
});
export default router;