@@ -92,6 +92,8 @@ public class AttestationServer {
92
92
private static final long SESSION_LENGTH = 48 * 60 * 60 * 1000 ;
93
93
private static final int HISTORY_PER_PAGE = 20 ;
94
94
95
+ private static final String ORIGIN = "https://attestation.app" ;
96
+
95
97
private static final Logger logger = Logger .getLogger (AttestationServer .class .getName ());
96
98
97
99
// This should be moved to a table in the database so that it can be modified dynamically
@@ -388,6 +390,17 @@ public static void main(final String[] args) throws Exception {
388
390
private abstract static class PostHandler implements HttpHandler {
389
391
protected abstract void handlePost (final HttpExchange exchange ) throws IOException , SQLiteException ;
390
392
393
+ public void checkOrigin (final HttpExchange exchange ) throws GeneralSecurityException {
394
+ final List <String > origin = exchange .getRequestHeaders ().get ("Origin" );
395
+ if (origin != null && !origin .get (0 ).equals (ORIGIN )) {
396
+ throw new GeneralSecurityException ();
397
+ }
398
+ final List <String > fetchSite = exchange .getRequestHeaders ().get ("Sec-Fetch-Site" );
399
+ if (fetchSite != null && !fetchSite .get (0 ).equals ("same-origin" )) {
400
+ throw new GeneralSecurityException ();
401
+ }
402
+ }
403
+
391
404
@ Override
392
405
public final void handle (final HttpExchange exchange ) throws IOException {
393
406
try {
@@ -396,6 +409,12 @@ public final void handle(final HttpExchange exchange) throws IOException {
396
409
exchange .sendResponseHeaders (405 , -1 );
397
410
return ;
398
411
}
412
+ try {
413
+ checkOrigin (exchange );
414
+ } catch (final GeneralSecurityException e ) {
415
+ exchange .sendResponseHeaders (403 , -1 );
416
+ return ;
417
+ }
399
418
handlePost (exchange );
400
419
} catch (final Exception e ) {
401
420
logger .log (Level .SEVERE , "unhandled error handling request" , e );
@@ -406,6 +425,13 @@ public final void handle(final HttpExchange exchange) throws IOException {
406
425
}
407
426
}
408
427
428
+ private abstract static class AppPostHandler extends PostHandler {
429
+ protected abstract void handlePost (final HttpExchange exchange ) throws IOException , SQLiteException ;
430
+
431
+ @ Override
432
+ public void checkOrigin (final HttpExchange exchange ) {}
433
+ }
434
+
409
435
private static final SecureRandom random = new SecureRandom ();
410
436
411
437
private static byte [] generateRandomToken () {
@@ -1248,7 +1274,7 @@ private static void writeAttestationHistoryJson(final HttpExchange exchange, fin
1248
1274
}
1249
1275
}
1250
1276
1251
- private static class ChallengeHandler extends PostHandler {
1277
+ private static class ChallengeHandler extends AppPostHandler {
1252
1278
@ Override
1253
1279
public void handlePost (final HttpExchange exchange ) throws IOException {
1254
1280
final byte [] challenge = AttestationProtocol .getChallenge ();
@@ -1265,7 +1291,7 @@ public void handlePost(final HttpExchange exchange) throws IOException {
1265
1291
}
1266
1292
}
1267
1293
1268
- private static class VerifyHandler extends PostHandler {
1294
+ private static class VerifyHandler extends AppPostHandler {
1269
1295
@ Override
1270
1296
public void handlePost (final HttpExchange exchange ) throws IOException , SQLiteException {
1271
1297
final List <String > authorization = exchange .getRequestHeaders ().get ("Authorization" );
@@ -1347,7 +1373,7 @@ public void handlePost(final HttpExchange exchange) throws IOException, SQLiteEx
1347
1373
}
1348
1374
}
1349
1375
1350
- private static class SubmitHandler extends PostHandler {
1376
+ private static class SubmitHandler extends AppPostHandler {
1351
1377
@ Override
1352
1378
public void handlePost (final HttpExchange exchange ) throws IOException , SQLiteException {
1353
1379
final InputStream input = exchange .getRequestBody ();
0 commit comments