Skip to content
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

APIv4 - Throw exception instead of munging illegal join aliases #21072

Merged
merged 1 commit into from
Aug 10, 2021
Merged
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
5 changes: 4 additions & 1 deletion Civi/Api4/Query/Api4SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,10 @@ private function addExplicitJoins() {
continue;
}
// Ensure alias is a safe string, and supply default if not given
$alias = $alias ? \CRM_Utils_String::munge($alias, '_', 256) : strtolower($entity);
$alias = $alias ?: strtolower($entity);
if ($alias === self::MAIN_TABLE_ALIAS || !preg_match('/^[-\w]{1,256}$/', $alias)) {
throw new \API_Exception('Illegal join alias: "' . $alias . '"');
}
// First item in the array is a boolean indicating if the join is required (aka INNER or LEFT).
// The rest are join conditions.
$side = array_shift($join);
Expand Down
34 changes: 34 additions & 0 deletions tests/phpunit/api/v4/Action/FkJoinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,40 @@ public function testExcludeJoin() {
$this->assertNotContains($this->getReference('test_contact_1')['id'], $contacts);
}

public function testInvalidJoinAlias() {
// Not allowed to use same alias as the base table
try {
Contact::get(FALSE)->addJoin('Address AS a')->execute();
}
catch (\API_Exception $e) {
$message = $e->getMessage();
}
$this->assertEquals('Illegal join alias: "a"', $message);

// Not allowed to use dots in the alias
try {
Contact::get(FALSE)->addJoin('Address AS add.ress')->execute();
}
catch (\API_Exception $e) {
$message = $e->getMessage();
}
$this->assertEquals('Illegal join alias: "add.ress"', $message);
Copy link
Contributor

Choose a reason for hiding this comment

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

@colemanw - this makes sense but for test legibility could you add an OK alias - that would make it clearer when reading the test IMHO


// Not allowed to use an alias > 256 characters
try {
$longAlias = str_repeat('z', 257);
Contact::get(FALSE)->addJoin("Address AS $longAlias")->execute();
}
catch (\API_Exception $e) {
$message = $e->getMessage();
}
$this->assertEquals("Illegal join alias: \"$longAlias\"", $message);

// Alpha-numeric with dashes 256 characters long - weird but allowed
$okAlias = str_repeat('-0_a-9Z_', 32);
Contact::get(FALSE)->addJoin("Address AS $okAlias")->execute();
}

public function testJoinToTheSameTableTwice() {
$cid1 = Contact::create(FALSE)
->addValue('first_name', 'Aaa')
Expand Down