Protection of sensitive information in source code
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
void main() {
var handler = const shelf.Pipeline().addMiddleware(shelf.logRequests())
.addHandler(_echoRequest);
io.serve(handler, 'localhost', 8080);
}
shelf.Response _echoRequest(shelf.Request request) {
// Sensitive information in source code
var username = 'admin';
var password = '123456';
var email = 'admin@test.com';
var ip = '192.168.1.1';
var aws_secret_key = 'test';
return shelf.Response.ok('Request for "${request.url}"');
}
The Dart code above is an example of a backend application using the Shelf framework. The function
_echoRequest
contains sensitive information in the source code.
The variables
username
,
password
,
email
,
ip
, and
aws_secret_key
are hardcoded into the code, which is a serious security vulnerability. This information should never be stored directly in the source code, especially not without any form of encryption.
If this code were to be committed to a public repository, anyone who has access to that repository would be able to see this sensitive information. This could potentially lead to unauthorized access of any systems that use these credentials.
Additionally, this code reveals a bad practice of storing sensitive information in the repository with no encryption. Even if these are placeholder values, they can be replaced with real sensitive values, which can lead to serious security risks.
The recommended practice is to store sensitive information in environment variables or in a secure and encrypted database, and to use secure methods to access this information. This way, the sensitive information is not exposed in the source code and is kept secure.
import 'dart:io';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
void main() {
var handler = const shelf.Pipeline().addMiddleware(shelf.logRequests())
.addHandler(_echoRequest);
io.serve(handler, 'localhost', 8080);
}
shelf.Response _echoRequest(shelf.Request request) {
// Sensitive information retrieved from environment variables
var username = Platform.environment['USERNAME'];
var password = Platform.environment['PASSWORD'];
var email = Platform.environment['EMAIL'];
var ip = Platform.environment['IP'];
var aws_secret_key = Platform.environment['AWS_SECRET_KEY'];
return shelf.Response.ok('Request for "${request.url}"');
}
The updated code no longer contains hard-coded sensitive information. Instead, it retrieves this information from environment variables. This is a more secure practice as it ensures that sensitive data is not exposed in the code.
The
Platform.environment
method is used to retrieve the values of the environment variables. These variables should be set in the environment where the application is running.
For example, in a Unix-based system, you can set these variables in the terminal before running the application:
export USERNAME=admin
export PASSWORD=123456
export EMAIL=admin@test.com
export IP=192.168.1.1
export AWS_SECRET_KEY=test