Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit df24cb2

Browse files
authored
add exception catch for relative endpoint (#984)
1 parent 6a0f42c commit df24cb2

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

packages/lu/src/parser/lubuild/core.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const exception = require('./../utils/exception')
1313
const Luis = require('./../luis/luis')
1414

1515
const rateLimitErrorCode = 429
16+
const absoluteUrlPattern = /^https?:\/\//i
1617

1718
export class LuBuildCore {
1819
private readonly client: any
@@ -27,6 +28,11 @@ export class LuBuildCore {
2728
this.retryCount = retryCount
2829
this.retryDuration = retryDuration
2930

31+
// check endpoint is absolute or not
32+
if (!absoluteUrlPattern.test(endpoint)) {
33+
throw (new exception(retCode.errorCode.INVALID_URI, `Only absolute URLs are supported. "${endpoint}" is not an absolute LUIS endpoint URL.`))
34+
}
35+
3036
// new luis api client
3137
const creds = new CognitiveServicesCredentials(subscriptionKey)
3238
this.client = new LUISAuthoringClient(creds, endpoint)

packages/lu/src/parser/qnabuild/core.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ const exception = require('./../utils/exception')
88
const {ServiceBase} = require('./serviceBase')
99
const NEWLINE = require('os').EOL
1010

11+
const absoluteUrlPattern = /^https?:\/\//i
12+
1113
export class QnaBuildCore {
1214
private readonly service: any
1315

1416
constructor(subscriptionkey: string, endpoint: string) {
17+
// check endpoint is absolute or not
18+
if (!absoluteUrlPattern.test(endpoint)) {
19+
throw (new exception(retCode.errorCode.INVALID_URI, `Only absolute URLs are supported. "${endpoint}" is not an absolute qnamaker endpoint URL.`))
20+
}
21+
1522
this.service = new ServiceBase(endpoint, subscriptionkey)
1623
}
1724

packages/lu/test/parser/lubuild/lubuild.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,38 @@ describe('builder: loadContents function can catch invalid import exceptions suc
377377
assert.isTrue(e.text.includes("Invalid LU file") && e.text.includes(`[ERROR] URI: "https://xxxxx/1.lu" appears to be invalid.`))
378378
}
379379
})
380+
})
381+
382+
describe('builder: build function can catch relative endpoint exception successfully', () => {
383+
it('should throw exception for non absolute endpoint', async () => {
384+
const builder = new Builder(() => { })
385+
try {
386+
await builder.build(
387+
[new luObject('# Greeting')],
388+
undefined,
389+
'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
390+
'http:fsd'
391+
)
392+
393+
assert.fail("Relative endpoint exception is not thrown.")
394+
} catch (e) {
395+
assert.equal(e.text, `Only absolute URLs are supported. "http:fsd" is not an absolute LUIS endpoint URL.`)
396+
}
397+
})
398+
399+
it('should throw exception for non absolute endpoint', async () => {
400+
const builder = new Builder(() => { })
401+
try {
402+
await builder.build(
403+
[new luObject('# Greeting')],
404+
undefined,
405+
'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
406+
'fsd'
407+
)
408+
409+
assert.fail("Relative endpoint exception is not thrown.")
410+
} catch (e) {
411+
assert.equal(e.text, `Only absolute URLs are supported. "fsd" is not an absolute LUIS endpoint URL.`)
412+
}
413+
})
380414
})

packages/lu/test/parser/qnabuild/qnabuild.test.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const uuidv1 = require('uuid/v1')
44
const path = require('path')
55
const NEWLINE = require('os').EOL
66
const Builder = require('../../../src/parser/qnabuild/builder').Builder
7-
const luObject = require('../../../src/parser/lu/lu')
8-
const luOptions = require('../../../src/parser/lu/luOptions')
7+
const qnaObject = require('../../../src/parser/lu/qna')
8+
const qnaOptions = require('../../../src/parser/lu/qnaOptions')
99
const txtfile = require('../../../src/parser/lufile/read-text-file');
1010

1111
const rootDir = path.join(__dirname, './../../fixtures/testcases/import-resolver/qna-import-resolver')
@@ -271,7 +271,7 @@ describe('builder: loadContents function can resolve import files with customize
271271
file.filePath = file.filePath.slice(0, file.filePath.length - 3) + "en-us.qna"
272272
}
273273

274-
luObjects.push(new luObject(txtfile.readSync(file.filePath), new luOptions(file.filePath, file.includeInCollate)))
274+
luObjects.push(new qnaObject(txtfile.readSync(file.filePath), new qnaOptions(file.filePath, file.includeInCollate)))
275275
}
276276
return luObjects
277277
};
@@ -290,4 +290,38 @@ describe('builder: loadContents function can resolve import files with customize
290290
assert.isTrue(result.qnaContents[0].content.includes(
291291
`!# @qna.pair.source = custom editorial${NEWLINE}${NEWLINE}## ? help${NEWLINE}- could you help${NEWLINE}${NEWLINE}\`\`\`markdown${NEWLINE}help answer${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}> !# @qna.pair.source = custom editorial${NEWLINE}${NEWLINE}## ? welcome${NEWLINE}${NEWLINE}\`\`\`markdown${NEWLINE}welcome here${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}> !# @qna.pair.source = custom editorial${NEWLINE}${NEWLINE}## ? cancel${NEWLINE}${NEWLINE}\`\`\`markdown${NEWLINE}cancel the task${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}> !# @qna.pair.source = custom editorial${NEWLINE}${NEWLINE}## ? stop${NEWLINE}${NEWLINE}\`\`\`markdown${NEWLINE}stop that${NEWLINE}\`\`\``))
292292
})
293+
})
294+
295+
describe('builder: build function can catch relative endpoint exception successfully', () => {
296+
it('should throw exception for non absolute endpoint', async () => {
297+
const builder = new Builder(() => { })
298+
try {
299+
await builder.build(
300+
[new qnaObject(`# ? Greeting${NEWLINE}\`\`\`${NEWLINE}hello${NEWLINE}\`\`\``)],
301+
undefined,
302+
'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
303+
'http:fsd'
304+
)
305+
306+
assert.fail("Relative endpoint exception is not thrown.")
307+
} catch (e) {
308+
assert.equal(e.text, `Only absolute URLs are supported. "http:fsd" is not an absolute qnamaker endpoint URL.`)
309+
}
310+
})
311+
312+
it('should throw exception for non absolute endpoint', async () => {
313+
const builder = new Builder(() => { })
314+
try {
315+
await builder.build(
316+
[new qnaObject(`# ? Greeting${NEWLINE}\`\`\`${NEWLINE}hello${NEWLINE}\`\`\``)],
317+
undefined,
318+
'f8c64e2a-1111-3a09-8f78-39d7adc76ec5',
319+
'fsd'
320+
)
321+
322+
assert.fail("Relative endpoint exception is not thrown.")
323+
} catch (e) {
324+
assert.equal(e.text, `Only absolute URLs are supported. "fsd" is not an absolute qnamaker endpoint URL.`)
325+
}
326+
})
293327
})

0 commit comments

Comments
 (0)