Skip to content

Commit 5d275b7

Browse files
committed
Implemented a token scrambler to make the low level GitHub token not directly accessible.
1 parent 1d2880d commit 5d275b7

4 files changed

Lines changed: 104 additions & 3 deletions

File tree

Binary file not shown.

src/de/halirutan/mathematica/errorreporting/AnonymousFeedback.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import javax.annotation.Nullable;
3434
import java.io.IOException;
35+
import java.net.URL;
3536
import java.util.*;
3637
import java.util.Map.Entry;
3738

@@ -40,7 +41,7 @@
4041
*/
4142
class AnonymousFeedback {
4243

43-
private final static String gitAccessToken = "097a2a4e4a94ff65a73508083da690d4565fd038";
44+
private final static String tokenFile = "de/halirutan/mathematica/errorreporting/ScrambledToken.bin";
4445
private final static String gitRepoUser = "Mathematica-IntelliJ-Plugin";
4546
private final static String gitRepo = "Auto-generated-issues-for-the-Mathematica-Plugin";
4647

@@ -61,6 +62,11 @@ static SubmittedReportInfo sendFeedback(LinkedHashMap<String, String> environmen
6162

6263
final SubmittedReportInfo result;
6364
try {
65+
final URL resource = AnonymousFeedback.class.getClassLoader().getResource(tokenFile);
66+
if (resource == null) {
67+
throw new IOException("Could not decrypt access token");
68+
}
69+
final String gitAccessToken = GitHubAccessTokenScrambler.decrypt(resource.getFile());
6470
GitHubClient client = new GitHubClient();
6571
client.setOAuth2Token(gitAccessToken);
6672
RepositoryId repoID = new RepositoryId(gitRepoUser, gitRepo);
@@ -85,7 +91,7 @@ static SubmittedReportInfo sendFeedback(LinkedHashMap<String, String> environmen
8591
final String message = ErrorReportBundle.message(isNewIssue ? "git.issue.text" : "git.issue.duplicate.text", htmlUrl, id);
8692
result = new SubmittedReportInfo(htmlUrl, message, isNewIssue ? SubmissionStatus.NEW_ISSUE : SubmissionStatus.DUPLICATE);
8793
return result;
88-
} catch (IOException e) {
94+
} catch (Exception e) {
8995
return new SubmittedReportInfo(null, ErrorReportBundle.message("report.error.connection.failure"), SubmissionStatus.FAILED);
9096
}
9197
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2017 Patrick Scheibe
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy
4+
* of this software and associated documentation files (the "Software"), to deal
5+
* in the Software without restriction, including without limitation the rights
6+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
* copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in
11+
* all copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
* THE SOFTWARE.
20+
*/
21+
22+
package de.halirutan.mathematica.errorreporting;
23+
24+
import org.apache.commons.codec.binary.Base64;
25+
26+
import javax.crypto.Cipher;
27+
import javax.crypto.spec.IvParameterSpec;
28+
import javax.crypto.spec.SecretKeySpec;
29+
import java.io.*;
30+
31+
/**
32+
* Provides functionality to encode and decode secret tokens to make them not directly readable. Let me be clear:
33+
* THIS IS THE OPPOSITE OF SECURITY!
34+
* @author patrick (20.06.17).
35+
*/
36+
public class GitHubAccessTokenScrambler {
37+
38+
private static final String myInitVector = "RandomInitVector";
39+
private static final String myKey = "GitHubErrorToken";
40+
41+
public static void main(String[] args) {
42+
if (args.length != 2) {
43+
return;
44+
}
45+
String horse = args[0];
46+
String outputFile = args[1];
47+
try {
48+
final String e = encrypt(horse);
49+
final ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(outputFile));
50+
o.writeObject(e);
51+
o.close();
52+
} catch (Exception e) {
53+
e.printStackTrace();
54+
}
55+
}
56+
57+
private static String encrypt(String value) {
58+
try {
59+
IvParameterSpec iv = new IvParameterSpec(myInitVector.getBytes("UTF-8"));
60+
SecretKeySpec keySpec = new SecretKeySpec(myKey.getBytes("UTF-8"), "AES");
61+
62+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
63+
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
64+
65+
byte[] encrypted = cipher.doFinal(value.getBytes());
66+
return Base64.encodeBase64String(encrypted);
67+
} catch (Exception ex) {
68+
ex.printStackTrace();
69+
}
70+
return null;
71+
}
72+
73+
static String decrypt(String file) throws Exception {
74+
String in;
75+
final ObjectInputStream o = new ObjectInputStream(new FileInputStream(file));
76+
in = (String) o.readObject();
77+
IvParameterSpec iv = new IvParameterSpec(myInitVector.getBytes("UTF-8"));
78+
SecretKeySpec keySpec = new SecretKeySpec(myKey.getBytes("UTF-8"), "AES");
79+
80+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
81+
cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
82+
83+
byte[] original = cipher.doFinal(Base64.decodeBase64(in));
84+
return new String(original);
85+
}
86+
}

src/de/halirutan/mathematica/errorreporting/package-info.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* </p>
2828
* <ul>
2929
* <li>@see <a href="https://android.googlesource.com/platform/tools/adt/idea/+/master/android/src/com/android/tools/idea/diagnostics/error/ErrorReporter.java">the android implementation</a></li>
30-
* <li>@see <a href="http://devnet.jetbrains.com/message/5526206>a post on jetbrains</a></li>
30+
* <li>@see <a href="http://devnet.jetbrains.com/message/5526206">a post on jetbrains</a></li>
3131
* </ul>
3232
* <p>
3333
* Furthermore, an earlier implementation of Jon Akhtar (https://github.com/sylvanaar) gave me the idea of finding duplicates,
@@ -50,5 +50,14 @@
5050
* which is run as background task {@link de.halirutan.mathematica.errorreporting.AnonymousFeedbackTask}.
5151
* The main class that is also registered in the plugin.xml and starts the whole procedure is {@link de.halirutan.mathematica.errorreporting.GitHubErrorReporter}.
5252
* </p>
53+
* <p>
54+
* You might consider using a subset of this functionality by not using the duplicate finding and commenting feature.
55+
* The advantage of this is, that you should be able to implement this without having a real fake user in the background
56+
* that reports for you. Creating issues can be done by anyone, commenting and labeling not. For commenting on an existing issue,
57+
* you need a higher level of access to GitHub and your requests need to be backed by a user/password or token. The problem
58+
* here is clearly that you need to give the plugin access to this token. No matter how you secure it, if the code is
59+
* open access it is not hard to decipher your token. Therefore, always use tokens with the lowest possible access level
60+
* (in this case, access to public repositories only) if you need to.
61+
* </p>
5362
*/
5463
package de.halirutan.mathematica.errorreporting;

0 commit comments

Comments
 (0)