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

Bugfix: UPN single quote escaping #643

Merged
merged 3 commits into from
Nov 11, 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: 3 additions & 2 deletions internal/services/users/user_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/hashicorp/terraform-provider-azuread/internal/clients"
"github.com/hashicorp/terraform-provider-azuread/internal/tf"
"github.com/hashicorp/terraform-provider-azuread/internal/utils"
"github.com/hashicorp/terraform-provider-azuread/internal/validate"
)

Expand Down Expand Up @@ -316,7 +317,7 @@ func userDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interf

if upn, ok := d.Get("user_principal_name").(string); ok && upn != "" {
query := odata.Query{
Filter: fmt.Sprintf("userPrincipalName eq '%s'", upn),
Filter: fmt.Sprintf("userPrincipalName eq '%s'", utils.EscapeSingleQuote(upn)),
}
users, _, err := client.List(ctx, query)
if err != nil {
Expand Down Expand Up @@ -346,7 +347,7 @@ func userDataSourceRead(ctx context.Context, d *schema.ResourceData, meta interf
user = *u
} else if mailNickname, ok := d.Get("mail_nickname").(string); ok && mailNickname != "" {
query := odata.Query{
Filter: fmt.Sprintf("mailNickname eq '%s'", mailNickname),
Filter: fmt.Sprintf("mailNickname eq '%s'", utils.EscapeSingleQuote(mailNickname)),
}
users, _, err := client.List(ctx, query)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/services/users/user_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ data "azuread_domains" "test" {
}
resource "azuread_user" "test" {
user_principal_name = "acctestUser.%[1]d@${data.azuread_domains.test.domains.0.domain_name}"
user_principal_name = "acctestUser'%[1]d@${data.azuread_domains.test.domains.0.domain_name}"
display_name = "acctestUser-%[1]d"
password = "%[2]s"
}
Expand All @@ -171,7 +171,7 @@ resource "azuread_user" "manager" {
}
resource "azuread_user" "test" {
user_principal_name = "acctestUser.%[1]d@${data.azuread_domains.test.domains.0.domain_name}"
user_principal_name = "acctestUser'%[1]d@${data.azuread_domains.test.domains.0.domain_name}"
mail = "acctestUser.%[1][email protected]"
mail_nickname = "acctestUser-%[1]d-MailNickname"
other_mails = ["acctestUser.%[1][email protected]", "acctestUser.%[1][email protected]"]
Expand Down Expand Up @@ -223,7 +223,7 @@ data "azuread_domains" "test" {
}
resource "azuread_user" "testA" {
user_principal_name = "acctestUser.%[1]d.A@${data.azuread_domains.test.domains.0.domain_name}"
user_principal_name = "acctestUser'%[1]d.A@${data.azuread_domains.test.domains.0.domain_name}"
display_name = "acctestUser-%[1]d-A"
password = "%[2]s"
}
Expand Down
5 changes: 3 additions & 2 deletions internal/services/users/users_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/hashicorp/terraform-provider-azuread/internal/clients"
"github.com/hashicorp/terraform-provider-azuread/internal/tf"
"github.com/hashicorp/terraform-provider-azuread/internal/utils"
"github.com/hashicorp/terraform-provider-azuread/internal/validate"
)

Expand Down Expand Up @@ -179,7 +180,7 @@ func usersDataSourceRead(ctx context.Context, d *schema.ResourceData, meta inter
expectedCount = len(upns)
for _, v := range upns {
query := odata.Query{
Filter: fmt.Sprintf("userPrincipalName eq '%s'", v),
Filter: fmt.Sprintf("userPrincipalName eq '%s'", utils.EscapeSingleQuote(v.(string))),
}
result, _, err := client.List(ctx, query)
if err != nil {
Expand Down Expand Up @@ -222,7 +223,7 @@ func usersDataSourceRead(ctx context.Context, d *schema.ResourceData, meta inter
expectedCount = len(mailNicknames)
for _, v := range mailNicknames {
query := odata.Query{
Filter: fmt.Sprintf("mailNickname eq '%s'", v),
Filter: fmt.Sprintf("mailNickname eq '%s'", utils.EscapeSingleQuote(v.(string))),
}
result, _, err := client.List(ctx, query)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions internal/utils/odata_query_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

import "strings"

// EscapeSingleQuote replaces all occurrences of single quote, with 2 single quotes.
// For requests that use single quotes, if any parameter values also contain single quotes,
// those must be double escaped; otherwise, the request will fail due to invalid syntax.
// https://docs.microsoft.com/en-us/graph/query-parameters#escaping-single-quotes
func EscapeSingleQuote(qparam string) string {
return strings.ReplaceAll(qparam, `'`, `''`)
}