Skip to content

Reject keywords#1565

Open
dancewithheart wants to merge 7 commits into
trifectatechfoundation:mainfrom
dancewithheart:reject-keywords
Open

Reject keywords#1565
dancewithheart wants to merge 7 commits into
trifectatechfoundation:mainfrom
dancewithheart:reject-keywords

Conversation

@dancewithheart
Copy link
Copy Markdown

Fix #700

  • reject keywords: CHROOT, CWD, NOTAFTER, NOTBEFORE, TIMEOUT
  • un-ignore compliance tests for reserved alias keywords

"{stderr}"
);
} else {
assert!(!stderr.is_empty(), "expected stderr for {bad_keyword}");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please keep the check for the syntax error message.

Copy link
Copy Markdown
Author

@dancewithheart dancewithheart May 3, 2026

Choose a reason for hiding this comment

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

This is a bit tricky :) Ideally I would like to keep original check:

assert_contains!(output.stderr(), "syntax error");

and express more specific assertion for reserved words like so ( 7b702ea ):

let stderr = output.stderr();

if super::is_reserved_alias_keyword(bad_keyword) {
    assert!(
        stderr.contains("reserved alias")
            || stderr.contains("reserved word")
            || stderr.contains("syntax error"),
        "{stderr}"
    );
} else {
    assert_contains!(stderr, "syntax error");
}

But after un-ignore compliance tests they e.g.:

SUDO_UNDER_TEST=ours cargo test --manifest-path test-framework/Cargo.toml -p sudo-compliance-tests --features apparmor sudo::sudoers::host_alias::keywords -- --exact --nocapture

fail with:

[sudo-compliance-tests/src/sudo/sudoers/host_alias.rs:221:9] bad_keyword = "Cmnd_Alias"

thread 'sudo::sudoers::host_alias::keywords' panicked at sudo-compliance-tests/src/sudo/sudoers/host_alias.rs:240:13:
"/etc/sudoers:2:13: expecting '=' but found 'm'\nHost_Alias Cmnd_Alias = container\n            ^\nsudo: I'm sorry root. I'm afraid I can't do that" did not contain "syntax error"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test sudo::sudoers::host_alias::keywords ... FAILED

So I went into the route of having:

let stderr = output.stderr();
if super::is_reserved_alias_keyword(bad_keyword) {
    assert!(
        stderr.contains("reserved alias")
            || stderr.contains("reserved word")
                || stderr.contains("syntax error"),
            "{stderr}"
    );
} else {
    assert!(!stderr.is_empty(), "expected stderr for {bad_keyword}");
}

KEYWORDS_ALIAS_BAD seems to contain several classes of invalid alias names, and not all of them produce diagnostics containing the literal text "syntax error".

I am open to improve the logic of those tests, if you have a different solution in mind.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Perhaps the best approach is to keep this PR focused on rejecting keywords and regression tests. That is what issue #700 asked for.

To fix un-ignored compliance tests, I added conditional matching against stderr - this smells like ad-hoc solution.

For enabling compliance tests (and re-design them) separate issue could be created.

Comment thread src/sudoers/ast.rs Outdated
- preserve previous behavior: all bad keywords should report a syntax error
- check for more specific assert rejection reson for reserved alias / keywords
}

for good_keyword in super::keywords_alias_good() {
for good_keyword in super::keywords_alias_good_for_cmnd_alias() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All these can remain keyword_alias_good except for the Cmnd_Alias test, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point. It should be only for Cmnd_Alias - restored in other 3 tests.

@@ -352,12 +351,22 @@ fn user_alias_keywords() {
.build();

let output = Command::new("sudo").arg("true").output(&env);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add an output.assert_exit_code(1) call.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added (conditionally when bad keyword is not ALL.

…stronger assertion: output.assert_exit_code(1) for bad keyword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sudoers: should reject more keywords in alias name position

2 participants