feat: add support for base64url regex validator#1557
Conversation
Closes open-circle#947. Add a `base64Url(message?)` action that validates the base64url alphabet with optional padding (both padded `xx==` and unpadded `xx` forms, since base64url commonly omits padding). The existing `base64` action is unchanged.
WalkthroughAdds a Base64URL validation action with a strict regular expression, public issue and action types, and overloads supporting optional custom messages. The action validates typed dataset values, records an issue for invalid values, and returns the dataset. The new action is re-exported through its module index and the main actions barrel. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with 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.
Inline comments:
In `@library/src/actions/base64Url/base64Url.ts`:
- 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.
- Around line 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=”.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 092ddcc3-0a41-42e3-9157-df3a782d77e0
📒 Files selected for processing (3)
library/src/actions/base64Url/base64Url.tslibrary/src/actions/base64Url/index.tslibrary/src/actions/index.ts
| const BASE64URL_REGEX: RegExp = | ||
| /^(?:[\da-z_-]{4})*(?:[\da-z_-]{2}={0,2}|[\da-z_-]{3}={0,1})?$/iu; |
There was a problem hiding this comment.
🎯 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.
| 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=”.
| readonly message: TMessage; | ||
| } | ||
|
|
||
| export function base64Url<TInput extends string>(): Base64UrlAction< |
There was a problem hiding this comment.
📐 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.
| 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
There was a problem hiding this comment.
2 issues found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="library/src/actions/base64Url/base64Url.ts">
<violation number="1" location="library/src/actions/base64Url/base64Url.ts:9">
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.</violation>
<violation number="2" location="library/src/actions/base64Url/base64Url.ts:9">
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.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| import { _addIssue } from '../../utils/index.ts'; | ||
|
|
||
| const BASE64URL_REGEX: RegExp = | ||
| /^(?:[\da-z_-]{4})*(?:[\da-z_-]{2}={0,2}|[\da-z_-]{3}={0,1})?$/iu; |
There was a problem hiding this comment.
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>
| import { _addIssue } from '../../utils/index.ts'; | ||
|
|
||
| const BASE64URL_REGEX: RegExp = | ||
| /^(?:[\da-z_-]{4})*(?:[\da-z_-]{2}={0,2}|[\da-z_-]{3}={0,1})?$/iu; |
There was a problem hiding this comment.
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>
Closes #947.
Add a
base64Url(message?)action that validates the base64url alphabet with optional padding (both paddedxx==and unpaddedxxforms, since base64url commonly omits padding). The existingbase64action is unchanged.Summary by CodeRabbit