-
Notifications
You must be signed in to change notification settings - Fork 675
fix: user-agent should be uri safe #2546
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@slack/web-api": patch | ||
| --- | ||
|
|
||
| Fix user-agent header to URI-encode characters outside the Latin-1 range, preventing errors when `process.title` contains non-ASCII characters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { createRequire } from 'node:module'; | ||
| import { afterEach, describe, it } from 'node:test'; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
|
|
||
| function isLatin1Safe(s: string): boolean { | ||
| return Buffer.from(s, 'latin1').toString('latin1') === s; | ||
| } | ||
|
|
||
| describe('instrument', () => { | ||
| const originalDescriptor = Object.getOwnPropertyDescriptor(process, 'title'); | ||
| const modulePath = require.resolve('./instrument.ts'); | ||
|
|
||
| afterEach(() => { | ||
| // Restore the original process.title property | ||
| if (originalDescriptor) { | ||
| Object.defineProperty(process, 'title', originalDescriptor); | ||
| } | ||
| // Clear module cache so next require gets a fresh evaluation | ||
| delete require.cache[modulePath]; | ||
| }); | ||
|
|
||
| function mockProcessTitle(title: string): void { | ||
| Object.defineProperty(process, 'title', { | ||
| get: () => title, | ||
| configurable: true, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a fresh import of the instrument module. Since `baseUserAgent` is computed at module | ||
| * load time, we clear the require cache and re-require to pick up a mocked `process.title`. | ||
| */ | ||
| function freshImport(): typeof import('./instrument') { | ||
| delete require.cache[modulePath]; | ||
| return require(modulePath); | ||
| } | ||
|
|
||
| describe('getUserAgent', () => { | ||
| it('should contain the package name', () => { | ||
| const { getUserAgent } = freshImport(); | ||
| const ua = getUserAgent(); | ||
| assert.ok(ua.includes('@slack:web-api'), `User-Agent should contain @slack:web-api: ${ua}`); | ||
| }); | ||
|
|
||
| it('should include an ASCII process.title in the user agent', () => { | ||
| mockProcessTitle('node'); | ||
| const { getUserAgent } = freshImport(); | ||
| const ua = getUserAgent(); | ||
| assert.ok(ua.includes('node/'), `User-Agent should contain node/: ${ua}`); | ||
| assert.ok(isLatin1Safe(ua)); | ||
| }); | ||
|
|
||
| it('should include other ASCII process.title in the user agent', () => { | ||
| mockProcessTitle('openclaw-gateway'); | ||
| const { getUserAgent } = freshImport(); | ||
| const ua = getUserAgent(); | ||
| assert.ok(ua.includes('openclaw-gateway/'), `User-Agent should contain openclaw-gateway/: ${ua}`); | ||
| assert.ok(isLatin1Safe(ua)); | ||
| }); | ||
|
|
||
| it('should return a Latin-1 safe user agent when process.title contains non-Latin-1 characters', () => { | ||
| const notLatin1SafeTitle = '管理者: Windows PowerShell'; | ||
| assert.strictEqual(isLatin1Safe(notLatin1SafeTitle), false); | ||
|
|
||
| mockProcessTitle(notLatin1SafeTitle); | ||
| const { getUserAgent } = freshImport(); | ||
| const ua = getUserAgent(); | ||
| assert.ok(isLatin1Safe(ua), `User-Agent contains non-Latin-1 characters: ${ua}`); | ||
| assert.ok(!ua.includes(notLatin1SafeTitle), 'User-Agent should not contain raw non-ASCII characters'); | ||
| assert.ok( | ||
| ua.includes('%E7%AE%A1%E7%90%86%E8%80%85: Windows PowerShell'), | ||
| 'User-Agent should percent-encode only non-Latin-1 characters', | ||
| ); | ||
| }); | ||
|
|
||
| it('should preserve Latin-1 characters in process.title', () => { | ||
| mockProcessTitle('café'); | ||
| const { getUserAgent } = freshImport(); | ||
| const ua = getUserAgent(); | ||
| assert.ok(ua.includes('café/'), `User-Agent should preserve Latin-1 characters: ${ua}`); | ||
| assert.ok(isLatin1Safe(ua)); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎹 praise: Wonderful additions to coverages and cases here! Thanks!