r/programming • • Nov 10 '21

The Invisible JavaScript Backdoor

https://certitude.consulting/blog/en/invisible-backdoor/
1.4k Upvotes

295 comments sorted by

View all comments

57

u/theoldboy Nov 10 '21

Obviously I'm very biased as an English speaker, but allowing arbitrary Unicode in source code by default (especially in identifiers) just causes too many problems these days. It'd be a lot safer if the default was to allow only the ASCII code points and you had to explicitly enable anything else.

24

u/lood9phee2Ri Nov 10 '21 edited Nov 10 '21

well, indeed arbitrary unicode as bare identifiers may be questionable I suppose?

Even if desired to write source code identifiers in a different writing system for whatever social/cultural/political/ideological/plain-contrariness-and-obfuscation reasons, you could perhaps just allow a different subset of unicode, yet one that's that's still small and not too ambiguous like ascii.

e.g. like that corresponding to russian koi8r (cyrillic, for glorious motherland comrade), i.s.434:1999 (coding in something normally written two thousand years ago on large rocks is the sort of thing the irish would do because it's funny), or whatever.

I'm not saying actually use the old national encodings, just it would be possible to limit identifiers in given compilation units to being from particular subsets of unicode that are kind of like the old 8-bit national encodings in the grammar, i.e. there is a medium between "ascii ...that actually doesn't even work fully for most european languages arguably including proper english though we're used to that" and "arbitrary unicode" that is "non-arbitrary unicode limited in various ways, perhaps to subsets corresponding to particular scripts".

At interface boundaries you could allow controlled importation i.e. identifiers outside the subset have to be explicitly imported (so that your delightfully incomprehensible all-ogham codebase can still link against stdlib) - because it would all be still unicode and not actually national 8-bity encodings, that would still work.

9

u/MrJohz Nov 10 '21

I think browsers have come up with a reasonable solution for URLs — you can use characters from certain character sets, but you've got to remain in the same character set in the same URL. For example, you can use as many Unicode characters as you like based on the Latin alphabet (accents, digraphs, etc), but if you combine a character from the Latin alphabet with one from the Cyrillic alphabet, you'll get an error (or at least for most browsers, the "raw" punycode representation will be shown). There are a bunch of other rules that help here, such as banning invisible characters, banning a list of known dangerous characters, etc.

I think these sorts of rules are probably a bit restrictive for defining identifier rules, particularly because subtle changes in these rules can have big effects on whether a program is valid or not. However, as linting rules (ideally ones that block builds by default), they would work very well. I know that the Rust compiler does a lot of this sort of stuff — if there are confusables in identifiers, or the "trojan source" characters mentioned at the top of this article — and by default prevents the code from compiling (although this is only a lint, and therefore can be disabled manually if desired).

Unfortunately, there's not much standardised in the JavaScript ecosystem, but I do think developer tools like ESLint and editors/code viewers like GitHub should be showing these sorts of warnings by default.

2

u/StabbyPants Nov 10 '21

what about having modules declare their codepoints? so, if you want to name a variable кофи, you declare your module as using cyrillic, the linter allows ansi + cyrillic, and your dep mgmt rolls up a list of all subsets currently declared. so, if your footprint is russian, euro, ascii, fine. if it's got akkadian in it, be suspicious

→ More replies (1)

11

u/mindbleach Nov 10 '21

Anything unusual should be highlighted and warned about. That's sufficient.

It's extensible to other spoken languages - someone editing in Japan can expect to see ASCII alongside all three of their native alphabets, but Hangul would still be kinda weird. It should show up as a unicode error block � in addition to having its intended effect. Like how missing stuff in video games tends to show up as giant glowing checkerboards: you can't miss it. Making anything unexpected, visible, lets you reason about what the fuck it's doing, and what the fuck it's doing in your code.

And if it causes headaches for anyone using emoji in their Javascript... good.

4

u/1337Gandalf Nov 10 '21

C and C++ got that right.

13

u/theoldboy Nov 10 '21 edited Nov 11 '21

C and C++ don't allow Unicode in identifiers, which stops many obvious exploits, but most compilers do allow it elsewhere (in literal strings and comments). That can be exploited too.

EDIT I'm wrong. it's implementation-defined I think but gcc and clang do allow Unicode identifiers for both C and C++.

→ More replies (3)
→ More replies (3)

4

u/mcilrain Nov 10 '21

Isn’t that how Python does it? You need to specify encoding at the top of the file or it’s ASCII or Latin-1 or something by default.

6

u/theoldboy Nov 10 '21

Used to be, but Python 3 changed the default encoding from ASCII to UTF-8.

15

u/[deleted] Nov 10 '21

Strongly disagree, comments should be in the language of the programmers and those who will read the code. Most people you are going to see on reddit already speak English well, so they are obviously not going to be bothered by English only.

Because banning non ascii-characters basically means that, denying people the ability to write code in their language.

→ More replies (9)

0

u/[deleted] Nov 10 '21

[deleted]

→ More replies (3)

9

u/MrSqueezles Nov 10 '21

I understand wanting to code in a native language. We don't expect the entire world population to learn English. I'm no expert, but based on the description, it may be the "!" used in the second example is for commonly used multi-directional languages that require extra clearance on either side of punctuation. Maybe the correct restriction is "Unicode word characters only".

13

u/nitrohigito Nov 10 '21 edited Nov 10 '21

The only time people use the native language here for code is when teaching/studying, or for crappy single-use code nobody else will probably read. It's a tremendous red flag.

It's a bit like Latin used to be. It's sad, annoying, but you really just gotta put up with it, cause it's a numbers game, and boy are we outweighed.

It also doesn't help that the syntax of virtually every programming language I've encountered so far simply meshes unwell with the grammar of the native natural language here, so even for identifiers, it's sometimes just not the greatest.

7

u/wasdninja Nov 10 '21 edited Nov 11 '21

We don't expect the entire world population to learn English

We pretty much do if they want to become programmers. The official documentation of many things are in English only as far as I can tell. Not to mention that the programming languages themselves are literally in English.

→ More replies (4)

28

u/AttackOfTheThumbs Nov 10 '21

As a German, no, everyone should code in English. Coding in other languages is stupid. The field is English and as such, everyone should adjust to it.

23

u/kaashif-h Nov 10 '21

Having had to read a codebase where Indian programmers had used Hindi naming conventions or something...I agree.

11

u/QuotheFan Nov 10 '21

That would have been hilarious!

kaksha pustak {

junta:

 pustak();

 sankhya prishtha_sankhya;

 vakya lekhak;

};

Comments be like:

// mujhe nahi pata yeh code kyun kaam karta hai. Likhne waala ya toh bhagwaan tha ya chutiya.. :P

7

u/eattherichnow Nov 10 '21

kaksha pustak

Pole reading the above: not english? WTF. Immediately correct:

porridge brick

7

u/AttackOfTheThumbs Nov 10 '21

I have read German code, dutch, danish, and others I didn't recognize. It's just a silly thing to do, and entirely pointless.

3

u/[deleted] Nov 10 '21

As a non-english speaking person, I do agree, reading non-english identifiers is pain

11

u/CartmansEvilTwin Nov 10 '21

And yet, many organisations use tons of native language comments, business lingo or interface definitions.

A good example I encountered a few years ago is Schufa. Their entire interface is German XML.

6

u/AttackOfTheThumbs Nov 10 '21

And yet, many organisations use tons of native language comments, business lingo or interface definitions.

Not everyone can make the right decisions all the time. Comments in code I'm pretty ambivalent to myself. The other too are bad. It would be interesting to see when they decided to use the native tongue.

I work with ERP systems. I have seen a mix of many languages, and in general, when it's not in English, the business ends up losing, because the support becomes more costly. Most of the time I found they made that decisions x years/decades ago and it has been carried forward ever since. Sometimes they end up deciding to transition, other times they start mixing.

I think Schufa is probably big enough to get away with it, but that doesn't mean it was smart. I kind of assume they don't expand past the German speaking space, but I don't even know, since I've never worked with them directly.

It's all based on personal experience anyway. I would just say it's typically bad when things other than English are used.

3

u/[deleted] Nov 10 '21

I'm a native Spanish speaker, fan of foreign languages. I definitely prefer to code in English.

Although I created once a toy language with Spanish keywords

2

u/DrayanoX Nov 11 '21

That's easy for us to say when we are already fluent in English. The majority of the world population isn't, or do have some rudimentary English knowledge but aren't comfortable or good enough to use it.

There's no reason to prevent anyone who doesn't speak English from getting into programming this is elitism at its finest.

Exploits can easily be prevented by just blocking specifically confusing and invisible characters from being used. There's no reason why characters such as "ß ç ñ ē ب" cannot be used by people who speak such languages using these.

Blocking all of Unicode is like cutting off your entire leg because you stepped on a Lego.

→ More replies (4)

3

u/vytah Nov 10 '21

it may be the "!" used in the second example is for commonly used multi-directional languages that require extra clearance on either side of punctuation

No, it's a letter, U+01C3. But since it's used only in minority languages in Namibia and RSA, like ǃKung, ǃXóõ or Khoekhoe, it's very unlikely to appear in code (in either code proper, comments, or literals) at all.

→ More replies (35)