Skip to content

Commit

Permalink
fix: URI encode user-inputted string
Browse files Browse the repository at this point in the history
  • Loading branch information
phiHero committed Aug 27, 2024
1 parent 9f33382 commit 1491153
Show file tree
Hide file tree
Showing 18 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Alias
*/
public function __construct(string $name, ApiCall $apiCall)
{
$this->name = $name;
$this->name = encodeURIComponent($name);
$this->apiCall = $apiCall;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(ApiCall $apiCall)
*/
public function endPointPath(string $aliasName): string
{
return sprintf('%s/%s', static::RESOURCE_PATH, $aliasName);
return sprintf('%s/%s', static::RESOURCE_PATH, encodeURIComponent($aliasName));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyticsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AnalyticsRule

public function __construct(string $ruleName, ApiCall $apiCall)
{
$this->ruleName = $ruleName;
$this->ruleName = encodeURIComponent($ruleName);
$this->apiCall = $apiCall;
}

Expand Down
2 changes: 1 addition & 1 deletion src/AnalyticsRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public function retrieve()

private function endpoint_path($operation = null)
{
return self::RESOURCE_PATH . ($operation === null ? '' : "/$operation");
return self::RESOURCE_PATH . ($operation === null ? '' : "/" . encodeURIComponent($operation));
}
}
1 change: 1 addition & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Typesense\Exceptions\ConfigError;
use Typesense\Lib\Configuration;

include('utils/utils.php');
/**
* Class Client
*
Expand Down
9 changes: 5 additions & 4 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ class Collection
*/
public function __construct(string $name, ApiCall $apiCall)
{
$this->name = $name;
$encodedName = encodeURIComponent($name);
$this->name = $encodedName;
$this->apiCall = $apiCall;
$this->documents = new Documents($name, $this->apiCall);
$this->overrides = new Overrides($name, $this->apiCall);
$this->synonyms = new Synonyms($name, $this->apiCall);
$this->documents = new Documents($encodedName, $this->apiCall);
$this->overrides = new Overrides($encodedName, $this->apiCall);
$this->synonyms = new Synonyms($encodedName, $this->apiCall);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Conversation
*/
public function __construct(string $id, ApiCall $apiCall)
{
$this->id = $id;
$this->id = encodeURIComponent($id);
$this->apiCall = $apiCall;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ConversationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ConversationModel
*/
public function __construct(string $id, ApiCall $apiCall)
{
$this->id = $id;
$this->id = encodeURIComponent($id);
$this->apiCall = $apiCall;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class Document
*/
public function __construct(string $collectionName, string $documentId, ApiCall $apiCall)
{
$this->collectionName = $collectionName;
$this->documentId = $documentId;
$this->collectionName = $collectionName; // already URI encoded in Collection construct
$this->documentId = encodeURIComponent($documentId);
$this->apiCall = $apiCall;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Documents implements \ArrayAccess
*/
public function __construct(string $collectionName, ApiCall $apiCall)
{
$this->collectionName = $collectionName;
$this->collectionName = $collectionName; // already URI encoded in Collection construct
$this->apiCall = $apiCall;
}

Expand Down Expand Up @@ -125,7 +125,7 @@ public function createMany(array $documents, array $options = []): array
{
$this->apiCall->getLogger()->warning(
"createMany is deprecated and will be removed in a future version. " .
"Use import instead, which now takes both an array of documents or a JSONL string of documents"
"Use import instead, which now takes both an array of documents or a JSONL string of documents"
);
return $this->import($documents, $options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Key
*/
public function __construct(string $keyId, ApiCall $apiCall)
{
$this->keyId = $keyId;
$this->keyId = encodeURIComponent($keyId);
$this->apiCall = $apiCall;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Override.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class Override
*/
public function __construct(string $collectionName, string $overrideId, ApiCall $apiCall)
{
$this->collectionName = $collectionName;
$this->overrideId = $overrideId;
$this->collectionName = $collectionName; // already URI encoded in Collection construct
$this->overrideId = encodeURIComponent($overrideId);
$this->apiCall = $apiCall;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Overrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Http\Client\Exception as HttpClientException;
use Typesense\Exceptions\TypesenseClientError;



/**
* Class Overrides
*
Expand Down Expand Up @@ -39,7 +41,7 @@ class Overrides implements \ArrayAccess
*/
public function __construct(string $collectionName, ApiCall $apiCall)
{
$this->collectionName = $collectionName;
$this->collectionName = $collectionName; // already URI encoded in Collection construct
$this->apiCall = $apiCall;
}

Expand All @@ -55,7 +57,7 @@ public function endPointPath(string $overrideId = ''): string
Collections::RESOURCE_PATH,
$this->collectionName,
static::RESOURCE_PATH,
$overrideId
encodeURIComponent($overrideId)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Presets.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private function endpointPath($presetsName)
return sprintf(
'%s/%s',
static::PRESETS_PATH,
$presetsName
encodeURIComponent($presetsName)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Stopwords.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function endpointPath($stopwordsName)
return sprintf(
'%s/%s',
static::STOPWORDS_PATH,
$stopwordsName
encodeURIComponent($stopwordsName)
);
}
}
4 changes: 2 additions & 2 deletions src/Synonym.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class Synonym
*/
public function __construct(string $collectionName, string $synonymId, ApiCall $apiCall)
{
$this->collectionName = $collectionName;
$this->synonymId = $synonymId;
$this->collectionName = $collectionName; // already URI encoded in Collection construct
$this->synonymId = encodeURIComponent($synonymId);
$this->apiCall = $apiCall;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Synonyms.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Synonyms implements \ArrayAccess
*/
public function __construct(string $collectionName, ApiCall $apiCall)
{
$this->collectionName = $collectionName;
$this->collectionName = $collectionName; // already URI encoded in Collection construct
$this->apiCall = $apiCall;
}

Expand All @@ -53,7 +53,7 @@ public function endPointPath(string $synonymId = ''): string
Collections::RESOURCE_PATH,
$this->collectionName,
static::RESOURCE_PATH,
$synonymId
encodeURIComponent($synonymId)
);
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// https://stackoverflow.com/a/1734255
function encodeURIComponent($str)
{
$revert = array('%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')');
return strtr(rawurlencode($str), $revert);
}

0 comments on commit 1491153

Please sign in to comment.