Skip to content

Commit e9d4c1f

Browse files
authored
fix(integrations): reject invalid kalshi order flags and anchor the arxiv link match (#10186)
1 parent bc48d10 commit e9d4c1f

4 files changed

Lines changed: 101 additions & 12 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { getPaperTool } from '@/tools/arxiv/get_paper'
3+
4+
function requestedId(paperId: string): string | null {
5+
const url = getPaperTool.request.url
6+
if (typeof url !== 'function') throw new Error('get paper tool has no url builder')
7+
return new URL(url({ paperId })).searchParams.get('id_list')
8+
}
9+
10+
describe('arXiv get paper ID parsing', () => {
11+
it.each([
12+
['1706.03762', '1706.03762'],
13+
[' 1706.03762 ', '1706.03762'],
14+
['hep-th/9901001', 'hep-th/9901001'],
15+
['https://arxiv.org/abs/1706.03762', '1706.03762'],
16+
['http://arxiv.org/abs/1706.03762v5', '1706.03762v5'],
17+
['https://arxiv.org/pdf/1706.03762v2.pdf', '1706.03762v2'],
18+
['https://arxiv.org/pdf/1706.03762v2', '1706.03762v2'],
19+
['https://arxiv.org/abs/1706.03762?context=cs#abstract', '1706.03762'],
20+
['https://arxiv.org/abs/hep-th/9901001', 'hep-th/9901001'],
21+
['https://www.arxiv.org/abs/1706.03762', '1706.03762'],
22+
['https://export.arxiv.org/abs/1706.03762', '1706.03762'],
23+
['arxiv.org/abs/1706.03762', '1706.03762'],
24+
['www.arxiv.org/pdf/1706.03762.pdf', '1706.03762'],
25+
])('reads %s as %s', (input, expected) => {
26+
expect(requestedId(input)).toBe(expected)
27+
})
28+
29+
it.each([
30+
'https://evil.com/?x=arxiv.org/abs/1234',
31+
'https://evil.com/arxiv.org/abs/1234',
32+
'https://notarxiv.org/abs/1234',
33+
'https://arxiv.org.evil.com/abs/1234',
34+
])('does not unwrap %s as an arXiv link', (input) => {
35+
expect(requestedId(input)).not.toBe('1234')
36+
})
37+
})

‎apps/sim/tools/arxiv/get_paper.ts‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,36 @@ import type { ArxivGetPaperParams, ArxivGetPaperResponse } from '@/tools/arxiv/t
22
import { parseArxivXML } from '@/tools/arxiv/utils'
33
import type { ToolConfig } from '@/tools/types'
44

5+
/** A scheme-less link on arxiv.org or one of its subdomains, such as `www.arxiv.org/abs/<id>`. */
6+
const SCHEMELESS_ARXIV_LINK = /^(?:[a-z0-9-]+\.)*arxiv\.org\//i
7+
8+
/**
9+
* The paper ID from a bare ID or an arxiv.org `/abs/` or `/pdf/` link. Only a link whose host is
10+
* arxiv.org or a subdomain of it is unwrapped; any other input is taken as a bare ID.
11+
*/
12+
function arxivPaperId(input: string): string {
13+
const trimmed = input.trim()
14+
const linkPath = arxivLinkPath(
15+
SCHEMELESS_ARXIV_LINK.test(trimmed) ? `https://${trimmed}` : trimmed
16+
)
17+
const id = linkPath ?? trimmed.replace(/[?#].*$/, '')
18+
return id.replace(/\.pdf$/, '')
19+
}
20+
21+
/** The ID after `/abs/` or `/pdf/` when `candidate` is an http(s) arxiv.org link. */
22+
function arxivLinkPath(candidate: string): string | undefined {
23+
let url: URL
24+
try {
25+
url = new URL(candidate)
26+
} catch {
27+
return undefined
28+
}
29+
if (url.protocol !== 'https:' && url.protocol !== 'http:') return undefined
30+
const host = url.hostname.toLowerCase()
31+
if (host !== 'arxiv.org' && !host.endsWith('.arxiv.org')) return undefined
32+
return /^\/(?:abs|pdf)\/(.+)$/.exec(url.pathname)?.[1]
33+
}
34+
535
export const getPaperTool: ToolConfig<ArxivGetPaperParams, ArxivGetPaperResponse> = {
636
id: 'arxiv_get_paper',
737
name: 'ArXiv Get Paper',
@@ -19,12 +49,7 @@ export const getPaperTool: ToolConfig<ArxivGetPaperParams, ArxivGetPaperResponse
1949

2050
request: {
2151
url: (params: ArxivGetPaperParams) => {
22-
const paperId = params.paperId
23-
.trim()
24-
.replace(/^.*arxiv\.org\/(?:abs|pdf)\//, '')
25-
.replace(/[?#].*$/, '')
26-
.replace(/\.pdf$/, '')
27-
52+
const paperId = arxivPaperId(params.paperId)
2853
const baseUrl = 'https://export.arxiv.org/api/query'
2954
const searchParams = new URLSearchParams()
3055
searchParams.append('id_list', paperId)

‎apps/sim/tools/kalshi/create_order.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ describe('Kalshi create order on the V2 event-order endpoint', () => {
109109
expect(() => createBody({ yesPrice: '56', noPrice: '50' })).toThrow(/different prices/)
110110
})
111111

112+
it('refuses an order flag that is neither true nor false instead of dropping the restriction', () => {
113+
expect(() => createBody({ yesPrice: '56', postOnly: 'ture' })).toThrow(
114+
/Post Only must be true or false, got "ture"/
115+
)
116+
expect(() => createBody({ yesPrice: '56', reduceOnly: 'yes' })).toThrow(/Reduce Only/)
117+
expect(() => createBody({ yesPrice: '56', cancelOrderOnPause: '1' })).toThrow(
118+
/Cancel Order on Pause/
119+
)
120+
expect(
121+
createBody({
122+
yesPrice: '56',
123+
postOnly: ' TRUE ',
124+
reduceOnly: true as unknown as string,
125+
cancelOrderOnPause: 'False',
126+
})
127+
).toMatchObject({ post_only: true, reduce_only: true, cancel_order_on_pause: false })
128+
expect(createBody({ yesPrice: '56', postOnly: '' })).not.toHaveProperty('post_only')
129+
})
130+
112131
it('posts to /portfolio/events/orders and signs that path', () => {
113132
const { url, headers } = kalshiCreateOrderTool.request
114133
if (typeof url !== 'function' || typeof headers !== 'function') {

‎apps/sim/tools/kalshi/orders.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,17 @@ function oneOf(value: unknown, allowed: string[], fallback: string, label: strin
8686
return text
8787
}
8888

89-
/** `undefined` when unset, so an untouched field sends nothing; `'false'` stays an explicit false. */
90-
function optionalFlag(value: unknown): boolean | undefined {
91-
return isBlank(value) ? undefined : String(value).trim().toLowerCase() === 'true'
89+
/**
90+
* `undefined` when unset, so an untouched field sends nothing; `'false'` stays an explicit false.
91+
* Anything other than true or false throws, so a typo never drops a requested safety restriction.
92+
*/
93+
function optionalFlag(value: unknown, label: string): boolean | undefined {
94+
if (typeof value === 'boolean') return value
95+
if (isBlank(value)) return undefined
96+
const text = String(value).trim().toLowerCase()
97+
if (text === 'true') return true
98+
if (text === 'false') return false
99+
throw new Error(`${label} must be true or false, got "${String(value).trim()}"`)
92100
}
93101

94102
/** The V2 book side for a legacy (side, action): buy yes and sell no bid; buy no and sell yes ask. */
@@ -194,11 +202,11 @@ export function buildKalshiCreateOrderBody(
194202
if (!isBlank(params.expirationTs)) {
195203
body.expiration_time = parseScaled(params.expirationTs, 0, 'Expiration')
196204
}
197-
const postOnly = optionalFlag(params.postOnly)
205+
const postOnly = optionalFlag(params.postOnly, 'Post Only')
198206
if (postOnly !== undefined) body.post_only = postOnly
199-
const reduceOnly = optionalFlag(params.reduceOnly)
207+
const reduceOnly = optionalFlag(params.reduceOnly, 'Reduce Only')
200208
if (reduceOnly !== undefined) body.reduce_only = reduceOnly
201-
const cancelOnPause = optionalFlag(params.cancelOrderOnPause)
209+
const cancelOnPause = optionalFlag(params.cancelOrderOnPause, 'Cancel Order on Pause')
202210
if (cancelOnPause !== undefined) body.cancel_order_on_pause = cancelOnPause
203211
if (!isBlank(params.orderGroupId)) body.order_group_id = params.orderGroupId
204212
if (!isBlank(params.subaccount)) body.subaccount = parseScaled(params.subaccount, 0, 'Subaccount')

0 commit comments

Comments
 (0)