Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import { RetryError } from '../../error/http-error';
import { consoleFetch } from '../console-fetch';
import { shouldLogout, validateStatus } from '../console-fetch-utils';
import { shouldLogout, unescapeGoUnicode, validateStatus } from '../console-fetch-utils';

describe('unescapeGoUnicode', () => {
it('should unescape 4-digit Go unicode escapes', () => {
expect(unescapeGoUnicode('\\ue00f')).toBe('\ue00f');
expect(unescapeGoUnicode('\\ue4c8')).toBe('\ue4c8');
});

it('should unescape 8-digit Go unicode escapes for supplementary plane characters', () => {
expect(unescapeGoUnicode('\\U0002ebf0')).toBe(String.fromCodePoint(0x2ebf0));
expect(unescapeGoUnicode('\\U0002ebf1')).toBe(String.fromCodePoint(0x2ebf1));
});

it('should unescape mixed content with normal text and escapes', () => {
const input = 'a啊阿沸犯跃kg\\ue00f\\ue010\\ue011\\ue4c8丙乩h妖哪匸与f去\\U0002ebf0\\U0002ebf1';
const expected = `a啊阿沸犯跃kg\ue00f\ue010\ue011\ue4c8丙乩h妖哪匸与f去${String.fromCodePoint(
0x2ebf0,
)}${String.fromCodePoint(0x2ebf1)}`;
expect(unescapeGoUnicode(input)).toBe(expected);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add a test case for invalid/out-of-range escape sequences to ensure the function doesn't throw unexpectedly?


it('should not throw on out-of-range 8-digit escape sequences', () => {
expect(unescapeGoUnicode('\\UFFFFFFFF')).toBe('\\UFFFFFFFF');
});
});

describe('consoleFetch', () => {
const json = async () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ export const shouldLogout = (url: string): boolean => {
return false;
};

/**
* Converts Go-style Unicode escape sequences (\uXXXX, \UXXXXXXXX) in K8s API error
* messages back to actual Unicode characters for proper display in the browser.
*/
export const unescapeGoUnicode = (str: string): string =>
str
.replace(/\\U([0-9a-fA-F]{8})/g, (match, hex) => {
const codePoint = parseInt(hex, 16);
return codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : match;
})
.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCodePoint(parseInt(hex, 16)));

export const validateStatus = async (
response: Response,
url: string,
Expand Down Expand Up @@ -183,7 +195,7 @@ export const validateStatus = async (
if (response.status === 403) {
return response.json().then((json) => {
throw new HttpError(
json.message || 'Access denied due to cluster policy.',
unescapeGoUnicode(json.message || 'Access denied due to cluster policy.'),
response.status,
response,
json,
Expand Down Expand Up @@ -217,6 +229,6 @@ export const validateStatus = async (
reason = response.statusText;
}

throw new HttpError(reason, response.status, response, json);
throw new HttpError(unescapeGoUnicode(reason), response.status, response, json);
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Apply unescape consistently in the 403 JSON error path as well.

Right now unescaping is only applied in this branch; the 403 branch still uses raw json.message, so escaped Unicode can still leak to users there.

💡 Proposed fix
   if (response.status === 403) {
     return response.json().then((json) => {
       throw new HttpError(
-        json.message || 'Access denied due to cluster policy.',
+        unescapeGoUnicode(json.message || 'Access denied due to cluster policy.'),
         response.status,
         response,
         json,
       );
     });
   }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@frontend/packages/console-dynamic-plugin-sdk/src/utils/fetch/console-fetch-utils.ts`
at line 229, The 403 JSON error branch still passes raw json.message into the
HttpError, so escaped Unicode can leak; update the 403 handling to call
unescapeGoUnicode(json.message) before constructing the HttpError (same as the
other branch that throws new HttpError(unescapeGoUnicode(reason), ...)). Locate
the 403 response branch in console-fetch-utils.ts and replace the raw
json.message usage with unescapeGoUnicode(json.message) when creating the
HttpError so both error paths behave consistently.

Copy link
Contributor

Choose a reason for hiding this comment

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

The fix was applied here but not to the existing 403 handler above (line 194). Could Unicode escapes also appear in 403 json.message responses? If so, should we apply the same treatment there for consistency?

});
};