Skip to content

Commit e949c4e

Browse files
committed
Reject illegal channel IDs in configtxgen
The orderer already refuses these IDs. configtxgen would still write a genesis block the network cannot join. Signed-off-by: Hamed Yousefi <hdyousefi@gmail.com>
1 parent c754e61 commit e949c4e

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎cmd/configtxgen/main.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/hyperledger/fabric-lib-go/bccsp/factory"
2020
"github.com/hyperledger/fabric-lib-go/common/flogging"
2121
cb "github.com/hyperledger/fabric-protos-go-apiv2/common"
22+
"github.com/hyperledger/fabric/common/configtx"
2223
"github.com/hyperledger/fabric/internal/configtxgen/encoder"
2324
"github.com/hyperledger/fabric/internal/configtxgen/genesisconfig"
2425
"github.com/hyperledger/fabric/internal/configtxgen/metadata"
@@ -29,6 +30,9 @@ import (
2930
var logger = flogging.MustGetLogger("common.tools.configtxgen")
3031

3132
func doOutputBlock(config *genesisconfig.Profile, channelID string, outputBlock string) error {
33+
if err := configtx.ValidateChannelID(channelID); err != nil {
34+
return errors.WithMessage(err, "invalid channel ID")
35+
}
3236
pgen, err := encoder.NewBootstrapper(config)
3337
if err != nil {
3438
return errors.WithMessage(err, "could not create bootstrapper")
@@ -55,6 +59,9 @@ func doOutputBlock(config *genesisconfig.Profile, channelID string, outputBlock
5559
}
5660

5761
func doOutputChannelCreateTx(conf, baseProfile *genesisconfig.Profile, channelID string, outputChannelCreateTx string) error {
62+
if err := configtx.ValidateChannelID(channelID); err != nil {
63+
return errors.WithMessage(err, "invalid channel ID")
64+
}
5865
logger.Info("Generating new channel configtx")
5966

6067
var configtx *cb.Envelope

‎cmd/configtxgen/main_test.go‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func TestBftOrdererTypeWithoutV3CapabilitiesShouldRaiseAnError(t *testing.T) {
205205
config.Capabilities["V3_0"] = false
206206

207207
// ### Act & Assert
208-
require.EqualError(t, doOutputBlock(config, "testChannelId", blockDest), "could not create bootstrapper: could not create channel group: could not create orderer group: orderer type BFT must be used with V3_0 channel capability: map[V3_0:false]")
208+
require.EqualError(t, doOutputBlock(config, "testchannelid", blockDest), "could not create bootstrapper: could not create channel group: could not create orderer group: orderer type BFT must be used with V3_0 channel capability: map[V3_0:false]")
209209
}
210210

211211
func TestBftOrdererTypeWithV3CapabilitiesShouldNotRaiseAnError(t *testing.T) {
@@ -215,5 +215,17 @@ func TestBftOrdererTypeWithV3CapabilitiesShouldNotRaiseAnError(t *testing.T) {
215215
config.Capabilities["V3_0"] = true
216216

217217
// ### Act & Assert
218-
require.NoError(t, doOutputBlock(config, "testChannelId", blockDest))
218+
require.NoError(t, doOutputBlock(config, "testchannelid", blockDest))
219+
}
220+
221+
func TestRejectsIllegalChannelID(t *testing.T) {
222+
blockDest := filepath.Join(tmpDir, "block")
223+
config := genesisconfig.Load(genesisconfig.SampleAppChannelInsecureSoloProfile, configtest.GetDevConfigDir())
224+
225+
err := doOutputBlock(config, "Foo", blockDest)
226+
require.ErrorContains(t, err, "invalid channel ID")
227+
228+
configTxDest := filepath.Join(tmpDir, "configtx")
229+
err = doOutputChannelCreateTx(config, nil, "1abc", configTxDest)
230+
require.ErrorContains(t, err, "invalid channel ID")
219231
}

0 commit comments

Comments
 (0)