Skip to content

Commit 78cc226

Browse files
[fix] Refactored mixed-content handling and added regression test
- Consolidated duplicated error handling into submitWithMixedContentCheck helper. - Added a regression test case to verify loading state reset and toast notification when an insecure form submission is blocked on an HTTPS page. - Fixed linting by using destructured setLoading throughout the status component. fixes #602
1 parent beb9d7c commit 78cc226

2 files changed

Lines changed: 45 additions & 23 deletions

File tree

client/components/status/status.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ export default class Status extends React.Component {
103103
}
104104
}
105105

106+
submitCaptivePortalForm = (formRef) => {
107+
const {setLoading} = this.context;
108+
try {
109+
checkMixedContent(formRef.current.action);
110+
formRef.current.submit();
111+
return true;
112+
} catch (error) {
113+
setLoading(false);
114+
toast.error(`Security/Network Error: ${error.message}`);
115+
console.error("Mixed Content Exception:", error);
116+
return false;
117+
}
118+
};
119+
106120
async componentDidMount() {
107121
const {
108122
cookies,
@@ -247,14 +261,7 @@ export default class Status extends React.Component {
247261
cookies,
248262
);
249263
this.notifyCpLogin(userData);
250-
try {
251-
checkMixedContent(this.loginFormRef.current.action);
252-
this.loginFormRef.current.submit();
253-
} catch (error) {
254-
setLoading(false);
255-
toast.error(`Security/Network Error: ${error.message}`);
256-
console.error("Mixed Content Exception:", error);
257-
}
264+
this.submitCaptivePortalForm(this.loginFormRef);
258265
} else if (!shouldLogin) {
259266
// If the user is already logged in, we need to handle the
260267
// the response from the captive portal.
@@ -608,14 +615,7 @@ export default class Status extends React.Component {
608615
true,
609616
cookies,
610617
);
611-
try {
612-
checkMixedContent(this.logoutFormRef.current.action);
613-
this.logoutFormRef.current.submit();
614-
} catch (error) {
615-
setLoading(false);
616-
toast.error(`Security/Network Error: ${error.message}`);
617-
console.error("Mixed Content Exception:", error);
618-
}
618+
this.submitCaptivePortalForm(this.logoutFormRef);
619619
}
620620
return;
621621
}
@@ -927,13 +927,7 @@ export default class Status extends React.Component {
927927
});
928928
const {setLoading} = this.context;
929929
if (this.logoutFormRef && this.logoutFormRef.current) {
930-
try {
931-
checkMixedContent(this.logoutFormRef.current.action);
932-
this.logoutFormRef.current.submit();
933-
} catch (error) {
934-
setLoading(false);
935-
toast.error(`Security/Network Error: ${error.message}`);
936-
console.error("Mixed Content Exception:", error);
930+
if (!this.submitCaptivePortalForm(this.logoutFormRef)) {
937931
return;
938932
}
939933
}

client/components/status/status.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,4 +2886,32 @@ describe("<Status /> accounting_swap_octets", () => {
28862886
expect(rows.at(3).find("td").text()).toBe("1 kB");
28872887
expect(rows.at(4).find("td").text()).toBe("2 kB");
28882888
});
2889+
2890+
it("should catch mixed content exceptions in submitCaptivePortalForm", () => {
2891+
props = createTestProps();
2892+
const setLoadingMock = jest.fn();
2893+
wrapper = shallow(<Status {...props} />, {
2894+
context: {setLoading: setLoadingMock},
2895+
disableLifecycleMethods: true,
2896+
});
2897+
jest.spyOn(toast, "error");
2898+
2899+
const mockFormRef = {
2900+
current: {
2901+
action: "http://example.com",
2902+
submit: () => {
2903+
throw new Error(
2904+
"Mixed Content: Cannot submit insecure HTTP form from a secure HTTPS page.",
2905+
);
2906+
},
2907+
},
2908+
};
2909+
2910+
const result = wrapper.instance().submitCaptivePortalForm(mockFormRef);
2911+
2912+
expect(result).toBe(false);
2913+
expect(setLoadingMock).toHaveBeenCalledWith(false);
2914+
expect(toast.error).toHaveBeenCalled();
2915+
expect(toast.error.mock.calls[0][0]).toContain("Mixed Content");
2916+
});
28892917
});

0 commit comments

Comments
 (0)