-
-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guard
blitz generate
input against unwanted characters (#4024)
Co-authored-by: Dillon Raphael <[email protected]> Co-authored-by: Siddharth Suresh <[email protected]> Closes #4021
- Loading branch information
Showing
6 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@blitzjs/generator": patch | ||
--- | ||
|
||
Guard `blitz g` input via an allow-list of characters; throw if unwanted characters are found. Prevents to break the blitz command by accident (https://github.com/blitz-js/blitz/issues/4021). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import chalk from "chalk" | ||
import {log} from "./log" | ||
|
||
export const checkInputsOrRaise = (inputs: string[] | string) => { | ||
if (typeof inputs === "string") { | ||
checkInputOrRaise(inputs) | ||
} else { | ||
inputs.forEach((input) => checkInputOrRaise(input)) | ||
} | ||
} | ||
|
||
const checkInputOrRaise = (input: string) => { | ||
const regex = /^[a-zA-Z0-9-_:=\?[\]\s]+$/ | ||
if (!regex.test(input)) { | ||
const firstInvalidCharacter = input.match(/[^a-zA-Z0-9-_:=\?[\]\s]/) | ||
if (firstInvalidCharacter) { | ||
log.branded( | ||
"Blitz Generator Parser Error: " + | ||
chalk.red( | ||
`Input contains invalid character: "${firstInvalidCharacter[0]}" in ${firstInvalidCharacter.input} at position ${firstInvalidCharacter.index}`, | ||
), | ||
) | ||
} | ||
throw new Error( | ||
"Input should only contain alphanumeric characters, spaces, and the following characters: - _ : = ? [ ]", | ||
) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/generator/test/generators/utils/checkInputOrRaise.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {checkInputsOrRaise} from "../../../../generator/src/utils/checkInputOrRaise" | ||
import {describe, expect, it} from "vitest" | ||
|
||
describe("checkInputsOrRaise()", () => { | ||
describe("with input string", () => { | ||
it("does not raise when input string OK", () => { | ||
expect(() => checkInputsOrRaise("Foo:foo=bar[]?:-_ ")).not.toThrow() | ||
}) | ||
it("raisees when input has unwanted characters", () => { | ||
expect(() => checkInputsOrRaise("()!")).toThrowError( | ||
"Input should only contain alphanumeric characters, spaces, and the following characters: - _ : = ? [ ]", | ||
) | ||
}) | ||
}) | ||
|
||
describe("with input string[]", () => { | ||
it("does not raise when input strings OK", () => { | ||
expect(() => checkInputsOrRaise(["ok:ok=ok", "also ok:ok"])).not.toThrow() | ||
}) | ||
it("raisees when one input has unwanted characters", () => { | ||
expect(() => checkInputsOrRaise(["ok:ok=ok", "not ok!"])).toThrowError( | ||
"Input should only contain alphanumeric characters, spaces, and the following characters: - _ : = ? [ ]", | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters