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

bridge: Fix crash on invalid cockpit.conf #21270

Merged
merged 2 commits into from
Nov 15, 2024
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
14 changes: 14 additions & 0 deletions pkg/base1/test-dbus.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,20 @@ Empty=
assert.rejects(proxy.GetString("SomeSection", "UnknownKey"),
/key.*UnknownKey.*not exist/,
"unknown key raises an error");

// empty config
await cockpit.file(configDir + "/cockpit/cockpit.conf").replace("");
await proxy.Reload();
assert.rejects(proxy.GetString("SomeSection", "SomeA"),
/key.*SomeSection.*not exist/,
"query in empty config raises an error");

// broken config (missing section header)
await cockpit.file(configDir + "/cockpit/cockpit.conf").replace("SomeA = two");
await proxy.Reload();
assert.rejects(proxy.GetString("SomeSection", "SomeA"),
/key.*SomeSection.*not exist/,
"query in broken config raises an error");
});

QUnit.test("nonexisting address", async assert => {
Expand Down
7 changes: 6 additions & 1 deletion src/cockpit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ def reload(self):
cockpit_conf = lookup_config('cockpit.conf')
logger.debug("cockpit.Config: loading %s", cockpit_conf)
# this may not exist, but it's ok to not have a config file and thus leave self.config empty
self.config.read(cockpit_conf)
try:
self.config.read(cockpit_conf)
except configparser.Error as exc:
logger.warning("cockpit.conf is invalid: %s", exc)
self.config.clear()
return


class Environment(bus.Object, interface='cockpit.Environment'):
Expand Down
17 changes: 16 additions & 1 deletion test/verify/check-connection
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ until pgrep -f '^(/usr/[^ ]+/[^ /]*python[^ /]* )?/usr/bin/cockpit-bridge'; do s
self.assertEqual(m.execute("curl -sfS http://localhost:9090/ca.cer"), "FAKE CERT FOR TESTING\n")

@testlib.nondestructive
def test_branding(self):
def testBranding(self):
m = self.machine
m.start_cockpit()

Expand Down Expand Up @@ -1209,6 +1209,21 @@ UnixPath=/run/cockpit/session
self.login_and_go(None)
b.wait_visible("#nav-system li:contains(Hackices)")
self.assertFalse(b.is_present("#nav-system li:contains(Services)"))
b.logout()

# still starts with a broken config (no section headers)
m.write("/etc/test-xdg/cockpit/cockpit.conf", "LoginTitle=NoNoNO\n")
m.execute("systemctl stop cockpit")
self.login_and_go(None)
b.wait_visible("#nav-system li:contains(Hackices)")
self.assertFalse(b.is_present("#nav-system li:contains(Services)"))
b.logout()
# message from cockpit-ws
self.allow_journal_messages(".*cockpit.conf: key=val line not in any section.*")
# message from bridge
self.allow_journal_messages(".*cockpit.conf is invalid: File contains no section headers.*")
self.allow_journal_messages(".*cockpit.conf.*line: 1")
self.allow_journal_messages(".*LoginTitle=NoNoNO.*")

@testlib.nondestructive
@testlib.skipBeiboot("no cockpit-bridge binary on host in beiboot mode")
Expand Down