Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions library/src/actions/base64Url/base64Url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type {
BaseIssue,
BaseValidation,
ErrorMessage,
} from '../../types/index.ts';
import { _addIssue } from '../../utils/index.ts';

const BASE64URL_REGEX: RegExp =
/^(?:[\da-z_-]{4})*(?:[\da-z_-]{2}={0,2}|[\da-z_-]{3}={0,1})?$/iu;
Comment on lines +8 to +9

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

Fix regex to reject incorrectly padded strings.

The ={0,2} quantifier allows a single padding character (=) for a 2-character base64 block, which mathematically represents an invalid encoding (e.g., ab=). A 2-character block must have exactly two padding characters (==) or none. Use (?:==)? and =? to enforce correct padding lengths.

🐛 Proposed fix for the regex
-const BASE64URL_REGEX: RegExp =
-  /^(?:[\da-z_-]{4})*(?:[\da-z_-]{2}={0,2}|[\da-z_-]{3}={0,1})?$/iu;
+const BASE64URL_REGEX: RegExp =
+  /^(?:[\da-z_-]{4})*(?:[\da-z_-]{2}(?:==)?|[\da-z_-]{3}=?)?$/iu;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const BASE64URL_REGEX: RegExp =
/^(?:[\da-z_-]{4})*(?:[\da-z_-]{2}={0,2}|[\da-z_-]{3}={0,1})?$/iu;
const BASE64URL_REGEX: RegExp =
/^(?:[\da-z_-]{4})*(?:[\da-z_-]{2}(?:==)?|[\da-z_-]{3}=?)?$/iu;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@library/src/actions/base64Url/base64Url.ts` around lines 8 - 9, Update
BASE64URL_REGEX to enforce valid Base64URL padding: require exactly two padding
characters or none for a 2-character final block, and allow at most one optional
padding character for a 3-character block. Preserve acceptance of unpadded valid
strings while rejecting incorrectly padded values such as “ab=”.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: The regex accepts invalid padding xx= (2 data chars with exactly 1 =), which is neither properly padded (xx==) nor unpadded (xx). For example, "aA=" would incorrectly validate. Change ={0,2} to (?:==)? to only allow exactly 0 or 2 padding chars, matching the base64url specification.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At library/src/actions/base64Url/base64Url.ts, line 9:

<comment>The regex accepts invalid padding `xx=` (2 data chars with exactly 1 `=`), which is neither properly padded (`xx==`) nor unpadded (`xx`). For example, `"aA="` would incorrectly validate. Change `={0,2}` to `(?:==)?` to only allow exactly 0 or 2 padding chars, matching the base64url specification.</comment>

<file context>
@@ -0,0 +1,60 @@
+import { _addIssue } from '../../utils/index.ts';
+
+const BASE64URL_REGEX: RegExp =
+  /^(?:[\da-z_-]{4})*(?:[\da-z_-]{2}={0,2}|[\da-z_-]{3}={0,1})?$/iu;
+
+export interface Base64UrlIssue<TInput extends string>
</file context>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Malformed padding and non-ASCII characters pass validation: Zg= is accepted, and Unicode case-folded letters match under /iu. Require either no padding or exact RFC padding, and use an explicit ASCII case range.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At library/src/actions/base64Url/base64Url.ts, line 9:

<comment>Malformed padding and non-ASCII characters pass validation: `Zg=` is accepted, and Unicode case-folded letters match under `/iu`. Require either no padding or exact RFC padding, and use an explicit ASCII case range.</comment>

<file context>
@@ -0,0 +1,60 @@
+import { _addIssue } from '../../utils/index.ts';
+
+const BASE64URL_REGEX: RegExp =
+  /^(?:[\da-z_-]{4})*(?:[\da-z_-]{2}={0,2}|[\da-z_-]{3}={0,1})?$/iu;
+
+export interface Base64UrlIssue<TInput extends string>
</file context>


export interface Base64UrlIssue<TInput extends string>
extends BaseIssue<TInput> {
readonly kind: 'validation';
readonly type: 'base64url';
readonly expected: null;
readonly received: `"${string}"`;
readonly requirement: RegExp;
}

export interface Base64UrlAction<
TInput extends string,
TMessage extends ErrorMessage<Base64UrlIssue<TInput>> | undefined,
> extends BaseValidation<TInput, TInput, Base64UrlIssue<TInput>> {
readonly type: 'base64url';
readonly reference: typeof base64Url;
readonly expects: null;
readonly requirement: RegExp;
readonly message: TMessage;
}

export function base64Url<TInput extends string>(): Base64UrlAction<

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Add missing JSDoc to the exported function.

As per coding guidelines, JSDoc required on exported functions (first overload only for overload sets).

📝 Proposed fix to add JSDoc
+/**
+ * Creates a base64url validation action.
+ *
+ * `@returns` A base64url action.
+ */
 export function base64Url<TInput extends string>(): Base64UrlAction<
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function base64Url<TInput extends string>(): Base64UrlAction<
/**
* Creates a base64url validation action.
*
* `@returns` A base64url action.
*/
export function base64Url<TInput extends string>(): Base64UrlAction<
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@library/src/actions/base64Url/base64Url.ts` at line 31, Add JSDoc immediately
before the first exported overload, base64Url<TInput extends string>(),
documenting the function’s purpose and usage according to the project’s existing
action documentation conventions; do not duplicate documentation on subsequent
overloads.

Source: Coding guidelines

TInput,
undefined
>;

export function base64Url<
TInput extends string,
const TMessage extends ErrorMessage<Base64UrlIssue<TInput>> | undefined,
>(message: TMessage): Base64UrlAction<TInput, TMessage>;

// @__NO_SIDE_EFFECTS__
export function base64Url(
message?: ErrorMessage<Base64UrlIssue<string>>,
): Base64UrlAction<string, ErrorMessage<Base64UrlIssue<string>> | undefined> {
return {
kind: 'validation',
type: 'base64url',
reference: base64Url,
async: false,
expects: null,
requirement: BASE64URL_REGEX,
message,
'~run'(dataset, config) {
if (dataset.typed && !this.requirement.test(dataset.value)) {
_addIssue(this, 'base64url', dataset, config);
}
return dataset;
},
};
}
1 change: 1 addition & 0 deletions library/src/actions/base64Url/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './base64Url.ts';
1 change: 1 addition & 0 deletions library/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './args/index.ts';
export * from './await/index.ts';
export * from './base64/index.ts';
export * from './base64Url/index.ts';
export * from './bic/index.ts';
export * from './brand/index.ts';
export * from './bytes/index.ts';
Expand Down