Skip to content

Commit

Permalink
feat: limit logs timestamp_format length
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragory committed Nov 25, 2023
1 parent c82e147 commit fafaefa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 3 additions & 2 deletions backend/src/plugins/Logs/LogsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ import { logVoiceChannelForceMove } from "./logFunctions/logVoiceChannelForceMov
import { logVoiceChannelJoin } from "./logFunctions/logVoiceChannelJoin";
import { logVoiceChannelLeave } from "./logFunctions/logVoiceChannelLeave";
import { logVoiceChannelMove } from "./logFunctions/logVoiceChannelMove";
import { asBoundedString } from "../../utils/iotsUtils";

// The `any` cast here is to prevent TypeScript from locking up from the circular dependency
function getCasesPlugin(): Promise<any> {
Expand All @@ -120,12 +121,12 @@ const defaultOptions: PluginOptions<LogsPluginType> = {
config: {
channels: {},
format: {
timestamp: FORMAT_NO_TIMESTAMP, // Legacy/deprecated, use timestamp_format below instead
timestamp: asBoundedString(FORMAT_NO_TIMESTAMP), // Legacy/deprecated, use timestamp_format below instead
...DefaultLogMessages,
},
ping_user: true, // Legacy/deprecated, if below is false mentions wont actually ping. In case you really want the old behavior, set below to true
allow_user_mentions: false,
timestamp_format: "[<t:]X[>]",
timestamp_format: asBoundedString("[<t:]X[>]"),
include_embed_timestamp: true,
},

Expand Down
5 changes: 3 additions & 2 deletions backend/src/plugins/Logs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
TemplateSafeUser,
} from "../../utils/templateSafeObjects";
import { TRegex } from "../../validatorUtils";
import { tBoundedString } from "../../utils/iotsUtils";

export const tLogFormats = t.record(t.string, t.union([t.string, tMessageContent]));
export type TLogFormats = t.TypeOf<typeof tLogFormats>;
Expand Down Expand Up @@ -53,12 +54,12 @@ export const ConfigSchema = t.type({
format: t.intersection([
tLogFormats,
t.type({
timestamp: t.string, // Legacy/deprecated
timestamp: tBoundedString(0, 64), // Legacy/deprecated
}),
]),
ping_user: t.boolean, // Legacy/deprecated, if below is false mentions wont actually ping
allow_user_mentions: t.boolean,
timestamp_format: t.string,
timestamp_format: tBoundedString(0, 64),
include_embed_timestamp: t.boolean,
});
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
Expand Down
17 changes: 17 additions & 0 deletions backend/src/utils/iotsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as t from "io-ts";

interface BoundedStringBrand {
readonly BoundedString: unique symbol;
}

export function asBoundedString(str: string) {
return str as t.Branded<string, BoundedStringBrand>;
}

export function tBoundedString(min: number, max: number) {
return t.brand(
t.string,
(str): str is t.Branded<string, BoundedStringBrand> => (str.length >= min && str.length <= max),
"BoundedString",
);
}

0 comments on commit fafaefa

Please sign in to comment.