Skip to content

Commit d8692db

Browse files
authored
(parser) use null prototype objects for languages/aliases (highlightjs#2636)
Fix: Discord uses getLanguage to validate that a language specified exists in highlightJS and retrieve metadata about the language for code block highlighting in chat. Because highlightJS returns prototype values instead of the highlight languages themselves, the result is a few different bugs in our clients which expect the return type to be only `Language | undefined`.
1 parent eec9b84 commit d8692db

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ Language Improvements:
88

99
- enh(matlab) Add new R2019b `arguments` keyword and fix `enumeration` keyword (#2619) [Andrew Janke][]
1010
- fix(kotlin) Remove very old keywords and update example code (#2623) [kageru][]
11+
- fix(night) Prevent object prototypes method values from being returned in `getLanguage` (#2636) [night][]
1112

1213
[Andrew Janke]: https://github.com/apjanke
1314
[Samia Ali]: https://github.com/samiaab1990
1415
[kageru]: https://github.com/kageru
16+
[night]: https://github.com/night
1517

1618

1719
## Version 10.1.1

src/highlight.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ const HLJS = function(hljs) {
2929

3030
// Global internal variables used within the highlight.js library.
3131
/** @type {Record<string, Language>} */
32-
var languages = {};
32+
var languages = Object.create(null);
3333
/** @type {Record<string, string>} */
34-
var aliases = {};
34+
var aliases = Object.create(null);
3535
/** @type {HLJSPlugin[]} */
3636
var plugins = [];
3737

test/api/getLanguage.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,16 @@ describe('.getLanguage()', () => {
4141
result.should.have.property('aliases').with.containEql('cs');
4242
should.strictEqual(result, hljs.getLanguage('csharp'))
4343
});
44+
45+
it('should not succeed for constructor', () => {
46+
const result = hljs.getLanguage('constructor');
47+
48+
should.strictEqual(result, undefined);
49+
});
50+
51+
it('should not succeed for __proto__', () => {
52+
const result = hljs.getLanguage('__proto__');
53+
54+
should.strictEqual(result, undefined);
55+
});
4456
});

0 commit comments

Comments
 (0)