fix: handle breaking changes from dep update

This commit is contained in:
shiftinv 2024-02-01 17:14:59 +01:00
parent 3827c7f9c9
commit 7e4a78fcf2
4 changed files with 19 additions and 19 deletions

View File

@ -13,7 +13,7 @@ async function getKey(): Promise<CryptoKey> {
_signKey = await crypto.subtle.importKey(
"raw",
hex.decode(new TextEncoder().encode(config.signKey)),
hex.decodeHex(config.signKey),
{ name: "HMAC", hash: "SHA-256" },
false,
["sign", "verify"],
@ -26,12 +26,12 @@ export async function sign(input: string): Promise<string> {
const key = await getKey();
const inputData = encoder.encode(input);
const sig = await crypto.subtle.sign("HMAC", key, inputData);
return new TextDecoder().decode(hex.encode(new Uint8Array(sig)));
return hex.encodeHex(sig);
}
export async function verify(input: string, signature: string): Promise<boolean> {
const key = await getKey();
const signatureData = hex.decode(encoder.encode(signature));
const inputData = encoder.encode(input);
return await crypto.subtle.verify("HMAC", key, signatureData, inputData);
const sig = hex.decodeHex(signature);
return await crypto.subtle.verify("HMAC", key, sig, inputData);
}

View File

@ -1,4 +1,4 @@
import { http } from "../deps.ts";
import { httpErrors } from "../deps.ts";
import config from "./config.ts";
import { hasKey, verify } from "./crypto.ts";
import filterWebhook from "./filter.ts";
@ -19,20 +19,20 @@ export default async function handle(
// everything else should be a POST
if (req.method !== "POST") {
throw http.createHttpError(405);
throw httpErrors.createError(405);
}
// split url into parts
const [, id, token] = url.pathname.split("/");
if (!id || !token) {
throw http.createHttpError(400);
throw httpErrors.createError(400);
}
// verify signature
if (hasKey) {
const signature = url.searchParams.get("sig");
if (!signature) throw http.createHttpError(400);
if (!(await verify(`${id}/${token}`, signature))) throw http.createHttpError(403);
if (!signature) throw httpErrors.createError(400);
if (!(await verify(`${id}/${token}`, signature))) throw httpErrors.createError(403);
}
// extract data
@ -70,7 +70,7 @@ function getUrlConfig(params: URLSearchParams): UrlConfig {
config.commentBurstLimit = parseInt(value);
break;
default:
throw http.createHttpError(418, `Unknown config option: ${key}`);
throw httpErrors.createError(418, `Unknown config option: ${key}`);
}
}
return config;

View File

@ -1,4 +1,4 @@
import { Lock, log, redis, TTLCache } from "../deps.ts";
import { log, Mutex, redis, TTLCache } from "../deps.ts";
import config from "./config.ts";
const KEY_EXPIRY = 3; // seconds
@ -22,12 +22,12 @@ class LocalCommentManager implements CommentManager {
class RedisCommentManager implements CommentManager {
private connectOptions: redis.RedisConnectOptions;
private lock: Lock;
private lock: Mutex;
private _redis: Promise<redis.Redis> | undefined;
constructor(options: redis.RedisConnectOptions) {
this.connectOptions = options;
this.lock = new Lock();
this.lock = new Mutex();
}
// manual lazy init, redis.createLazyClient currently doesn't support pipelines :/

12
main.ts
View File

@ -1,12 +1,12 @@
import { http, log } from "./deps.ts";
import { http, httpErrors, log } from "./deps.ts";
import config from "./lib/config.ts";
import handler from "./lib/handler.ts";
import { requestLog } from "./lib/util.ts";
async function setupLogs(): Promise<void> {
await log.setup({
function setupLogs() {
log.setup({
handlers: {
console: new log.handlers.ConsoleHandler("DEBUG", {
console: new log.ConsoleHandler("DEBUG", {
formatter: (rec) => `${rec.datetime.toISOString()} [${rec.levelName}] ${rec.msg}`,
}),
},
@ -32,7 +32,7 @@ async function handleRequest(req: Request, connInfo: http.ConnInfo): Promise<Res
[res, meta] = webhookResult;
}
} catch (err) {
if (http.isHttpError(err) && err.expose) {
if (err instanceof httpErrors.HttpError && err.expose) {
reqLog.warning(`http error: ${err.message}`);
res = new Response(err.message, { status: err.status });
} else {
@ -69,7 +69,7 @@ async function handleRequest(req: Request, connInfo: http.ConnInfo): Promise<Res
}
if (import.meta.main) {
await setupLogs();
setupLogs();
if (!config.signKey) {
log.warning("url signing disabled");