Skip to content

Commit

Permalink
Revert "fix: repair the impact of JSONP on normal requests(#2692) (#2699
Browse files Browse the repository at this point in the history
)" (#2700)

This reverts commit a2d7ad7.
  • Loading branch information
czy88840616 authored Feb 2, 2023
1 parent a2d7ad7 commit b635767
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 35 deletions.
41 changes: 19 additions & 22 deletions packages/cross-domain/src/jsonp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,28 @@ export class JSONPService {
res;

jsonp(body: any, config?: JSONPOptions) {
const { callback, limit } = Object.assign({}, this.jsonpConfig, config);
const name = this.ctx.query[callback] || this.ctx.query['callback'];
if (name) {
this.ctx.type = 'js';
// https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/X-Content-Type-Options
if (this.ctx.set) {
this.ctx.set('x-content-type-options', 'nosniff');
} else if (this.res.set) {
this.res.set('x-content-type-options', 'nosniff');
}
this.ctx.type = 'js';
// https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/X-Content-Type-Options
if (this.ctx.set) {
this.ctx.set('x-content-type-options', 'nosniff');
} else if (this.res.set) {
this.res.set('x-content-type-options', 'nosniff');
}

// Only allow "[","]","a-zA-Z0123456789_", "$" and "." characters.
let cb = (this.ctx.query[callback] || 'callback').replace(
/[^[\]\w$.]+/g,
''
);
const { callback, limit } = Object.assign({}, this.jsonpConfig, config);

if (cb.length > limit) {
cb = cb.substring(0, limit);
}
// Only allow "[","]","a-zA-Z0123456789_", "$" and "." characters.
let cb = (this.ctx.query[callback] || 'callback').replace(
/[^[\]\w$.]+/g,
''
);

const str = JSON.stringify(body === undefined ? null : body);
// protect from jsonp xss
return `/**/ typeof ${cb} === 'function' && ${cb}(${str});`;
if (cb.length > limit) {
cb = cb.substring(0, limit);
}
return body;

const str = JSON.stringify(body === undefined ? null : body);
// protect from jsonp xss
return `/**/ typeof ${cb} === 'function' && ${cb}(${str});`;
}
}
9 changes: 2 additions & 7 deletions packages/cross-domain/test/express.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('test/express.test.ts', function () {
.expect(res => {
assert(!res.headers['access-control-allow-origin']);
})
.expect(200);
.expect(200)
});

it('jsonp callback', async () => {
Expand All @@ -51,11 +51,6 @@ describe('test/express.test.ts', function () {
.post('/jsonp?callback=fn')
.expect(200)
.expect('x-content-type-options', 'nosniff')
.expect(`/**/ typeof callback === 'function' && callback({"test":123});`);
});

it('jsonp callback ignore', async () => {
const request = await createHttpRequest(app);
await request.post('/jsonp').expect(200).expect({ test: 123 });
.expect(`/**/ typeof callback === 'function' && callback({"test":123});`)
});
});
9 changes: 3 additions & 6 deletions packages/cross-domain/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('test/koa.test.ts', function () {
try {
app = await createApp(appDir);
} catch (e) {
console.log('e', e);
console.log("e", e);
}
});

Expand Down Expand Up @@ -49,17 +49,14 @@ describe('test/koa.test.ts', function () {
.expect(200);
});


it('jsonp callback', async () => {
const request = await createHttpRequest(app);
await request
.post('/jsonp?callback=fn')
.expect(200)
.expect('x-content-type-options', 'nosniff')
.expect(`/**/ typeof callback === 'function' && callback({"test":123});`);
});
.expect(`/**/ typeof callback === 'function' && callback({"test":123});`)

it('jsonp callback ignore', async () => {
const request = await createHttpRequest(app);
await request.post('/jsonp').expect(200).expect({ test: 123 });
});
});

0 comments on commit b635767

Please sign in to comment.