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

Escape single-quote characters in partition and row keys #21232

Merged
merged 5 commits into from
Jul 20, 2023
Merged
Changes from 1 commit
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
Next Next commit
Escapse single-quote characters in partition and row keys
jhendrixMSFT committed Jul 20, 2023

Verified

This commit was signed with the committer’s verified signature.
commit 5748b43c5e1e184b86a98d37b6ca2f26a7c75a40
1 change: 1 addition & 0 deletions sdk/data/aztables/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
### Breaking Changes

### Bugs Fixed
* Escape single-quote characters in partition and row keys.

### Other Changes

2 changes: 1 addition & 1 deletion sdk/data/aztables/assets.json
Original file line number Diff line number Diff line change
@@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "go",
"TagPrefix": "go/data/aztables",
"Tag": "go/data/aztables_abcca51964"
"Tag": "go/data/aztables_d831c04dd4"
}
20 changes: 10 additions & 10 deletions sdk/data/aztables/client.go
Original file line number Diff line number Diff line change
@@ -298,7 +298,7 @@ func (t *Client) GetEntity(ctx context.Context, partitionKey string, rowKey stri
}

genOptions, queryOptions := options.toGenerated()
resp, err := t.client.QueryEntityWithPartitionAndRowKey(ctx, generated.Enum1Three0, t.name, partitionKey, rowKey, genOptions, queryOptions)
resp, err := t.client.QueryEntityWithPartitionAndRowKey(ctx, generated.Enum1Three0, t.name, prepareKey(partitionKey), prepareKey(rowKey), genOptions, queryOptions)
if err != nil {
return GetEntityResponse{}, err
}
@@ -375,7 +375,7 @@ func (t *Client) DeleteEntity(ctx context.Context, partitionKey string, rowKey s
nilEtag := azcore.ETag("*")
options.IfMatch = &nilEtag
}
resp, err := t.client.DeleteEntity(ctx, generated.Enum1Three0, t.name, partitionKey, rowKey, string(*options.IfMatch), options.toGenerated(), &generated.QueryOptions{})
resp, err := t.client.DeleteEntity(ctx, generated.Enum1Three0, t.name, prepareKey(partitionKey), prepareKey(rowKey), string(*options.IfMatch), options.toGenerated(), &generated.QueryOptions{})
if err != nil {
return DeleteEntityResponse{}, err
}
@@ -477,8 +477,8 @@ func (t *Client) UpdateEntity(ctx context.Context, entity []byte, options *Updat
ctx,
generated.Enum1Three0,
t.name,
partKey,
rowkey,
prepareKey(partKey),
prepareKey(rowkey),
options.toGeneratedMergeEntity(mapEntity),
&generated.QueryOptions{},
)
@@ -491,8 +491,8 @@ func (t *Client) UpdateEntity(ctx context.Context, entity []byte, options *Updat
ctx,
generated.Enum1Three0,
t.name,
partKey,
rowkey,
prepareKey(partKey),
prepareKey(rowkey),
options.toGeneratedUpdateEntity(mapEntity),
&generated.QueryOptions{},
)
@@ -580,8 +580,8 @@ func (t *Client) UpsertEntity(ctx context.Context, entity []byte, options *Upser
ctx,
generated.Enum1Three0,
t.name,
partKey,
rowkey,
prepareKey(partKey),
prepareKey(rowkey),
&generated.TableClientMergeEntityOptions{TableEntityProperties: mapEntity},
&generated.QueryOptions{},
)
@@ -594,8 +594,8 @@ func (t *Client) UpsertEntity(ctx context.Context, entity []byte, options *Upser
ctx,
generated.Enum1Three0,
t.name,
partKey,
rowkey,
prepareKey(partKey),
prepareKey(rowkey),
&generated.TableClientUpdateEntityOptions{TableEntityProperties: mapEntity},
&generated.QueryOptions{},
)
2 changes: 1 addition & 1 deletion sdk/data/aztables/client_test.go
Original file line number Diff line number Diff line change
@@ -216,7 +216,7 @@ func TestInsertEntity(t *testing.T) {
defer delete()

// 1. Create Basic Entity
entityToCreate := createSimpleEntity(1, "partition")
entityToCreate := createSimpleEntityWithRowKey(1, "parti'tion", "one'")
marshalled, err := json.Marshal(entityToCreate)
require.NoError(t, err)

2 changes: 1 addition & 1 deletion sdk/data/aztables/entity.go
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ type EDMEntity struct {
// MarshalJSON implements the json.Marshal method
func (e EDMEntity) MarshalJSON() ([]byte, error) {
entity := map[string]interface{}{}
entity["PartitionKey"], entity["RowKey"] = e.PartitionKey, e.RowKey
entity["PartitionKey"], entity["RowKey"] = prepareKey(e.PartitionKey), prepareKey(e.RowKey)

for propName, propValue := range e.Properties {
entity[propName] = propValue
6 changes: 6 additions & 0 deletions sdk/data/aztables/models.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
package aztables

import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
@@ -349,3 +350,8 @@ func toGeneratedStatusType(g *generated.GeoReplicationStatusType) *GeoReplicatio
}
return nil
}

func prepareKey(key string) string {
// escape any single-quotes
return strings.ReplaceAll(key, "'", "''")
}
6 changes: 5 additions & 1 deletion sdk/data/aztables/zt_table_recorded_tests.go
Original file line number Diff line number Diff line change
@@ -67,10 +67,14 @@ type complexTestEntity struct {
}

func createSimpleEntity(count int, pk string) basicTestEntity {
return createSimpleEntityWithRowKey(count, fmt.Sprint(count), pk)
}

func createSimpleEntityWithRowKey(count int, pk string, rk string) basicTestEntity {
return basicTestEntity{
Entity: Entity{
PartitionKey: pk,
RowKey: fmt.Sprint(count),
RowKey: rk,
},
String: fmt.Sprintf("some string %d", count),
Integer: int32(count),