Test HTTP headers set with `clnrest-csp` and `clnrest-cors-origins` options.
(node_factory)
| 452 | |
| 453 | |
| 454 | def test_http_headers(node_factory): |
| 455 | """Test HTTP headers set with `clnrest-csp` and `clnrest-cors-origins` options.""" |
| 456 | # start a node with clnrest |
| 457 | l1, base_url, ca_cert = start_node_with_clnrest(node_factory) |
| 458 | http_session = http_session_with_retry() |
| 459 | |
| 460 | # Default values for `clnrest-csp` and `clnrest-cors-origins` options |
| 461 | response = http_session.get(base_url + '/v1/list-methods', verify=ca_cert) |
| 462 | assert response.headers['Content-Security-Policy'] == "default-src 'self'; font-src 'self'; img-src 'self' data:; frame-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline';" |
| 463 | assert response.headers['Access-Control-Allow-Origin'] == '*' |
| 464 | # This might happen really early! |
| 465 | l1.daemon.logsearch_start = 0 |
| 466 | l1.daemon.wait_for_log(f'plugin-clnrest: REST server running at {base_url}') |
| 467 | |
| 468 | # Custom values for `clnrest-csp` and `clnrest-cors-origins` options |
| 469 | rest_port = str(node_factory.get_unused_port()) |
| 470 | rest_certs = node_factory.directory + '/clnrest-certs' |
| 471 | l2 = node_factory.get_node(options={ |
| 472 | 'clnrest-port': rest_port, |
| 473 | 'clnrest-certs': rest_certs, |
| 474 | 'clnrest-csp': "default-src 'self'; font-src 'self'; img-src 'self'; frame-src 'self'; style-src 'self'; script-src 'self';", |
| 475 | 'clnrest-cors-origins': ['https://localhost:5500', 'http://192.168.1.30:3030', 'http://192.168.1.10:1010'] |
| 476 | }) |
| 477 | base_url = 'https://127.0.0.1:' + rest_port |
| 478 | # This might happen really early! |
| 479 | l2.daemon.logsearch_start = 0 |
| 480 | l2.daemon.wait_for_log(f'plugin-clnrest: REST server running at {base_url}') |
| 481 | ca_cert = Path(rest_certs) / 'ca.pem' |
| 482 | |
| 483 | response = http_session.get(base_url + '/v1/list-methods', |
| 484 | headers={'Origin': 'http://192.168.1.30:3030'}, |
| 485 | verify=ca_cert) |
| 486 | assert response.headers['Content-Security-Policy'] == "default-src 'self'; font-src 'self'; img-src 'self'; frame-src 'self'; style-src 'self'; script-src 'self';" |
| 487 | assert response.headers['Access-Control-Allow-Origin'] == 'http://192.168.1.30:3030' |
| 488 | response = http_session.get(base_url + '/v1/list-methods', |
| 489 | headers={'Origin': 'http://192.168.1.10:1010'}, |
| 490 | verify=ca_cert) |
| 491 | assert response.headers['Access-Control-Allow-Origin'] == 'http://192.168.1.10:1010' |
| 492 | |
| 493 | |
| 494 | def test_websocket_upgrade_header(node_factory): |
nothing calls this directly
no test coverage detected