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

chore: Add regex validation for session name to relevant resolvers #1700

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import class AwsCommonRuntimeKit.CredentialsProvider
import ClientRuntime
import enum Smithy.ClientError
import protocol SmithyIdentity.AWSCredentialIdentityResolver
import protocol SmithyIdentity.AWSCredentialIdentityResolvedByCRT
import struct Foundation.TimeInterval
Expand Down Expand Up @@ -36,6 +37,7 @@
sessionName: String,
durationSeconds: TimeInterval = 900
) throws {
try validateString(name: sessionName, regex: "^[\\w+=,.@-]*$")
self.crtAWSCredentialIdentityResolver = try AwsCommonRuntimeKit.CredentialsProvider(source: .sts(
bootstrap: SDKDefaultIO.shared.clientBootstrap,
tlsContext: SDKDefaultIO.shared.tlsContext,
Expand All @@ -48,3 +50,9 @@
}

// swiftlint:enable type_name

func validateString(name: String, regex: String) throws {
guard let _ = name.range(of: regex, options: .regularExpression) else {

Check warning on line 55 in Sources/Core/AWSSDKIdentity/Sources/AWSSDKIdentity/AWSCredentialIdentityResolvers/STSAssumeRoleAWSCredentialIdentityResolver.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Prefer `!= nil` over `let _ =` (unused_optional_binding)
jbelkins marked this conversation as resolved.
Show resolved Hide resolved
throw ClientError.invalidValue("The input value [\(name)] does not match the required regex: \(regex)")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public struct STSWebIdentityAWSCredentialIdentityResolver: AWSCredentialIdentity
roleSessionName: String? = nil,
tokenFilePath: String? = nil
) throws {
if let roleSessionName {
try validateString(name: roleSessionName, regex: "^[\\w+=,.@-]*$")
}
let fileBasedConfig = try CRTFileBasedConfiguration(
configFilePath: configFilePath,
credentialsFilePath: credentialsFilePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import XCTest
import struct AWSSDKIdentity.STSAssumeRoleAWSCredentialIdentityResolver
import struct AWSSDKIdentity.EnvironmentAWSCredentialIdentityResolver
import enum Smithy.ClientError

class STSAssumeRoleAWSCredentialIdentityResolverTests: XCTestCase {
func testInit() {
Expand All @@ -22,4 +23,18 @@ class STSAssumeRoleAWSCredentialIdentityResolverTests: XCTestCase {
sessionName: "some-session"
))
}

func testInvalidSessionName() async throws {
XCTAssertThrowsError(try STSAssumeRoleAWSCredentialIdentityResolver(
awsCredentialIdentityResolver: try EnvironmentAWSCredentialIdentityResolver(),
roleArn: "role",
sessionName: "invalid session name with spaces"
)) { error in
if case ClientError.invalidValue = error {
// The test passes if this case is matched
} else {
XCTFail("Expected ClientError.invalidValue error, but got \(error)")
}
}
}
}
Loading