-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add base64 command to encode/decode base64 strings #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e2606b6
f53fe6b
e143b33
edd9174
97d3042
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,91 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { EmbedBuilder } from "discord.js"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { CommandCallbackOpts } from "../../types/command.ts"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "base64", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: "Encode or decode Base64 strings.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usage: "base64 <encode|decode> <text>", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| aliases: ["b64"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| encode: async ({ message, args }: CommandCallbackOpts) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const text = args.join(" "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!text) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const embed = new EmbedBuilder() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setTitle("❌ Missing Input") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setDescription("Please provide some text to encode.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setColor(0xe74c3c); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return message.reply({ embeds: [embed] }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const encoded = Buffer.from(text, "utf8").toString("base64"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The encode operation lacks input size validation, which could cause memory exhaustion if an attacker provides an extremely large text string. This could lead to service disruption or denial of service. Confidence: 5/5 Suggested FixAdd input size validation before attempting to encode:
Suggested change
Add size validation before the encode operation to prevent memory exhaustion. Use Prompt for AICopy this prompt to your AI IDE to fix this issue locally:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The encoding operation lacks input size validation before creating a Buffer. A malicious user could provide extremely large text input (e.g., millions of characters), causing excessive memory allocation and potentially crashing the bot or causing service disruption. Confidence: 5/5 Suggested FixAdd input size validation before encoding to prevent memory exhaustion attacks:
Suggested change
Add size validation immediately after checking if text exists to prevent memory exhaustion from oversized inputs. Prompt for AICopy this prompt to your AI IDE to fix this issue locally: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const embed = new EmbedBuilder() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setTitle("🔐 Base64 Encode") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .addFields( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "Original Text", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value: text.length > 1024 ? text.slice(0, 1021) + "..." : text, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "Encoded (Base64)", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| encoded.length > 1024 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? "```" + encoded.slice(0, 1021) + "...```" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : "```" + encoded + "```", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setColor(0x2ecc71); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await message.reply({ embeds: [embed] }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| decode: async ({ message, args }: CommandCallbackOpts) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const input = args.join(" "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!input) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const embed = new EmbedBuilder() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setTitle("❌ Missing Input") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setDescription("Please provide a Base64 string to decode.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setColor(0xe74c3c); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return message.reply({ embeds: [embed] }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const decoded = Buffer.from(input, "base64").toString("utf8"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The decode operation lacks input size validation, which could cause memory exhaustion if an attacker provides an extremely large Base64 string. This could lead to service disruption or denial of service. Confidence: 5/5 Suggested FixAdd input size validation before attempting to decode. Limit the input to a reasonable size (e.g., 10MB when decoded):
Suggested change
Add size validation before the decode operation to prevent memory exhaustion attacks. The estimated decoded size is calculated as Prompt for AICopy this prompt to your AI IDE to fix this issue locally:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The decode operation lacks input size validation before creating a Buffer. A malicious user could provide an extremely large Base64 string, causing excessive memory allocation during decoding and potentially crashing the bot or causing service disruption. Confidence: 5/5 Suggested FixAdd input size validation before decoding to prevent memory exhaustion attacks:
Suggested change
Add size validation immediately after checking if input exists to prevent memory exhaustion from oversized Base64 strings. Prompt for AICopy this prompt to your AI IDE to fix this issue locally: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!decoded || decoded.includes("\u0000")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error("Invalid Base64"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+54
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing input length validation before Base64 decoding creates a potential Denial of Service (DoS) vulnerability. An attacker could provide an extremely long Base64 string that decodes to a massive UTF-8 output, causing memory exhaustion. Additionally, the validation logic at line 57 is insufficient - it only checks for null bytes but doesn't validate maximum decoded length or other problematic content. Confidence: 5/5 Suggested Fix
Suggested change
Add input validation before decoding to prevent DoS attacks. Validate both the input Base64 string length (before decoding) and the decoded output length (after decoding). The limits should be adjusted based on your application's requirements, but should prevent processing of maliciously large inputs. Prompt for AICopy this prompt to your AI IDE to fix this issue locally: 📍 This suggestion applies to lines 54-59 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const embed = new EmbedBuilder() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setTitle("🔓 Base64 Decode") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .addFields( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "Base64 Input", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input.length > 1024 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? "```" + input.slice(0, 1021) + "...```" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : "```" + input + "```", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "Decoded Text", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| decoded.length > 1024 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? decoded.slice(0, 1021) + "..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : decoded, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setColor(0x3498db); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await message.reply({ embeds: [embed] }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const embed = new EmbedBuilder() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setTitle("❌ Invalid Base64") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setDescription("That doesn't look like valid Base64.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .setColor(0xe74c3c); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await message.reply({ embeds: [embed] }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
uservariable is used with a fallback to empty string (user || ""), butuseris extracted from regex match at line 50 and should always be defined at this point. However, ifuseris somehow undefined, passing an empty string togenerateGitHubHeatMap("")will cause the function to make an API call with an invalid username, potentially causing errors or unexpected behavior. This indicates a logic error in the flow.Confidence: 5/5
Suggested Fix
Remove the
|| ""fallback sinceuseris guaranteed to be defined at this point (extracted from regex match at line 50). If there's concern about undefined values, the proper fix is to add validation earlier in the flow, not to pass empty strings to API functions.Prompt for AI
Copy this prompt to your AI IDE to fix this issue locally: