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

Fix a bug in origin authenticator that wrongly treats empty origin methods as pass #1962

Merged
merged 5 commits into from
Sep 12, 2018
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
51 changes: 20 additions & 31 deletions src/envoy/http/authn/origin_authenticator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,19 @@ OriginAuthenticator::OriginAuthenticator(FilterContext* filter_context,
: AuthenticatorBase(filter_context), policy_(policy) {}

bool OriginAuthenticator::run(Payload* payload) {
bool success = false;

if (policy_.origins_size() == 0) {
switch (policy_.principal_binding()) {
case iaapi::PrincipalBinding::USE_ORIGIN:
// Validation should reject policy that have rule to USE_ORIGIN but
// does not provide any origin method so this code should
// never reach. However, it's ok to treat it as authentication
// fails.
ENVOY_LOG(warn,
"Principal is binded to origin, but no method specified in "
"policy {}",
policy_.DebugString());
break;
case iaapi::PrincipalBinding::USE_PEER:
// On the other hand, it's ok to have no (origin) methods if
// rule USE_SOURCE
success = true;
break;
Copy link
Contributor

@PiotrSikora PiotrSikora Sep 11, 2018

Choose a reason for hiding this comment

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

Previously, this code path would result in filter_context()->setPrincipal(iaapi::PrincipalBinding::USE_PEER) call, whereas after your changes the principal is never set in case of iaapi::PrincipalBinding::USE_PEER.

To be honest, I'm not sure if the previous code was correct, but there is a change here, and I'm not sure if it's intentional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean when binding is USE_PEER and policy_.origins_size() == 0? In this case, we still call setPrincipal(), note I also changed line 76 to also check the !triggered

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, you're right. Thanks!

default:
// Should never come here.
ENVOY_LOG(error, "Invalid binding value for policy {}",
policy_.DebugString());
break;
}
if (policy_.origins_size() == 0 &&
policy_.principal_binding() == iaapi::PrincipalBinding::USE_ORIGIN) {
// Validation should reject policy that have rule to USE_ORIGIN but
// does not provide any origin method so this code should
// never reach. However, it's ok to treat it as authentication
// fails.
ENVOY_LOG(warn,
"Principal is binded to origin, but no method specified in "
"policy {}",
policy_.DebugString());
return false;
}

bool triggered = false;
const char* request_path = nullptr;
if (filter_context()->headerMap().Path() != nullptr) {
request_path = filter_context()->headerMap().Path()->value().c_str();
Expand All @@ -69,6 +54,8 @@ bool OriginAuthenticator::run(Payload* payload) {
"validation");
}

bool triggered = false;
bool triggered_success = false;
for (const auto& method : policy_.origins()) {
const auto& jwt = method.jwt();

Expand All @@ -79,20 +66,22 @@ bool OriginAuthenticator::run(Payload* payload) {
triggered = true;
if (validateJwt(jwt, payload)) {
ENVOY_LOG(debug, "JWT validation succeeded");
success = true;
triggered_success = true;
break;
}
}
}

if (success) {
// returns true if no jwt was triggered, or triggered and success.
if (!triggered || triggered_success) {
filter_context()->setOriginResult(payload);
filter_context()->setPrincipal(policy_.principal_binding());
ENVOY_LOG(debug, "Origin authenticator succeeded");
return true;
}

// If none of the JWT triggered, origin authentication will be ignored, as if
// it is not defined.
return !triggered || success;
ENVOY_LOG(debug, "Origin authenticator failed");
return false;
}

} // namespace AuthN
Expand Down
38 changes: 38 additions & 0 deletions src/envoy/http/authn/origin_authenticator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ namespace Istio {
namespace AuthN {
namespace {

const char kZeroOriginMethodPolicyBindPeer[] = R"(
principal_binding: USE_PEER
)";

const char kZeroOriginMethodPolicyBindOrigin[] = R"(
principal_binding: USE_ORIGIN
)";

const char kSingleOriginMethodPolicy[] = R"(
principal_binding: USE_ORIGIN
origins {
Expand Down Expand Up @@ -209,6 +217,36 @@ TEST_P(OriginAuthenticatorTest, Empty) {
filter_context_.authenticationResult()));
}

// It should fail if the binding is USE_ORIGIN but origin methods are empty.
TEST_P(OriginAuthenticatorTest, ZeroMethodFail) {
ASSERT_TRUE(Protobuf::TextFormat::ParseFromString(
kZeroOriginMethodPolicyBindOrigin, &policy_));
createAuthenticator();
EXPECT_FALSE(authenticator_->run(payload_));
}

// It should pass if the binding is USE_PEER and origin methods are empty.
TEST_P(OriginAuthenticatorTest, ZeroMethodPass) {
ASSERT_TRUE(Protobuf::TextFormat::ParseFromString(
kZeroOriginMethodPolicyBindPeer, &policy_));
createAuthenticator();

Result expected_result = TestUtilities::AuthNResultFromString(R"(
origin {
user: "bar"
presenter: "istio.io"
}
)");
if (set_peer_) {
expected_result.set_principal("bar");
expected_result.set_peer_user("bar");
}

EXPECT_TRUE(authenticator_->run(&jwt_extra_payload_));
EXPECT_TRUE(TestUtility::protoEqual(expected_result,
filter_context_.authenticationResult()));
}

TEST_P(OriginAuthenticatorTest, SingleMethodPass) {
ASSERT_TRUE(Protobuf::TextFormat::ParseFromString(kSingleOriginMethodPolicy,
&policy_));
Expand Down